-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add OpenTelemetry-specific
getTraceData
version
- Loading branch information
Showing
5 changed files
with
58 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,40 @@ | ||
import * as api from '@opentelemetry/api'; | ||
import type { Span } from '@opentelemetry/api'; | ||
import type { TraceData } from '@sentry/core'; | ||
import { getCurrentScope } from '@sentry/core'; | ||
import { getPropagationContextFromSpan } from '../propagator'; | ||
import { generateSpanContextForPropagationContext } from './generateSpanContextForPropagationContext'; | ||
|
||
/** | ||
* Otel-specific implementation of `getTraceData`. | ||
* @see `@sentry/core` version of `getTraceData` for more information | ||
*/ | ||
export function getTraceData(span?: Span): TraceData { | ||
const ctx = api.context.active(); | ||
const spanToUse = span || api.trace.getSpan(ctx); | ||
|
||
// This should never happen, given we always create an ambient non-recording span if there's no active span. | ||
if (!spanToUse) { | ||
return {}; | ||
} | ||
const headersObject: Record<string, string> = {}; | ||
|
||
const propagationContext = spanToUse | ||
? getPropagationContextFromSpan(spanToUse) | ||
: getCurrentScope().getPropagationContext(); | ||
|
||
const spanContext = generateSpanContextForPropagationContext(propagationContext); | ||
|
||
const context = api.trace.setSpanContext(ctx, spanContext); | ||
|
||
api.propagation.inject(context, headersObject); | ||
|
||
if (!headersObject['sentry-trace']) { | ||
return {}; | ||
} | ||
|
||
return { | ||
'sentry-trace': headersObject['sentry-trace'], | ||
baggage: headersObject['baggage'], | ||
}; | ||
} |