Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracing documentation and export tracing functions #16

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This changelog documents the changes between release versions.
Changes to be included in the next upcoming release

- Updated to [NDC TypeScript SDK v4.2.0](https://github.com/hasura/ndc-sdk-typescript/releases/tag/v4.2.0) to include OpenTelemetry improvements. Traced spans should now appear in the Hasura Console
- Custom OpenTelemetry trace spans can now be emitted by creating an OpenTelemetry tracer and using it with `sdk.withActiveSpan` ([#16](https://github.com/hasura/ndc-nodejs-lambda/pull/16))

## [1.0.0] - 2024-02-22
### ndc-lambda-sdk
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,34 @@ Descriptions are collected for:
* Types
* Type properties

### Tracing
Your functions are automatically instrumented with OpenTelemetry traces that Hasura will capture.

By default, the following spans are emitted:
* `handleQuery`/`handleMutation` - wraps the request from the Hasura DDN to the connector
* `prepare arguments` - wraps the process of preparing arguments to pass to your function
* `function invocation` - wraps a (potentially) parallel function invocation during a query
* `Function: <function name>` - wraps the invocation of your function
* `reshape result` - wraps the projection of the function result to match the requirements of the GraphQL selection set

If you want to add additional spans around your own code, you can do so by using the OpenTelemetry SDK and `withActiveSpan` from `ndc-lambda-sdk`:

```typescript
import opentelemetry from '@opentelemetry/api';
import * as sdk from "@hasura/ndc-lambda-sdk"

const tracer = opentelemetry.trace.getTracer("my functions"); // Name your functions service here

export async function doSomething(): Promise<string> {
const spanAttributes = { myAttribute: "value" };
return await sdk.withActiveSpan(tracer, "my span name", async () => {
return await doSomethingExpensive();
}, spanAttributes);
}
```

The span will be wrapped around the function you pass to `sdk.withActiveSpan`. The function can optionally be an async function that returns a Promise, and if so, the span will be ended when the Promise resolves.

## Deploying with `hasura3 connector create`

You will need:
Expand Down
1 change: 1 addition & 0 deletions ndc-lambda-sdk/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { JSONValue } from "./schema";
export { ConnectorError, BadRequest, Forbidden, Conflict, UnprocessableContent, InternalServerError, NotSupported, BadGateway } from "@hasura/ndc-sdk-typescript";
export { withActiveSpan, USER_VISIBLE_SPAN_ATTRIBUTE } from "@hasura/ndc-sdk-typescript/instrumentation"
export { ErrorDetails, getErrorDetails } from "./execution";