Skip to content

Commit 58bb2ac

Browse files
committed
fix propagator!!
1 parent c47f0ac commit 58bb2ac

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

packages/opentelemetry/src/propagator.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Baggage, Context, Span, TextMapGetter, TextMapSetter } from '@opentelemetry/api';
2+
import { SpanKind } from '@opentelemetry/api';
23
import { INVALID_TRACEID } from '@opentelemetry/api';
34
import { context } from '@opentelemetry/api';
45
import { propagation, trace } from '@opentelemetry/api';
@@ -32,6 +33,7 @@ import { DEBUG_BUILD } from './debug-build';
3233
import { getScopesFromContext, setScopesOnContext } from './utils/contextData';
3334
import { generateSpanContextForPropagationContext } from './utils/generateSpanContextForPropagationContext';
3435
import { getSamplingDecision } from './utils/getSamplingDecision';
36+
import { getSpanKind } from './utils/getSpanKind';
3537
import { setIsSetup } from './utils/setupCheck';
3638

3739
/** Get the Sentry propagation context from a span context. */
@@ -86,9 +88,14 @@ export class SentryPropagator extends W3CBaggagePropagator {
8688

8789
const activeSpan = trace.getSpan(context);
8890
const url = activeSpan && getCurrentURL(activeSpan);
91+
// We only want to check the URL against tracePropagationTargets for outgoing request spans
92+
// In other cases, we always inject the trace data
93+
const shouldCheckUrl =
94+
activeSpan && (spanToJSON(activeSpan).op === 'http.client' || getSpanKind(activeSpan) === SpanKind.CLIENT);
8995

9096
const tracePropagationTargets = getClient()?.getOptions()?.tracePropagationTargets;
9197
if (
98+
shouldCheckUrl &&
9299
typeof url === 'string' &&
93100
tracePropagationTargets &&
94101
!this._shouldInjectTraceData(tracePropagationTargets, url)

packages/opentelemetry/src/utils/getTraceData.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import { getContextFromScope } from './contextData';
1111
export function getTraceData({ span }: { span?: Span } = {}): SerializedTraceData {
1212
const headersObject: Record<string, string> = {};
1313

14+
let ctx = api.context.active();
15+
1416
if (span) {
1517
const { scope } = getCapturedScopesOnSpan(span);
1618
// fall back to current context if for whatever reason we can't find the one of the span
17-
const ctx = (scope && getContextFromScope(scope)) || api.trace.setSpan(api.context.active(), span);
18-
19-
api.propagation.inject(ctx, headersObject);
20-
} else {
21-
api.propagation.inject(api.context.active(), headersObject);
19+
ctx = (scope && getContextFromScope(scope)) || api.trace.setSpan(api.context.active(), span);
2220
}
2321

22+
api.propagation.inject(ctx, headersObject);
23+
2424
if (!headersObject['sentry-trace']) {
2525
return {};
2626
}

packages/opentelemetry/test/propagator.test.ts

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
ROOT_CONTEXT,
3+
SpanKind,
34
TraceFlags,
45
context,
56
defaultTextMapGetter,
@@ -8,7 +9,8 @@ import {
89
trace,
910
} from '@opentelemetry/api';
1011
import { suppressTracing } from '@opentelemetry/core';
11-
import { getCurrentScope, withScope } from '@sentry/core';
12+
import { ATTR_URL_FULL, SEMATTRS_HTTP_URL } from '@opentelemetry/semantic-conventions';
13+
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, getCurrentScope, withScope } from '@sentry/core';
1214

1315
import { SENTRY_BAGGAGE_HEADER, SENTRY_SCOPES_CONTEXT_KEY, SENTRY_TRACE_HEADER } from '../src/constants';
1416
import { SentryPropagator } from '../src/propagator';
@@ -28,6 +30,7 @@ describe('SentryPropagator', () => {
2830
release: '1.0.0',
2931
enableTracing: true,
3032
dsn: 'https://abc@domain/123',
33+
tracePropagationTargets: ['/path2'],
3134
});
3235
});
3336

@@ -545,6 +548,111 @@ describe('SentryPropagator', () => {
545548
expect(carrier[SENTRY_TRACE_HEADER]).toBe(undefined);
546549
expect(carrier[SENTRY_BAGGAGE_HEADER]).toBe(undefined);
547550
});
551+
552+
it.each([
553+
[
554+
{
555+
attributes: { [ATTR_URL_FULL]: 'https://domain.com/path2' },
556+
kind: SpanKind.CLIENT,
557+
},
558+
true,
559+
],
560+
[
561+
{
562+
// eslint-disable-next-line deprecation/deprecation
563+
attributes: { [SEMATTRS_HTTP_URL]: 'https://domain.com/path2' },
564+
kind: SpanKind.CLIENT,
565+
},
566+
true,
567+
],
568+
[
569+
{
570+
attributes: { [ATTR_URL_FULL]: 'https://domain.com/path3' },
571+
kind: SpanKind.CLIENT,
572+
},
573+
false,
574+
],
575+
[
576+
{
577+
// eslint-disable-next-line deprecation/deprecation
578+
attributes: { [SEMATTRS_HTTP_URL]: 'https://domain.com/path3' },
579+
kind: SpanKind.CLIENT,
580+
},
581+
false,
582+
],
583+
[
584+
{
585+
attributes: { [ATTR_URL_FULL]: 'https://domain.com/path2' },
586+
},
587+
true,
588+
],
589+
[
590+
{
591+
// eslint-disable-next-line deprecation/deprecation
592+
attributes: { [SEMATTRS_HTTP_URL]: 'https://domain.com/path2' },
593+
},
594+
true,
595+
],
596+
[
597+
{
598+
attributes: { [ATTR_URL_FULL]: 'https://domain.com/path3' },
599+
},
600+
true,
601+
],
602+
[
603+
{
604+
// eslint-disable-next-line deprecation/deprecation
605+
attributes: { [SEMATTRS_HTTP_URL]: 'https://domain.com/path3' },
606+
},
607+
true,
608+
],
609+
[
610+
{
611+
attributes: { [ATTR_URL_FULL]: 'https://domain.com/path3' },
612+
kind: SpanKind.SERVER,
613+
},
614+
true,
615+
],
616+
[
617+
{
618+
// eslint-disable-next-line deprecation/deprecation
619+
attributes: { [SEMATTRS_HTTP_URL]: 'https://domain.com/path3' },
620+
kind: SpanKind.SERVER,
621+
},
622+
true,
623+
],
624+
[
625+
{
626+
attributes: {
627+
[ATTR_URL_FULL]: 'https://domain.com/path3',
628+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.client',
629+
},
630+
},
631+
false,
632+
],
633+
[
634+
{
635+
attributes: {
636+
// eslint-disable-next-line deprecation/deprecation
637+
[SEMATTRS_HTTP_URL]: 'https://domain.com/path3',
638+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.client',
639+
},
640+
},
641+
false,
642+
],
643+
])('for %p injection is %p', (spanContext, shouldInject) => {
644+
trace.getTracer('test').startActiveSpan('test', spanContext, span => {
645+
propagator.inject(context.active(), carrier, defaultTextMapSetter);
646+
647+
if (shouldInject) {
648+
expect(carrier[SENTRY_TRACE_HEADER]).toBe(`${span.spanContext().traceId}-${span.spanContext().spanId}-1`);
649+
expect(carrier[SENTRY_BAGGAGE_HEADER]).toMatch(/.+/);
650+
} else {
651+
expect(carrier[SENTRY_TRACE_HEADER]).toBe(undefined);
652+
expect(carrier[SENTRY_BAGGAGE_HEADER]).toBe(undefined);
653+
}
654+
});
655+
});
548656
});
549657

550658
describe('extract', () => {

0 commit comments

Comments
 (0)