-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
ref(cloudflare)!: Use shared SentryTracerProvider for OpenTelemetry interop
#23300
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a04ef19
ref(cloudflare)!: Use shared `SentryTracerProvider` for OpenTelemetry…
JPeer264 af9a86c
fixup! ref(cloudflare)!: Use shared `SentryTracerProvider` for OpenTe…
JPeer264 1377b41
fix(opentelemetry): Scope startActiveSpan without an OTel context man…
JPeer264 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
41 changes: 41 additions & 0 deletions
41
...ckages/cloudflare-integration-tests/suites/tracing/opentelemetry-tracer/disabled/index.ts
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,41 @@ | ||
| import { SpanKind, trace } from '@opentelemetry/api'; | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| } | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| traceLifecycle: 'static', | ||
| // Deliberately left unset — the global tracer provider stays OTel's noop, so the spans below | ||
| // must not reach Sentry. | ||
| }), | ||
| { | ||
| async fetch() { | ||
| const tracer = trace.getTracer('integration-test-tracer'); | ||
|
|
||
| const inactive = tracer.startSpan('otel inactive', { attributes: { 'test.attribute': 'inactive' } }); | ||
| inactive.end(); | ||
|
|
||
| await tracer.startActiveSpan( | ||
| 'otel parent', | ||
| { kind: SpanKind.CLIENT, attributes: { 'test.attribute': 'parent' } }, | ||
| async otelParent => { | ||
| await Sentry.startSpan({ name: 'sentry child' }, async () => { | ||
| const otelGrandchild = tracer.startSpan('otel grandchild', { | ||
| attributes: { 'test.attribute': 'grandchild' }, | ||
| }); | ||
| otelGrandchild.end(); | ||
| }); | ||
|
|
||
| otelParent.end(); | ||
| }, | ||
| ); | ||
|
|
||
| return new Response('ok'); | ||
| }, | ||
| } satisfies ExportedHandler<Env>, | ||
| ); |
36 changes: 36 additions & 0 deletions
36
...ackages/cloudflare-integration-tests/suites/tracing/opentelemetry-tracer/disabled/test.ts
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,36 @@ | ||
| import { SENTRY_ORIGIN } from '@sentry/conventions/attributes'; | ||
| import type { Event } from '@sentry/core'; | ||
| import { expect, it } from 'vitest'; | ||
| import { SHORT_UUID_MATCHER } from '../../../../expect'; | ||
| import { createRunner } from '../../../../runner'; | ||
|
|
||
| it('drops spans emitted through @opentelemetry/api when `enableOpenTelemetrySetup` is not enabled', async ({ | ||
| signal, | ||
| }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as Event; | ||
|
|
||
| expect(transactionEvent.transaction).toBe('GET /'); | ||
|
|
||
| // Only the Sentry span survives; it re-parents onto the request span because the noop OTel | ||
| // span it was nested under never became a real parent. | ||
| expect(transactionEvent.spans).toEqual([ | ||
| { | ||
| data: { [SENTRY_ORIGIN]: 'manual' }, | ||
| description: 'sentry child', | ||
| parent_span_id: transactionEvent.contexts?.trace?.span_id, | ||
| span_id: SHORT_UUID_MATCHER, | ||
| start_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| timestamp: expect.any(Number), | ||
| trace_id: transactionEvent.contexts?.trace?.trace_id, | ||
| origin: 'manual', | ||
| }, | ||
| ]); | ||
| }) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/'); | ||
| await runner.completed(); | ||
| }); |
6 changes: 6 additions & 0 deletions
6
.../cloudflare-integration-tests/suites/tracing/opentelemetry-tracer/disabled/wrangler.jsonc
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 @@ | ||
| { | ||
| "name": "cloudflare-opentelemetry-tracer-disabled", | ||
| "main": "index.ts", | ||
| "compatibility_date": "2025-06-17", | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| } |
45 changes: 45 additions & 0 deletions
45
...ackages/cloudflare-integration-tests/suites/tracing/opentelemetry-tracer/enabled/index.ts
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,45 @@ | ||
| import { SpanKind, trace } from '@opentelemetry/api'; | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| } | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| traceLifecycle: 'static', | ||
| enableOpenTelemetrySetup: true, | ||
| }), | ||
| { | ||
| async fetch() { | ||
| const tracer = trace.getTracer('integration-test-tracer'); | ||
|
|
||
| const inactive = tracer.startSpan('otel inactive', { attributes: { 'test.attribute': 'inactive' } }); | ||
| inactive.end(); | ||
|
|
||
| await tracer.startActiveSpan( | ||
| 'otel parent', | ||
| { kind: SpanKind.CLIENT, attributes: { 'test.attribute': 'parent' } }, | ||
| async otelParent => { | ||
| await Sentry.startSpan({ name: 'sentry child' }, async () => { | ||
| const otelGrandchild = tracer.startSpan('otel grandchild', { | ||
| attributes: { 'test.attribute': 'grandchild' }, | ||
| }); | ||
| otelGrandchild.end(); | ||
| }); | ||
|
|
||
| otelParent.end(); | ||
| }, | ||
| ); | ||
|
|
||
| // Neither span may attach to the finished `otel parent` once `startActiveSpan` has returned. | ||
| const otelAfter = tracer.startSpan('otel after active', { attributes: { 'test.attribute': 'after' } }); | ||
| otelAfter.end(); | ||
| await Sentry.startSpan({ name: 'sentry after active' }, async () => {}); | ||
|
|
||
| return new Response('ok'); | ||
| }, | ||
| } satisfies ExportedHandler<Env>, | ||
| ); |
102 changes: 102 additions & 0 deletions
102
...packages/cloudflare-integration-tests/suites/tracing/opentelemetry-tracer/enabled/test.ts
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,102 @@ | ||
| import { SENTRY_KIND, SENTRY_ORIGIN } from '@sentry/conventions/attributes'; | ||
| import type { Event } from '@sentry/core'; | ||
| import { expect, it } from 'vitest'; | ||
| import { SHORT_UUID_MATCHER } from '../../../../expect'; | ||
| import { createRunner } from '../../../../runner'; | ||
|
|
||
| it('captures spans emitted through @opentelemetry/api and nests them with Sentry spans', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const event = envelope[1]?.[0]?.[1] as Event; | ||
| expect(event.transaction).toBe('GET /'); | ||
|
|
||
| const requestSpanId = event.contexts?.trace?.span_id; | ||
| const traceId = event.contexts?.trace?.trace_id; | ||
|
|
||
| // Cloudflare installs no OTel context manager, so `context.active()` never carries a span. | ||
| // Neither tracer API passes an explicit context here, so both fall back to the Sentry active | ||
| // span — the incoming request span — and everything stays in the request transaction. | ||
| const otelParentSpanId = event.spans?.[1]?.span_id; | ||
| const sentryChildSpanId = event.spans?.[2]?.span_id; | ||
|
|
||
| // Spans are ordered by start time. | ||
| expect(event.spans).toEqual([ | ||
| { | ||
| data: { [SENTRY_ORIGIN]: 'manual', 'test.attribute': 'inactive' }, | ||
| description: 'otel inactive', | ||
| parent_span_id: requestSpanId, | ||
| span_id: SHORT_UUID_MATCHER, | ||
| start_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| timestamp: expect.any(Number), | ||
| trace_id: traceId, | ||
| origin: 'manual', | ||
| }, | ||
| { | ||
| data: { [SENTRY_ORIGIN]: 'manual', [SENTRY_KIND]: 'client', 'test.attribute': 'parent' }, | ||
| description: 'otel parent', | ||
| parent_span_id: requestSpanId, | ||
| span_id: SHORT_UUID_MATCHER, | ||
| start_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| timestamp: expect.any(Number), | ||
| trace_id: traceId, | ||
| origin: 'manual', | ||
| }, | ||
| // Below the OTel parent the two APIs interleave correctly: the tracer publishes its active | ||
| // span on the Sentry scope, so the Sentry span picks it up as parent and the next OTel span | ||
| // picks up the Sentry one in turn. | ||
| { | ||
| data: { [SENTRY_ORIGIN]: 'manual' }, | ||
| description: 'sentry child', | ||
| parent_span_id: otelParentSpanId, | ||
| span_id: SHORT_UUID_MATCHER, | ||
| start_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| timestamp: expect.any(Number), | ||
| trace_id: traceId, | ||
| origin: 'manual', | ||
| }, | ||
| { | ||
| data: { [SENTRY_ORIGIN]: 'manual', 'test.attribute': 'grandchild' }, | ||
| description: 'otel grandchild', | ||
| parent_span_id: sentryChildSpanId, | ||
| span_id: SHORT_UUID_MATCHER, | ||
| start_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| timestamp: expect.any(Number), | ||
| trace_id: traceId, | ||
| origin: 'manual', | ||
| }, | ||
| // Without an OTel context manager `context.with` cannot restore anything, so the tracer has to | ||
| // fork the scope itself. Otherwise the finished `otel parent` would stay active and both of | ||
| // these would hang off it instead of the request span. | ||
| { | ||
| data: { [SENTRY_ORIGIN]: 'manual', 'test.attribute': 'after' }, | ||
| description: 'otel after active', | ||
| parent_span_id: requestSpanId, | ||
| span_id: SHORT_UUID_MATCHER, | ||
| start_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| timestamp: expect.any(Number), | ||
| trace_id: traceId, | ||
| origin: 'manual', | ||
| }, | ||
| { | ||
| data: { [SENTRY_ORIGIN]: 'manual' }, | ||
| description: 'sentry after active', | ||
| parent_span_id: requestSpanId, | ||
| span_id: SHORT_UUID_MATCHER, | ||
| start_timestamp: expect.any(Number), | ||
| status: 'ok', | ||
| timestamp: expect.any(Number), | ||
| trace_id: traceId, | ||
| origin: 'manual', | ||
| }, | ||
| ]); | ||
| }) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/'); | ||
| await runner.completed(); | ||
| }); |
6 changes: 6 additions & 0 deletions
6
...s/cloudflare-integration-tests/suites/tracing/opentelemetry-tracer/enabled/wrangler.jsonc
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 @@ | ||
| { | ||
| "name": "cloudflare-opentelemetry-tracer", | ||
| "main": "index.ts", | ||
| "compatibility_date": "2025-06-17", | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| } |
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
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 |
|---|---|---|
| @@ -1,80 +1,10 @@ | ||
| import type { Context, Span, SpanOptions, Tracer, TracerProvider } from '@opentelemetry/api'; | ||
| import { trace } from '@opentelemetry/api'; | ||
| import { startInactiveSpan, startSpanManual } from '@sentry/core'; | ||
| import { SentryTracerProvider } from '@sentry/opentelemetry'; | ||
|
|
||
| /** | ||
| * Set up a mock OTEL tracer to allow inter-op with OpenTelemetry emitted spans. | ||
| * This is not perfect but handles easy/common use cases. | ||
| */ | ||
| export function setupOpenTelemetryTracer(): void { | ||
| trace.setGlobalTracerProvider(new SentryCloudflareTraceProvider()); | ||
| } | ||
|
|
||
| class SentryCloudflareTraceProvider implements TracerProvider { | ||
| private readonly _tracers: Map<string, Tracer> = new Map(); | ||
|
|
||
| public getTracer(name: string, version?: string, options?: { schemaUrl?: string }): Tracer { | ||
| const key = `${name}@${version || ''}:${options?.schemaUrl || ''}`; | ||
| if (!this._tracers.has(key)) { | ||
| this._tracers.set(key, new SentryCloudflareTracer()); | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
| return this._tracers.get(key)!; | ||
| } | ||
| } | ||
|
|
||
| class SentryCloudflareTracer implements Tracer { | ||
| public startSpan(name: string, options?: SpanOptions): Span { | ||
| return startInactiveSpan({ | ||
| ...options, | ||
| name, | ||
| attributes: { | ||
| ...options?.attributes, | ||
| 'sentry.cloudflare_tracer': true, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * NOTE: This does not handle `context` being passed in. It will always put spans on the current scope. | ||
| */ | ||
| public startActiveSpan<F extends (span: Span) => unknown>(name: string, fn: F): ReturnType<F>; | ||
| public startActiveSpan<F extends (span: Span) => unknown>(name: string, options: SpanOptions, fn: F): ReturnType<F>; | ||
| public startActiveSpan<F extends (span: Span) => unknown>( | ||
| name: string, | ||
| options: SpanOptions, | ||
| context: Context, | ||
| fn: F, | ||
| ): ReturnType<F>; | ||
| public startActiveSpan<F extends (span: Span) => unknown>( | ||
| name: string, | ||
| options: unknown, | ||
| context?: unknown, | ||
| fn?: F, | ||
| ): ReturnType<F> { | ||
| const opts = (typeof options === 'object' && options !== null ? options : {}) as SpanOptions; | ||
|
|
||
| const spanOpts = { | ||
| ...opts, | ||
| name, | ||
| attributes: { | ||
| ...opts.attributes, | ||
| 'sentry.cloudflare_tracer': true, | ||
| }, | ||
| }; | ||
|
|
||
| const callback = ( | ||
| typeof options === 'function' | ||
| ? options | ||
| : typeof context === 'function' | ||
| ? context | ||
| : typeof fn === 'function' | ||
| ? fn | ||
| : () => {} | ||
| ) as F; | ||
|
|
||
| // In OTEL the semantic matches `startSpanManual` because spans are not auto-ended | ||
| return startSpanManual(spanOpts, callback) as ReturnType<F>; | ||
| } | ||
| trace.setGlobalTracerProvider(new SentryTracerProvider()); | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The
wrapRequestHandlerno longer acceptsenableOpenTelemetrySetup, but the SvelteKit integration still passes it. This will silently disable SvelteKit's OpenTelemetry tracing on Cloudflare.Severity: MEDIUM
Suggested Fix
Update the SvelteKit integration to handle this change. Either switch to using the main
initfunction fromsdk.tswhich still honorsenableOpenTelemetrySetup, or remove theenableOpenTelemetrySetup: truesetting from the options passed towrapRequestHandler.Prompt for AI Agent
Also affects:
packages/cloudflare/src/baseSdk.ts:18~23Did we get this right? 👍 / 👎 to inform future reviews.