-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(opentelemetry): Add SentryTracerProvider #21666
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
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d4f3f7f
feat(opentelemetry): Add SentryTraceProvider
andreiborza 65112df
Explain segment-root source guard
andreiborza 55f1d9a
Use updateName instead of writing _name directly
andreiborza d8e7d9b
Drop redudandant isolation scope pinning in startActiveSpan
andreiborza 6bfad57
Fix naming differences
andreiborza a66b30f
Reinstate isolation scope pinning in startActiveSpan
andreiborza fddafbf
Start a new trace for parentless root spans in SentryTracerProvider
andreiborza c9d7283
Don't freeze an incomplete DSC when continuing a baggageless remote t…
andreiborza 71ecd8d
Pass normalizedRequest to the sampling context for root spans
andreiborza 443ae5a
Strip leading ? and # from inferred http.query and http.fragment
andreiborza 1d6bec0
Propagate the sampling decision for native unsampled spans
andreiborza eab696a
Only apply the native-span sampling fallback to SentrySpans
andreiborza 39d9d06
Respect an explicitly set source on spans marked for OTel source infe…
andreiborza 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
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
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
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,120 @@ | ||
| import { SpanKind } from '@opentelemetry/api'; | ||
| import { HTTP_RESPONSE_STATUS_CODE, HTTP_STATUS_CODE } from '@sentry/conventions/attributes'; | ||
| import { | ||
| addNonEnumerableProperty, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_OP, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, | ||
| spanShouldInferOtelSource, | ||
| spanSourceWasExplicitlySet, | ||
| spanToJSON, | ||
| SPAN_STATUS_ERROR, | ||
| SPAN_STATUS_OK, | ||
| } from '@sentry/core'; | ||
| import type { Span, SpanAttributes } from '@sentry/core'; | ||
| import { inferStatusFromAttributes, isStatusErrorMessageValid } from './utils/mapStatus'; | ||
| import { inferSpanData } from './utils/parseSpanDescription'; | ||
|
|
||
| type SentrySpanWithOtelKind = Span & { kind?: SpanKind }; | ||
|
|
||
| /** | ||
| * Backfill a native Sentry span with the data the OpenTelemetry SDK pipeline would otherwise derive | ||
| * from OTel semantic attributes: `sentry.op`, `sentry.source`, the span name, `otel.kind`, and status. | ||
| * | ||
| * On the OTel SDK provider this happens in the `SentrySpanProcessor`/`SentrySpanExporter` while | ||
| * converting `ReadableSpan`s to Sentry payloads (via `parseSpanDescription` + `mapStatus`). | ||
| * `SentryTracerProvider` creates native Sentry spans directly and never goes through that pipeline, | ||
| * so the same inference has to run here instead — once at span start, and again at span end | ||
| * (`finalizeStatus`, once attributes like `http.route` and the status code are available). | ||
| */ | ||
| export function applyOtelSpanData(span: Span, options: { finalizeStatus?: boolean } = {}): void { | ||
| const spanJSON = spanToJSON(span); | ||
| const attributes = spanJSON.data; | ||
| const kind = (span as SentrySpanWithOtelKind).kind ?? SpanKind.INTERNAL; | ||
| const mayInferSource = spanShouldInferOtelSource(span); | ||
| const hasCustomSpanName = attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME] !== undefined; | ||
| // We may only infer the source/name when the span is OTel-branded and user code hasn't already | ||
| // chosen them: either via `updateSpanName` (which sets `sentry.custom_span_name`) or by explicitly | ||
| // setting `sentry.source`. Without the explicit-source check we couldn't tell a user-set `custom` | ||
| // apart from the default `custom` stamped on every root span at span start, and would override it. | ||
| const canInferSource = mayInferSource && !hasCustomSpanName && !spanSourceWasExplicitlySet(span); | ||
| const attributesForInference = | ||
| canInferSource && attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === 'custom' | ||
| ? { ...attributes, [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: undefined } | ||
| : attributes; | ||
| const inferred = inferSpanData(spanJSON.description || '<unknown>', attributesForInference, kind); | ||
|
|
||
| if (kind !== SpanKind.INTERNAL && attributes['otel.kind'] === undefined) { | ||
| span.setAttribute('otel.kind', SpanKind[kind]); | ||
| } | ||
|
|
||
| if (inferred.op && attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] === undefined) { | ||
| span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, inferred.op); | ||
| } | ||
|
|
||
| // Don't apply 'url' source at creation time, only at span end (finalizeStatus). | ||
| // At creation, http.route may not be set yet, so inference falls back to 'url'. | ||
| // Keeping the default 'custom' source from _startRootSpan allows | ||
| // enhanceDscWithOpenTelemetryRootSpanName to include the transaction name in | ||
| // the DSC. At span end, http.route is typically available and inference returns | ||
| // 'route' instead. If it's still 'url', it's applied then. | ||
| // We also only set `source` on segment roots (spans that become transactions): | ||
| // those with no parent, plus SERVER spans, which are the segment root even when | ||
| // continuing a distributed trace (where they carry a remote `parent_span_id`). | ||
| const shouldApplyInferredSource = | ||
| inferred.source !== undefined && | ||
| inferred.source !== 'custom' && | ||
| (options.finalizeStatus || inferred.source !== 'url') && | ||
| (spanJSON.parent_span_id === undefined || kind === SpanKind.SERVER); | ||
|
|
||
| if (shouldApplyInferredSource && (attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === undefined || canInferSource)) { | ||
| span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, inferred.source); | ||
| } | ||
|
|
||
| if (inferred.data) { | ||
| Object.entries(inferred.data).forEach(([key, value]) => { | ||
| if (value !== undefined && attributes[key] === undefined) { | ||
| span.setAttribute(key, value); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| if (options.finalizeStatus) { | ||
| applyOtelCompatibilityAttributes(span, attributes); | ||
| applyOtelSpanStatus(span, attributes, spanJSON.status); | ||
| } | ||
|
|
||
| if ( | ||
| inferred.description !== spanJSON.description && | ||
| (attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] !== 'custom' || canInferSource) | ||
| ) { | ||
| span.updateName(inferred.description); | ||
| } | ||
|
andreiborza marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** Stash the OTel span kind on a Sentry span so {@link applyOtelSpanData} can read it. */ | ||
| export function applyOtelSpanKind(span: Span, kind: SpanKind | undefined): void { | ||
| addNonEnumerableProperty(span as SentrySpanWithOtelKind, 'kind', kind ?? SpanKind.INTERNAL); | ||
| } | ||
|
|
||
| function applyOtelSpanStatus(span: Span, attributes: SpanAttributes, status: string | undefined): void { | ||
| if (status === undefined) { | ||
| span.setStatus(inferStatusFromAttributes(attributes) || { code: SPAN_STATUS_OK }); | ||
| return; | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (status !== 'ok' && !isStatusErrorMessageValid(status)) { | ||
| span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' }); | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
sentry[bot] marked this conversation as resolved.
|
||
|
|
||
| function applyOtelCompatibilityAttributes(span: Span, attributes: SpanAttributes): void { | ||
| // `http.status_code` is the deprecated legacy attribute, read for backward compatibility. | ||
| // eslint-disable-next-line typescript/no-deprecated | ||
| const legacyHttpStatusCode = attributes[HTTP_STATUS_CODE]; | ||
|
|
||
| if (attributes[HTTP_RESPONSE_STATUS_CODE] === undefined && legacyHttpStatusCode !== undefined) { | ||
| span.setAttribute(HTTP_RESPONSE_STATUS_CODE, legacyHttpStatusCode); | ||
| attributes[HTTP_RESPONSE_STATUS_CODE] = legacyHttpStatusCode; | ||
| } | ||
| } | ||
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
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.
the comment is pretty good but does not really explain this part of the condition?
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.
Updated the comment to explain it in 40d8abc