Skip to content

WIP feat(opentelemetry): Add SentryTraceProvider#21181

Closed
andreiborza wants to merge 9 commits into
developfrom
ab/sentry-tracer-provider
Closed

WIP feat(opentelemetry): Add SentryTraceProvider#21181
andreiborza wants to merge 9 commits into
developfrom
ab/sentry-tracer-provider

Conversation

@andreiborza

Copy link
Copy Markdown
Member

WIP

Comment thread packages/opentelemetry/src/sentryTraceProvider.ts
@github-actions

github-actions Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.45 kB - -
@sentry/browser - with treeshaking flags 25.88 kB - -
@sentry/browser (incl. Tracing) 45.93 kB -0.03% -10 B 🔽
@sentry/browser (incl. Tracing + Span Streaming) 47.69 kB -0.02% -8 B 🔽
@sentry/browser (incl. Tracing, Profiling) 50.72 kB -0.02% -9 B 🔽
@sentry/browser (incl. Tracing, Replay) 85.13 kB -0.02% -10 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 74.72 kB -0.01% -7 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas) 89.82 kB -0.02% -10 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback) 102.48 kB -0.01% -6 B 🔽
@sentry/browser (incl. Feedback) 44.62 kB - -
@sentry/browser (incl. sendFeedback) 32.25 kB - -
@sentry/browser (incl. FeedbackAsync) 37.38 kB - -
@sentry/browser (incl. Metrics) 28.52 kB - -
@sentry/browser (incl. Logs) 28.76 kB - -
@sentry/browser (incl. Metrics & Logs) 29.45 kB - -
@sentry/react 29.25 kB - -
@sentry/react (incl. Tracing) 48.23 kB -0.02% -8 B 🔽
@sentry/vue 32.6 kB -0.03% -7 B 🔽
@sentry/vue (incl. Tracing) 47.8 kB -0.02% -8 B 🔽
@sentry/svelte 27.48 kB - -
CDN Bundle 29.84 kB - -
CDN Bundle (incl. Tracing) 47.85 kB -0.02% -6 B 🔽
CDN Bundle (incl. Logs, Metrics) 31.39 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.19 kB -0.02% -6 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) 70.7 kB - -
CDN Bundle (incl. Tracing, Replay) 85.2 kB -0.01% -5 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.47 kB -0.01% -8 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) 91.05 kB -0.01% -4 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.3 kB -0.01% -7 B 🔽
CDN Bundle - uncompressed 88.8 kB - -
CDN Bundle (incl. Tracing) - uncompressed 144.85 kB +0.02% +16 B 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 93.5 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 148.83 kB +0.02% +16 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 218.33 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 263.72 kB +0.01% +16 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 267.68 kB +0.01% +16 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 277.42 kB +0.01% +16 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 281.37 kB +0.01% +16 B 🔺
@sentry/nextjs (client) 50.62 kB -0.01% -5 B 🔽
@sentry/sveltekit (client) 46.33 kB -0.01% -4 B 🔽
@sentry/core/server 75.88 kB +0.04% +25 B 🔺
@sentry/core/browser 63.02 kB +0.03% +13 B 🔺
@sentry/node-core 61.66 kB +0.06% +35 B 🔺
@sentry/node 124.86 kB +0.78% +962 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 70.05 kB - -
@sentry/node/light 50.55 kB -0.01% -2 B 🔽
@sentry/node - without tracing 75.01 kB +1.19% +877 B 🔺
@sentry/aws-serverless 86.17 kB +1.11% +941 B 🔺
@sentry/cloudflare (withSentry) - minified 172.94 kB +0.03% +49 B 🔺
@sentry/cloudflare (withSentry) 432.62 kB +0.04% +145 B 🔺

View base workflow run

