-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(otel): Add propagator #6095
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d64e3a
feat(otel): Add propogator
AbhiPrasad 0787574
get it working
AbhiPrasad a1dfa06
set baggage on outgoing header
AbhiPrasad a3d87be
make SENTRY_SPAN_PROCESSOR_MAP public
AbhiPrasad 4253cff
add inject unit tests
AbhiPrasad d3383b7
refactor inject tests
AbhiPrasad cb2a100
add tests for extract
AbhiPrasad 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createContextKey } from '@opentelemetry/api'; | ||
|
||
export const SENTRY_TRACE_HEADER = 'sentry-trace'; | ||
|
||
export const SENTRY_BAGGAGE_HEADER = 'baggage'; | ||
|
||
export const SENTRY_DYNAMIC_SAMPLING_CONTEXT_KEY = createContextKey('SENTRY_DYNAMIC_SAMPLING_CONTEXT_KEY'); | ||
|
||
export const SENTRY_TRACE_PARENT_CONTEXT_KEY = createContextKey('SENTRY_TRACE_PARENT_CONTEXT_KEY'); |
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,3 +1,4 @@ | ||
import '@sentry/tracing'; | ||
|
||
export { SentrySpanProcessor } from './spanprocessor'; | ||
export { SentryPropogator } from './propogator'; | ||
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,94 @@ | ||
import { | ||
Context, | ||
isSpanContextValid, | ||
TextMapGetter, | ||
TextMapPropagator, | ||
TextMapSetter, | ||
trace, | ||
TraceFlags, | ||
} from '@opentelemetry/api'; | ||
import { isTracingSuppressed } from '@opentelemetry/core'; | ||
import { | ||
baggageHeaderToDynamicSamplingContext, | ||
dynamicSamplingContextToSentryBaggageHeader, | ||
extractTraceparentData, | ||
} from '@sentry/utils'; | ||
|
||
import { | ||
SENTRY_BAGGAGE_HEADER, | ||
SENTRY_DYNAMIC_SAMPLING_CONTEXT_KEY, | ||
SENTRY_TRACE_HEADER, | ||
SENTRY_TRACE_PARENT_CONTEXT_KEY, | ||
} from './constants'; | ||
import { SENTRY_SPAN_PROCESSOR_MAP } from './spanprocessor'; | ||
|
||
/** | ||
* Injects and extracts `sentry-trace` and `baggage` headers from carriers. | ||
*/ | ||
export class SentryPropogator implements TextMapPropagator { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public inject(context: Context, carrier: unknown, setter: TextMapSetter): void { | ||
const spanContext = trace.getSpanContext(context); | ||
if (!spanContext || !isSpanContextValid(spanContext) || isTracingSuppressed(context)) { | ||
return; | ||
} | ||
|
||
// TODO: if sentry span use `parentSpanId`. | ||
// Same `isSentryRequest` as is used in `SentrySpanProcessor`. | ||
// const spanId = isSentryRequest(spanContext) ? spanContext.parentSpanId : spanContext.spanId; | ||
|
||
const traceparent = `${spanContext.traceId}-${spanContext.spanId}-${ | ||
// eslint-disable-next-line no-bitwise | ||
spanContext.traceFlags & TraceFlags.SAMPLED ? 1 : 0 | ||
}`; | ||
setter.set(carrier, SENTRY_TRACE_HEADER, traceparent); | ||
|
||
const span = SENTRY_SPAN_PROCESSOR_MAP.get(spanContext.spanId); | ||
if (span && span.transaction) { | ||
const dynamicSamplingContext = span.transaction.getDynamicSamplingContext(); | ||
const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext); | ||
if (sentryBaggageHeader) { | ||
setter.set(carrier, SENTRY_BAGGAGE_HEADER, sentryBaggageHeader); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public extract(context: Context, carrier: unknown, getter: TextMapGetter): Context { | ||
let newContext = context; | ||
|
||
const maybeSentryTraceHeader: string | string[] | undefined = getter.get(carrier, SENTRY_TRACE_HEADER); | ||
if (maybeSentryTraceHeader) { | ||
const header = Array.isArray(maybeSentryTraceHeader) ? maybeSentryTraceHeader[0] : maybeSentryTraceHeader; | ||
const traceparentData = extractTraceparentData(header); | ||
newContext = newContext.setValue(SENTRY_TRACE_PARENT_CONTEXT_KEY, traceparentData); | ||
if (traceparentData) { | ||
const traceFlags = traceparentData.parentSampled ? TraceFlags.SAMPLED : TraceFlags.NONE; | ||
const spanContext = { | ||
traceId: traceparentData.traceId || '', | ||
spanId: traceparentData.parentSpanId || '', | ||
isRemote: true, | ||
traceFlags, | ||
}; | ||
newContext = trace.setSpanContext(newContext, spanContext); | ||
} | ||
} | ||
|
||
const maybeBaggageHeader = getter.get(carrier, SENTRY_BAGGAGE_HEADER); | ||
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(maybeBaggageHeader); | ||
newContext = newContext.setValue(SENTRY_DYNAMIC_SAMPLING_CONTEXT_KEY, dynamicSamplingContext); | ||
|
||
return newContext; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public fields(): string[] { | ||
return [SENTRY_TRACE_HEADER, SENTRY_BAGGAGE_HEADER]; | ||
} | ||
} |
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.
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.
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.
I think this should be
propagator
, notpropogator
? (both in the filename & class name)Uh oh!
There was an error while loading. Please reload this page.
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.
Yup, fixed this afterwards, see: #6109
I literally spelled it wrong everywhere 😭