Skip to content

Commit 749dc46

Browse files
d-csclaude
andauthored
feat(webapp): link Sentry events to OTel traces via trace_id (#3531)
## Summary Stamps the active OpenTelemetry `trace_id` and `span_id` onto every Sentry event captured from the webapp, so engineers can copy a `trace_id` from a Sentry issue and search for the corresponding trace in any OTel-aware backend. Also adds an `otel_sampled` tag to indicate whether the trace was head-sampled — a cheap signal for whether the link will resolve to span data or hit a missing trace. ## Why Sentry and OTel were OTel-disconnected: `apps/webapp/sentry.server.ts` initialised Sentry with `skipOpenTelemetrySetup: true`, and no error-capture site (`logger.server.ts`, the Remix-wrapped `handleError`, the root `ErrorBoundary`) attached OTel context to the event. With many spans/sec across services, getting from a Sentry issue to its trace was guesswork. ## Approach Single global Sentry event processor, registered immediately after `Sentry.init`. On each event it reads `trace.getActiveSpan()?.spanContext()` via `@opentelemetry/api`, then writes: - `event.contexts.trace.trace_id` and `event.contexts.trace.span_id` (Sentry's native trace context fields) - `event.tags.otel_sampled` = `"true"` | `"false"` (derived from `traceFlags`) If no active span (module-load errors, scheduled timers without a context, primary cluster process), the processor returns the event unmodified — Sentry's default propagation context fills in. Implementation is co-located in `apps/webapp/sentry.server.ts` (no separate helper module — `sentry.server.ts` is built standalone by esbuild and a separate import would have required a new bundling step). Helper functions are exported so the unit tests can reach them without re-running `Sentry.init`. ## Non-goals (deliberate) - No sample rate change. ~95% of Sentry events will carry a `trace_id` that returns no spans in the tracing backend (head-sampled out). The `otel_sampled` tag makes that obvious at a glance. Raising find-rate is a separate conversation with cost trade-offs. - No user/org tags or `Sentry.setUser` (would need auth-helper + per-request scope wiring across multiple worker entrypoints — separate ticket). - Webapp image only. No changes to supervisor or CLI workers. ## Test plan - [x] Unit tests in `apps/webapp/test/sentryTraceContext.server.test.ts` — 9 tests covering: helper returns \`undefined\` with no active span; returns \`traceId\`/\`spanId\`/\`sampled=true\` for a recording span; returns \`sampled=false\` for a non-recording span; processor leaves the event unchanged with no active span; processor stamps \`trace_id\`/\`span_id\` onto \`contexts.trace\`; preserves existing \`contexts.trace\` fields; tags \`otel_sampled\` correctly for both sampled and non-sampled cases; never throws if \`@opentelemetry/api\` access throws. - [x] \`pnpm run typecheck --filter webapp\` passes. - [x] Manually verified end-to-end against a sandboxed Sentry project: confirmed both sampled and non-sampled traces correctly populate \`contexts.trace.trace_id\` matching the OTel ids logged from the loader, and the \`otel_sampled\` tag appears with the expected value. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3e6458f commit 749dc46

5 files changed

Lines changed: 189 additions & 1 deletion

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
area: webapp
3+
type: improvement
4+
---
5+
6+
Stamp the active OpenTelemetry trace_id and span_id onto every Sentry event so issues can be cross-referenced with traces in any OTel backend.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { type Span, TraceFlags, trace } from "@opentelemetry/api";
2+
import type { Event, EventHint } from "@sentry/remix";
3+
4+
export type GetActiveSpan = () => Span | undefined;
5+
6+
const defaultGetActiveSpan: GetActiveSpan = () => trace.getActiveSpan();
7+
8+
export function getActiveTraceIds(
9+
getActiveSpan: GetActiveSpan = defaultGetActiveSpan
10+
): { traceId: string; spanId: string; sampled: boolean } | undefined {
11+
try {
12+
const span = getActiveSpan();
13+
if (!span) return undefined;
14+
const ctx = span.spanContext();
15+
return {
16+
traceId: ctx.traceId,
17+
spanId: ctx.spanId,
18+
sampled: (ctx.traceFlags & TraceFlags.SAMPLED) !== 0,
19+
};
20+
} catch {
21+
return undefined;
22+
}
23+
}
24+
25+
export function addOtelTraceContextToEvent(
26+
event: Event,
27+
_hint: EventHint,
28+
getActiveSpan: GetActiveSpan = defaultGetActiveSpan
29+
): Event {
30+
const ids = getActiveTraceIds(getActiveSpan);
31+
if (!ids) return event;
32+
// We intentionally overwrite Sentry's own trace_id/span_id on contexts.trace.
33+
// With skipOpenTelemetrySetup: true, Sentry generates an internal trace_id
34+
// unrelated to OTel; replacing it with the active OTel ids is the whole
35+
// point of this processor — it makes Sentry issues navigable to the
36+
// corresponding OTel trace in any backend.
37+
return {
38+
...event,
39+
contexts: {
40+
...event.contexts,
41+
trace: {
42+
...event.contexts?.trace,
43+
trace_id: ids.traceId,
44+
span_id: ids.spanId,
45+
},
46+
},
47+
tags: {
48+
...event.tags,
49+
otel_sampled: ids.sampled ? "true" : "false",
50+
},
51+
};
52+
}

apps/webapp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"build": "run-s build:** && pnpm run upload:sourcemaps",
88
"build:remix": "remix build --sourcemap",
99
"build:server": "esbuild --platform=node --format=cjs ./server.ts --outdir=build --sourcemap",
10-
"build:sentry": "esbuild --platform=node --format=cjs ./sentry.server.ts --outdir=build --sourcemap",
10+
"build:sentry": "esbuild --platform=node --format=cjs --outbase=. ./sentry.server.ts ./app/utils/sentryTraceContext.server.ts --outdir=build --sourcemap",
1111
"dev": "cross-env PORT=3030 remix dev -c \"node ./build/server.js\"",
1212
"dev:worker": "cross-env NODE_PATH=../../node_modules/.pnpm/node_modules node ./build/server.js",
1313
"format": "prettier --write .",

apps/webapp/sentry.server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Sentry from "@sentry/remix";
2+
import { addOtelTraceContextToEvent } from "./app/utils/sentryTraceContext.server";
23

34
if (process.env.SENTRY_DSN) {
45
console.log("🔭 Initializing Sentry");
@@ -29,4 +30,6 @@ if (process.env.SENTRY_DSN) {
2930
ignoreErrors: ["queryRoute() call aborted", /^ServiceValidationError(?::|$)/],
3031
includeLocalVariables: false,
3132
});
33+
34+
Sentry.addEventProcessor(addOtelTraceContextToEvent);
3235
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { ROOT_CONTEXT, TraceFlags, context, trace } from "@opentelemetry/api";
2+
import { describe, expect, it } from "vitest";
3+
import {
4+
addOtelTraceContextToEvent,
5+
getActiveTraceIds,
6+
} from "../app/utils/sentryTraceContext.server";
7+
import { createInMemoryTracing } from "./utils/tracing";
8+
9+
describe("getActiveTraceIds", () => {
10+
it("returns undefined when no OTel span is active", () => {
11+
expect(getActiveTraceIds()).toBeUndefined();
12+
});
13+
14+
it("returns the trace_id, span_id, and sampled=true for an active recording span", () => {
15+
const { tracer } = createInMemoryTracing();
16+
17+
tracer.startActiveSpan("test-span", (span) => {
18+
const ids = getActiveTraceIds();
19+
expect(ids).toEqual({
20+
traceId: span.spanContext().traceId,
21+
spanId: span.spanContext().spanId,
22+
sampled: true,
23+
});
24+
span.end();
25+
});
26+
});
27+
28+
it("returns sampled=false when the active span is non-recording", () => {
29+
// Initialise the global context manager (createInMemoryTracing does this
30+
// as a side effect of NodeTracerProvider.register()).
31+
createInMemoryTracing();
32+
33+
const nonSampledSpan = trace.wrapSpanContext({
34+
traceId: "0123456789abcdef0123456789abcdef",
35+
spanId: "0123456789abcdef",
36+
traceFlags: TraceFlags.NONE,
37+
});
38+
39+
context.with(trace.setSpan(ROOT_CONTEXT, nonSampledSpan), () => {
40+
expect(getActiveTraceIds()).toEqual({
41+
traceId: "0123456789abcdef0123456789abcdef",
42+
spanId: "0123456789abcdef",
43+
sampled: false,
44+
});
45+
});
46+
});
47+
});
48+
49+
describe("addOtelTraceContextToEvent", () => {
50+
it("returns the event unchanged when no OTel span is active", () => {
51+
const event = { message: "boom" };
52+
const result = addOtelTraceContextToEvent(event, {});
53+
expect(result).toBe(event);
54+
expect(result).toEqual({ message: "boom" });
55+
});
56+
57+
it("stamps trace_id and span_id from the active span onto event.contexts.trace", () => {
58+
const { tracer } = createInMemoryTracing();
59+
60+
tracer.startActiveSpan("test-span", (span) => {
61+
const event = { message: "boom" };
62+
const result = addOtelTraceContextToEvent(event, {});
63+
expect(result.contexts?.trace?.trace_id).toBe(span.spanContext().traceId);
64+
expect(result.contexts?.trace?.span_id).toBe(span.spanContext().spanId);
65+
span.end();
66+
});
67+
});
68+
69+
it("tags the event with otel_sampled=true when the active span is recording", () => {
70+
const { tracer } = createInMemoryTracing();
71+
72+
tracer.startActiveSpan("test-span", (span) => {
73+
const event = { message: "boom" };
74+
const result = addOtelTraceContextToEvent(event, {});
75+
expect(result.tags?.otel_sampled).toBe("true");
76+
span.end();
77+
});
78+
});
79+
80+
it("tags the event with otel_sampled=false when the active span is non-recording", () => {
81+
createInMemoryTracing();
82+
83+
const nonSampledSpan = trace.wrapSpanContext({
84+
traceId: "0123456789abcdef0123456789abcdef",
85+
spanId: "0123456789abcdef",
86+
traceFlags: TraceFlags.NONE,
87+
});
88+
89+
context.with(trace.setSpan(ROOT_CONTEXT, nonSampledSpan), () => {
90+
const event = { message: "boom" };
91+
const result = addOtelTraceContextToEvent(event, {});
92+
expect(result.tags?.otel_sampled).toBe("false");
93+
});
94+
});
95+
96+
it("preserves existing event.contexts.trace fields", () => {
97+
const { tracer } = createInMemoryTracing();
98+
99+
tracer.startActiveSpan("test-span", (span) => {
100+
const event = {
101+
message: "boom",
102+
contexts: {
103+
trace: { op: "http.server", description: "GET /things" },
104+
runtime: { name: "node" },
105+
},
106+
};
107+
const result = addOtelTraceContextToEvent(event, {});
108+
expect(result.contexts?.trace).toMatchObject({
109+
op: "http.server",
110+
description: "GET /things",
111+
trace_id: span.spanContext().traceId,
112+
span_id: span.spanContext().spanId,
113+
});
114+
expect(result.contexts?.runtime).toEqual({ name: "node" });
115+
span.end();
116+
});
117+
});
118+
119+
it("returns the event unchanged if reading the OTel context throws", () => {
120+
const throwingAccessor = () => {
121+
throw new Error("otel api blew up");
122+
};
123+
const event = { message: "boom" };
124+
const result = addOtelTraceContextToEvent(event, {}, throwingAccessor);
125+
expect(result).toBe(event);
126+
});
127+
});

0 commit comments

Comments
 (0)