Skip to content

Commit

Permalink
Support b3 (zipkin) OpenTelemetry trace propagation headers (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-chambers authored Apr 17, 2024
1 parent 7377b6a commit 322b5dc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 4.5.0
- Support [b3](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-propagator-b3#b3-formats) (zipkin) OpenTelemetry trace propagation headers ([#28](https://github.com/hasura/ndc-sdk-typescript/pull/28))

## 4.4.0
- Updated to support [v0.1.2 of the NDC Spec](https://hasura.github.io/ndc-spec/specification/changelog.html#012)
- More precise [type representations](https://hasura.github.io/ndc-spec/specification/schema/scalar-types.html#type-representations) of scalar types were added and the general numeric ones were deprecated (`number` and `integer`)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hasura/ndc-sdk-typescript",
"version": "4.4.0",
"version": "4.5.0",
"description": "",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
14 changes: 14 additions & 0 deletions src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { FastifyInstrumentation } from "@opentelemetry/instrumentation-fastify";
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { FetchInstrumentation } from "@opentelemetry/instrumentation-fetch";
import { Attributes, Span, SpanStatusCode, Tracer } from "@opentelemetry/api";
import { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } from "@opentelemetry/core";
import { B3Propagator, B3InjectEncoding } from "@opentelemetry/propagator-b3"

let sdk: opentelemetry.NodeSDK | null = null;

Expand Down Expand Up @@ -52,9 +54,21 @@ export function initTelemetry(
new PinoInstrumentation({
logHook: (span, record, level) => {
record["resource.service.name"] = serviceName;
// This logs the parent span ID in the pino logs, useful for debugging propagation.
// parentSpanId is an internal property, hence the cast to any, because I can't
// seem to find a way to get at it through a supported API 😭
record["parent_span_id"] = (span as any).parentSpanId;
},
}),
],
textMapPropagator: new CompositePropagator({
propagators: [
new W3CTraceContextPropagator(),
new W3CBaggagePropagator(),
new B3Propagator(),
new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER }),
]
}),
});

process.on("beforeExit", async () => {
Expand Down
8 changes: 4 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export async function startServer<Configuration, State>(
Body: QueryRequest;
}>
) => {
request.log.debug({ requestBody: request.body }, "Query Request");
request.log.debug({ requestHeaders: request.headers, requestBody: request.body }, "Query Request");

const queryResponse = await withActiveSpan(
tracer,
Expand All @@ -201,7 +201,7 @@ export async function startServer<Configuration, State>(
},
},
async (request: FastifyRequest<{ Body: QueryRequest }>) => {
request.log.debug({ requestBody: request.body }, "Explain Request");
request.log.debug({ requestHeaders: request.headers, requestBody: request.body }, "Explain Request");

const explainResponse = await withActiveSpan(
tracer,
Expand Down Expand Up @@ -233,7 +233,7 @@ export async function startServer<Configuration, State>(
Body: MutationRequest;
}>
): Promise<MutationResponse> => {
request.log.debug({ requestBody: request.body }, "Mutation Request");
request.log.debug({ requestHeaders: request.headers, requestBody: request.body }, "Mutation Request");

const mutationResponse = await withActiveSpan(
tracer,
Expand Down Expand Up @@ -262,7 +262,7 @@ export async function startServer<Configuration, State>(
},
async (request: FastifyRequest<{ Body: MutationRequest }>) => {
request.log.debug(
{ requestBody: request.body },
{ requestHeaders: request.headers, requestBody: request.body },
"Mutation Explain Request"
);

Expand Down

0 comments on commit 322b5dc

Please sign in to comment.