-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(webapp): link Sentry events to OTel traces via trace_id #3531
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
Open
d-cs
wants to merge
1
commit into
main
Choose a base branch
from
align-sentry-axiom-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: improvement | ||
| --- | ||
|
|
||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { type Span, TraceFlags, trace } from "@opentelemetry/api"; | ||
| import type { Event, EventHint } from "@sentry/remix"; | ||
|
|
||
| export type GetActiveSpan = () => Span | undefined; | ||
|
|
||
| const defaultGetActiveSpan: GetActiveSpan = () => trace.getActiveSpan(); | ||
|
|
||
| export function getActiveTraceIds( | ||
| getActiveSpan: GetActiveSpan = defaultGetActiveSpan | ||
| ): { traceId: string; spanId: string; sampled: boolean } | undefined { | ||
| try { | ||
| const span = getActiveSpan(); | ||
| if (!span) return undefined; | ||
| const ctx = span.spanContext(); | ||
| return { | ||
| traceId: ctx.traceId, | ||
| spanId: ctx.spanId, | ||
| sampled: (ctx.traceFlags & TraceFlags.SAMPLED) !== 0, | ||
| }; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| export function addOtelTraceContextToEvent( | ||
| event: Event, | ||
| _hint: EventHint, | ||
| getActiveSpan: GetActiveSpan = defaultGetActiveSpan | ||
| ): Event { | ||
| const ids = getActiveTraceIds(getActiveSpan); | ||
| if (!ids) return event; | ||
| // We intentionally overwrite Sentry's own trace_id/span_id on contexts.trace. | ||
| // With skipOpenTelemetrySetup: true, Sentry generates an internal trace_id | ||
| // unrelated to OTel; replacing it with the active OTel ids is the whole | ||
| // point of this processor — it makes Sentry issues navigable to the | ||
| // corresponding OTel trace in any backend. | ||
| return { | ||
| ...event, | ||
| contexts: { | ||
| ...event.contexts, | ||
| trace: { | ||
| ...event.contexts?.trace, | ||
| trace_id: ids.traceId, | ||
| span_id: ids.spanId, | ||
| }, | ||
| }, | ||
| tags: { | ||
| ...event.tags, | ||
| otel_sampled: ids.sampled ? "true" : "false", | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { ROOT_CONTEXT, TraceFlags, context, trace } from "@opentelemetry/api"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| addOtelTraceContextToEvent, | ||
| getActiveTraceIds, | ||
| } from "../app/utils/sentryTraceContext.server"; | ||
| import { createInMemoryTracing } from "./utils/tracing"; | ||
|
|
||
| describe("getActiveTraceIds", () => { | ||
| it("returns undefined when no OTel span is active", () => { | ||
| expect(getActiveTraceIds()).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns the trace_id, span_id, and sampled=true for an active recording span", () => { | ||
| const { tracer } = createInMemoryTracing(); | ||
|
|
||
| tracer.startActiveSpan("test-span", (span) => { | ||
| const ids = getActiveTraceIds(); | ||
| expect(ids).toEqual({ | ||
| traceId: span.spanContext().traceId, | ||
| spanId: span.spanContext().spanId, | ||
| sampled: true, | ||
| }); | ||
| span.end(); | ||
| }); | ||
| }); | ||
|
|
||
| it("returns sampled=false when the active span is non-recording", () => { | ||
| // Initialise the global context manager (createInMemoryTracing does this | ||
| // as a side effect of NodeTracerProvider.register()). | ||
| createInMemoryTracing(); | ||
|
|
||
| const nonSampledSpan = trace.wrapSpanContext({ | ||
| traceId: "0123456789abcdef0123456789abcdef", | ||
| spanId: "0123456789abcdef", | ||
| traceFlags: TraceFlags.NONE, | ||
| }); | ||
|
|
||
| context.with(trace.setSpan(ROOT_CONTEXT, nonSampledSpan), () => { | ||
| expect(getActiveTraceIds()).toEqual({ | ||
| traceId: "0123456789abcdef0123456789abcdef", | ||
| spanId: "0123456789abcdef", | ||
| sampled: false, | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| describe("addOtelTraceContextToEvent", () => { | ||
| it("returns the event unchanged when no OTel span is active", () => { | ||
| const event = { message: "boom" }; | ||
| const result = addOtelTraceContextToEvent(event, {}); | ||
| expect(result).toBe(event); | ||
| expect(result).toEqual({ message: "boom" }); | ||
| }); | ||
|
|
||
| it("stamps trace_id and span_id from the active span onto event.contexts.trace", () => { | ||
| const { tracer } = createInMemoryTracing(); | ||
|
|
||
| tracer.startActiveSpan("test-span", (span) => { | ||
| const event = { message: "boom" }; | ||
| const result = addOtelTraceContextToEvent(event, {}); | ||
| expect(result.contexts?.trace?.trace_id).toBe(span.spanContext().traceId); | ||
| expect(result.contexts?.trace?.span_id).toBe(span.spanContext().spanId); | ||
| span.end(); | ||
| }); | ||
| }); | ||
|
|
||
| it("tags the event with otel_sampled=true when the active span is recording", () => { | ||
| const { tracer } = createInMemoryTracing(); | ||
|
|
||
| tracer.startActiveSpan("test-span", (span) => { | ||
| const event = { message: "boom" }; | ||
| const result = addOtelTraceContextToEvent(event, {}); | ||
| expect(result.tags?.otel_sampled).toBe("true"); | ||
| span.end(); | ||
| }); | ||
| }); | ||
|
|
||
| it("tags the event with otel_sampled=false when the active span is non-recording", () => { | ||
| createInMemoryTracing(); | ||
|
|
||
| const nonSampledSpan = trace.wrapSpanContext({ | ||
| traceId: "0123456789abcdef0123456789abcdef", | ||
| spanId: "0123456789abcdef", | ||
| traceFlags: TraceFlags.NONE, | ||
| }); | ||
|
|
||
| context.with(trace.setSpan(ROOT_CONTEXT, nonSampledSpan), () => { | ||
| const event = { message: "boom" }; | ||
| const result = addOtelTraceContextToEvent(event, {}); | ||
| expect(result.tags?.otel_sampled).toBe("false"); | ||
| }); | ||
| }); | ||
|
|
||
| it("preserves existing event.contexts.trace fields", () => { | ||
| const { tracer } = createInMemoryTracing(); | ||
|
|
||
| tracer.startActiveSpan("test-span", (span) => { | ||
| const event = { | ||
| message: "boom", | ||
| contexts: { | ||
| trace: { op: "http.server", description: "GET /things" }, | ||
| runtime: { name: "node" }, | ||
| }, | ||
| }; | ||
| const result = addOtelTraceContextToEvent(event, {}); | ||
| expect(result.contexts?.trace).toMatchObject({ | ||
| op: "http.server", | ||
| description: "GET /things", | ||
| trace_id: span.spanContext().traceId, | ||
| span_id: span.spanContext().spanId, | ||
| }); | ||
| expect(result.contexts?.runtime).toEqual({ name: "node" }); | ||
| span.end(); | ||
| }); | ||
| }); | ||
|
|
||
| it("returns the event unchanged if reading the OTel context throws", () => { | ||
| const throwingAccessor = () => { | ||
| throw new Error("otel api blew up"); | ||
| }; | ||
| const event = { message: "boom" }; | ||
| const result = addOtelTraceContextToEvent(event, {}, throwingAccessor); | ||
| expect(result).toBe(event); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.