Skip to content

Commit

Permalink
Fetch: set the parent context of the fetch (#73)
Browse files Browse the repository at this point in the history
* Fetch: set the parent context of the fetch

* changeset
  • Loading branch information
dvoytenko authored Mar 29, 2024
1 parent ca0db56 commit 5019762
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-dodos-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vercel/otel": patch
---

(fix) Use the fetch's span context for propagation, not parent's.
8 changes: 6 additions & 2 deletions packages/otel/src/instrumentations/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SpanStatusCode,
propagation,
context,
trace as traceApi,
} from "@opentelemetry/api";
import type {
Attributes,
Expand Down Expand Up @@ -272,6 +273,8 @@ export class FetchInstrumentation implements Instrumentation {
const spanName =
init?.opentelemetry?.spanName ?? `fetch ${req.method} ${req.url}`;

const parentContext = context.active();

const span = tracer.startSpan(
spanName,
{
Expand All @@ -283,15 +286,16 @@ export class FetchInstrumentation implements Instrumentation {
...init?.opentelemetry?.attributes,
},
},
context.active()
parentContext
);
if (!span.isRecording() || !isSampled(span.spanContext().traceFlags)) {
span.end();
return originalFetch(input, init);
}

if (shouldPropagate(url, init)) {
propagation.inject(context.active(), req.headers, HEADERS_SETTER);
const fetchContext = traceApi.setSpan(parentContext, span);
propagation.inject(fetchContext, req.headers, HEADERS_SETTER);
}

if (attributesFromRequestHeaders) {
Expand Down
35 changes: 31 additions & 4 deletions tests/e2e/test/vercel-deployment/render-outbound.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, it } from "vitest";
import { SpanKind, SpanStatusCode } from "@opentelemetry/api";
import type { ITrace } from "collector";
import { describe } from "../../lib/with-bridge";
import { expectTrace } from "../../lib/expect-trace";

Expand Down Expand Up @@ -59,13 +60,26 @@ describe("vercel deployment: outbound", {}, (props) => {
],
});

let fetchSpan: ITrace["spans"][0] | undefined;
collector.getAllTraces().forEach((trace) => {
trace.spans.forEach((span) => {
if (span.name === `fetch POST http://localhost:${bridge.port}/`) {
fetchSpan = span;
}
});
});
expect(fetchSpan).toBeDefined();
if (!fetchSpan) {
throw new Error("already asserted");
}

const fetches = bridge.fetches;
expect(fetches).toHaveLength(1);

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const fetch = fetches[0]!;
expect(fetch.headers.get("traceparent")).toMatch(
/00-[0-9a-fA-F]{32}-[0-9a-fA-F]{16}-01/
expect(fetch.headers.get("traceparent")).toEqual(
`00-${fetchSpan.traceId}-${fetchSpan.spanId}-01`
);
});

Expand Down Expand Up @@ -124,13 +138,26 @@ describe("vercel deployment: outbound", {}, (props) => {
],
});

let fetchSpan: ITrace["spans"][0] | undefined;
collector.getAllTraces().forEach((trace) => {
trace.spans.forEach((span) => {
if (span.name === `fetch POST http://localhost:${bridge.port}/`) {
fetchSpan = span;
}
});
});
expect(fetchSpan).toBeDefined();
if (!fetchSpan) {
throw new Error("already asserted");
}

const fetches = bridge.fetches;
expect(fetches).toHaveLength(1);

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const fetch = fetches[0]!;
expect(fetch.headers.get("traceparent")).toMatch(
/00-[0-9a-fA-F]{32}-[0-9a-fA-F]{16}-01/
expect(fetch.headers.get("traceparent")).toEqual(
`00-${fetchSpan.traceId}-${fetchSpan.spanId}-01`
);
});

Expand Down

0 comments on commit 5019762

Please sign in to comment.