@andreiborza andreiborza force-pushed the ab/sentry-tracer-provider branch from 0f2020a to 71e7c69 Compare May 27, 2026 05:27
Comment thread packages/core/src/tracing/sentrySpan.ts Outdated
*/
public recordException(_exception: unknown, _time?: number | undefined): void {
// noop
public recordException(exception: unknown, time?: SpanTimeInput | undefined): void {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can leave this as noop, we do not care about this and do not really support it, doing nothing with events etc.

@andreiborza andreiborza force-pushed the ab/sentry-tracer-provider branch from 71e7c69 to a44215e Compare May 28, 2026 04:15
Comment thread packages/opentelemetry/src/sentryTraceProvider.ts
Comment thread packages/node/src/sdk/initOtel.ts
Comment thread packages/opentelemetry/src/sentryTraceProvider.ts
@andreiborza andreiborza force-pushed the ab/sentry-tracer-provider branch from cf369b6 to 749e7e7 Compare June 6, 2026 22:56
Comment thread packages/node/src/sdk/initOtel.ts
Comment thread packages/core/src/tracing/trace.ts Outdated
@andreiborza andreiborza force-pushed the ab/sentry-tracer-provider branch 6 times, most recently from 8ebaf6b to 2b70730 Compare June 8, 2026 10:54
Comment thread packages/core/src/utils/spanUtils.ts Outdated
@andreiborza andreiborza force-pushed the ab/sentry-tracer-provider branch from 959483b to 87ed1f0 Compare June 8, 2026 14:05
Comment thread packages/opentelemetry/src/sentryTraceProvider.ts
Comment thread packages/core/src/tracing/trace.ts Outdated
Comment thread packages/opentelemetry/src/sentryTraceProvider.ts
Comment thread packages/opentelemetry/src/sentryTraceProvider.ts Outdated
Comment thread packages/core/src/tracing/idleSpan.ts Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 5917bab. Configure here.

Comment thread packages/node/src/sdk/initOtel.ts
@andreiborza andreiborza force-pushed the ab/sentry-tracer-provider branch 3 times, most recently from d265e00 to 34725df Compare June 17, 2026 19:06
andreiborza and others added 2 commits June 19, 2026 14:30
`SentrySpan.updateName()` stamps `sentry.source: 'custom'` unconditionally again (as on develop), except for spans branded via `markSpanForOtelSourceInference()`. SentryTraceProvider sets that brand on the spans it creates, so instrumentations renaming them don't pin 'custom' and `applyOtelSpanData` can infer the correct source (e.g. 'route', 'task') at span end — mirroring OTel SDK spans, which carry no Sentry source concept.

This replaces the earlier "only set custom when a source already exists" heuristic, which also unintentionally stopped stamping 'custom' on user-created child spans that start without a source. Branding scopes the carve-out to exactly the provider-created spans. Standalone this commit is develop-equivalent (nothing sets the brand yet); the follow-up SentryTraceProvider PR flips it on.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add a minimal OpenTelemetry `TracerProvider` that creates native Sentry spans
instead of bridging through the full OTel SDK. Includes the `SentryTracer`,
`applyOtelSpanData` OTel-to-Sentry inference, sampling-decision propagation via
the span context, and supporting changes to the async context strategy,
setup checks, and DSC/root-span-name enhancement. Exposed for wiring via
`_INTERNAL_startInactiveSpan` in core. Gated behind the consumer SDK and has no
effect until enabled there.

Co-Authored-By: Claude <noreply@anthropic.com>
andreiborza and others added 2 commits June 19, 2026 14:35
Add the `useSentryTraceProvider` experimental option and the Node setup path
that registers the `SentryTraceProvider` (and its async context strategy)
instead of the full OpenTelemetry SDK tracer provider when enabled. Skips the
`SentrySpanProcessor`/`SentrySampler` setup that the provider replaces, guards
the HTTP server-spans integration for the missing async-local-storage lookup,
and finalizes span data on `spanEnd`. Defaults to off.

Co-Authored-By: Claude <noreply@anthropic.com>
@andreiborza andreiborza force-pushed the ab/sentry-tracer-provider branch from 916e70a to a6caf6a Compare June 19, 2026 12:48

@isaacs isaacs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments, mostly in the nitpick/bikeshed category, but a few that might be worth at least verifying aren't a concern before landing.

const LEGACY_HTTP_RESPONSE_STATUS_CODE_ATTRIBUTE = 'http.status_code';
const RPC_GRPC_STATUS_CODE_ATTRIBUTE = 'rpc.grpc.status_code';

const VALID_SPAN_STATUS_MESSAGES = new Set([

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be somewhat minor, but it seems like this is duplicated with the same values in packages/opentelemetry/src/utils/mapStatus.ts. Would be nice to have a single source of truth. (Though, I suppose they're not likely to change, so maybe a non-issue.)

/**
* A minimal OpenTelemetry TracerProvider which creates native Sentry spans.
*/
export class SentryTraceProvider implements TracerProvider {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit, but SentryTraceProvider vs otel's naming TracerProvider, could cause confusion? Autocomplete mostly protects us, and I imagine it's unlikely that anyone who isn't writing an SDK will be using this, but could be less confusing if named SentryTracerProvider.

Ie, it seems like what it's providing are SentryTracer objects, not "traces", so it's a "tracer provider", not a "trace provider". (This is extremely bikesheddy, I do realize.)

'http.status_text': statusMessage?.toUpperCase(),
};

const rpcMetadata = getRPCMetadata(context.active());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this mean that if someone changes the RPCMetadata between the request and response, it won't be picked up? I'm imagining if a web framework updates the route at some point, or something like that.

}

/** Apply OTel semantic inference to a Sentry span. */
export function applyOtelSpanData(span: Span, options: { finalizeStatus?: boolean } = {}): void {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this method gets called twice per span, once at the span start, and then again on spanEnd. Since it does a full spanToJSON, I worry that the serialization overhead could get expensive.

Is it possible to either defer the whole thing until the span ends, or just do the necessary finalization at the end without computing all the data all over again? Presumably it won't often change, right?

/** @inheritdoc */
public getTracer(name: string, version?: string, options?: TracerOptions): Tracer {
const key = JSON.stringify([name, version, options]);
const cachedTracer = this._tracers.get(key);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like SentryTracer is a just stateless "bag o' methods" object, so any tracer is equivalent to any other. Are we using them as keys in a map or something, that would require that they be unique per name/version/options? If not, maybe we could skip the cache, and just have a single instance?

Possible this will be needed in the future use case, and I'm just not seeing it because it's not in this PR, if so, disregard. But in that case, we might want to consider making this an LRU or setting some upper bound, just for safety's sake, so it doesn't blow up and become a memory leak.

@@ -0,0 +1,356 @@
/* eslint-disable max-lines */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, also, if those repeated consts (VALID_SPAN_STATUS_MESSAGES et al) are moved out into a shared location, probably don't need this override ;)

@andreiborza

Copy link
Copy Markdown
Member Author

This PR is superseded by #21666.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants