Skip to content

Commit eac40ed

Browse files
Add tracing documentation and export tracing functions (#16)
* Add tracing documentation and export tracing functions * Update changelog
1 parent 86b98e9 commit eac40ed

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This changelog documents the changes between release versions.
88
Changes to be included in the next upcoming release
99

1010
- 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
11+
- 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))
1112

1213
## [1.0.0] - 2024-02-22
1314
### ndc-lambda-sdk

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,34 @@ Descriptions are collected for:
302302
* Types
303303
* Type properties
304304

305+
### Tracing
306+
Your functions are automatically instrumented with OpenTelemetry traces that Hasura will capture.
307+
308+
By default, the following spans are emitted:
309+
* `handleQuery`/`handleMutation` - wraps the request from the Hasura DDN to the connector
310+
* `prepare arguments` - wraps the process of preparing arguments to pass to your function
311+
* `function invocation` - wraps a (potentially) parallel function invocation during a query
312+
* `Function: <function name>` - wraps the invocation of your function
313+
* `reshape result` - wraps the projection of the function result to match the requirements of the GraphQL selection set
314+
315+
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`:
316+
317+
```typescript
318+
import opentelemetry from '@opentelemetry/api';
319+
import * as sdk from "@hasura/ndc-lambda-sdk"
320+
321+
const tracer = opentelemetry.trace.getTracer("my functions"); // Name your functions service here
322+
323+
export async function doSomething(): Promise<string> {
324+
const spanAttributes = { myAttribute: "value" };
325+
return await sdk.withActiveSpan(tracer, "my span name", async () => {
326+
return await doSomethingExpensive();
327+
}, spanAttributes);
328+
}
329+
```
330+
331+
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.
332+
305333
## Deploying with `hasura3 connector create`
306334

307335
You will need:

ndc-lambda-sdk/src/sdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { JSONValue } from "./schema";
22
export { ConnectorError, BadRequest, Forbidden, Conflict, UnprocessableContent, InternalServerError, NotSupported, BadGateway } from "@hasura/ndc-sdk-typescript";
3+
export { withActiveSpan, USER_VISIBLE_SPAN_ATTRIBUTE } from "@hasura/ndc-sdk-typescript/instrumentation"
34
export { ErrorDetails, getErrorDetails } from "./execution";

0 commit comments

Comments
 (0)