From bacbf4efe2a9a3e0caa75bc655d101ad48f7f5e7 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 29 Jun 2023 19:06:19 -0400 Subject: [PATCH 1/9] ref(tracing): Add propagation getter to scope --- packages/core/src/scope.ts | 9 ++++++++- packages/hub/test/scope.test.ts | 16 ++++++++++++++++ packages/tracing/test/utils.test.ts | 3 +++ packages/types/src/scope.ts | 5 +++++ packages/utils/src/baggage.ts | 6 +++++- packages/utils/src/tracing.ts | 14 ++++++++------ packages/utils/test/baggage.test.ts | 1 + 7 files changed, 46 insertions(+), 8 deletions(-) diff --git a/packages/core/src/scope.ts b/packages/core/src/scope.ts index 8d964f033739..b72defc56016 100644 --- a/packages/core/src/scope.ts +++ b/packages/core/src/scope.ts @@ -533,13 +533,20 @@ export class Scope implements ScopeInterface { } /** - * @inheritdoc + * @inheritDoc */ public setPropagationContext(context: PropagationContext): this { this._propagationContext = context; return this; } + /** + * @inheritDoc + */ + public getPropagationContext(): PropagationContext { + return this._propagationContext; + } + /** * This will be called after {@link applyToEvent} is finished. */ diff --git a/packages/hub/test/scope.test.ts b/packages/hub/test/scope.test.ts index 3b09c02a9f84..982cba766a87 100644 --- a/packages/hub/test/scope.test.ts +++ b/packages/hub/test/scope.test.ts @@ -152,6 +152,22 @@ describe('Scope', () => { expect((scope as any)._sdkProcessingMetadata.dogs).toEqual('are great!'); }); + test('set and get propagation context', () => { + const scope = new Scope(); + const oldPropagationContext = scope.getPropagationContext(); + scope.setPropagationContext({ + traceId: '86f39e84263a4de99c326acab3bfe3bd', + spanId: '6e0c63257de34c92', + sampled: true, + }); + expect(scope.getPropagationContext()).not.toEqual(oldPropagationContext); + expect(scope.getPropagationContext()).toEqual({ + traceId: '86f39e84263a4de99c326acab3bfe3bd', + spanId: '6e0c63257de34c92', + sampled: true, + }); + }); + test('chaining', () => { const scope = new Scope(); scope.setLevel('fatal').setUser({ id: '1' }); diff --git a/packages/tracing/test/utils.test.ts b/packages/tracing/test/utils.test.ts index 00d90e873c9e..94374ac58b23 100644 --- a/packages/tracing/test/utils.test.ts +++ b/packages/tracing/test/utils.test.ts @@ -77,6 +77,9 @@ describe('extractTraceparentData', () => { }); test('invalid', () => { + // undefined + expect(extractTraceparentData(undefined)).toBeUndefined(); + // empty string expect(extractTraceparentData('')).toBeUndefined(); diff --git a/packages/types/src/scope.ts b/packages/types/src/scope.ts index 1351d2ce3161..4a315aa7adda 100644 --- a/packages/types/src/scope.ts +++ b/packages/types/src/scope.ts @@ -192,4 +192,9 @@ export interface Scope { * Add propagation context to the scope, used for distributed tracing */ setPropagationContext(context: PropagationContext): this; + + /** + * Get propagation context from the scope, used for distributed tracing + */ + getPropagationContext(): PropagationContext; } diff --git a/packages/utils/src/baggage.ts b/packages/utils/src/baggage.ts index 406ee3adc819..674e7f60f7ef 100644 --- a/packages/utils/src/baggage.ts +++ b/packages/utils/src/baggage.ts @@ -83,8 +83,12 @@ export function baggageHeaderToDynamicSamplingContext( */ export function dynamicSamplingContextToSentryBaggageHeader( // this also takes undefined for convenience and bundle size in other places - dynamicSamplingContext: Partial, + dynamicSamplingContext?: Partial, ): string | undefined { + if (!dynamicSamplingContext) { + return undefined; + } + // Prefix all DSC keys with "sentry-" and put them into a new object const sentryPrefixedDSC = Object.entries(dynamicSamplingContext).reduce>( (acc, [dscKey, dscValue]) => { diff --git a/packages/utils/src/tracing.ts b/packages/utils/src/tracing.ts index 445c271a0ee5..f93f2ef8d477 100644 --- a/packages/utils/src/tracing.ts +++ b/packages/utils/src/tracing.ts @@ -18,11 +18,13 @@ export const TRACEPARENT_REGEXP = new RegExp( * * @returns Object containing data from the header, or undefined if traceparent string is malformed */ -export function extractTraceparentData(traceparent: string): TraceparentData | undefined { - const matches = traceparent.match(TRACEPARENT_REGEXP); +export function extractTraceparentData(traceparent?: string): TraceparentData | undefined { + if (!traceparent) { + return undefined; + } - if (!traceparent || !matches) { - // empty string or no matches is invalid traceparent data + const matches = traceparent.match(TRACEPARENT_REGEXP); + if (!matches) { return undefined; } @@ -44,8 +46,8 @@ export function extractTraceparentData(traceparent: string): TraceparentData | u * Create tracing context from incoming headers. */ export function tracingContextFromHeaders( - sentryTrace: Parameters[0] = '', - baggage: Parameters[0] = '', + sentryTrace: Parameters[0], + baggage: Parameters[0], ): { traceparentData: ReturnType; dynamicSamplingContext: ReturnType; diff --git a/packages/utils/test/baggage.test.ts b/packages/utils/test/baggage.test.ts index 8f848badf9de..539a34e44d9c 100644 --- a/packages/utils/test/baggage.test.ts +++ b/packages/utils/test/baggage.test.ts @@ -28,6 +28,7 @@ test.each([ }); test.each([ + [undefined, undefined], [{}, undefined], [{ release: 'abcdf' }, 'sentry-release=abcdf'], [{ release: 'abcdf', environment: '1234' }, 'sentry-release=abcdf,sentry-environment=1234'], From 26c44f7514fb7ebe40a95d7d4eb50a63500b3e8e Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 29 Jun 2023 21:58:39 -0400 Subject: [PATCH 2/9] create a helper to make sentry-trace --- packages/core/src/tracing/span.ts | 8 ++------ packages/utils/src/tracing.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/core/src/tracing/span.ts b/packages/core/src/tracing/span.ts index 59a8ed11d68a..617dc7eaa52c 100644 --- a/packages/core/src/tracing/span.ts +++ b/packages/core/src/tracing/span.ts @@ -7,7 +7,7 @@ import type { TraceContext, Transaction, } from '@sentry/types'; -import { dropUndefinedKeys, logger, timestampInSeconds, uuid4 } from '@sentry/utils'; +import { dropUndefinedKeys, generateSentryTraceHeader, logger, timestampInSeconds, uuid4 } from '@sentry/utils'; /** * Keeps track of finished spans for a given transaction @@ -265,11 +265,7 @@ export class Span implements SpanInterface { * @inheritDoc */ public toTraceparent(): string { - let sampledString = ''; - if (this.sampled !== undefined) { - sampledString = this.sampled ? '-1' : '-0'; - } - return `${this.traceId}-${this.spanId}${sampledString}`; + return generateSentryTraceHeader(this.traceId, this.spanId, this.sampled); } /** diff --git a/packages/utils/src/tracing.ts b/packages/utils/src/tracing.ts index f93f2ef8d477..f879b856f9f2 100644 --- a/packages/utils/src/tracing.ts +++ b/packages/utils/src/tracing.ts @@ -78,3 +78,18 @@ export function tracingContextFromHeaders( propagationContext, }; } + +/** + * Create sentry-trace header from span context values. + */ +export function generateSentryTraceHeader( + traceId: string = uuid4(), + spanId: string = uuid4().substring(16), + sampled?: boolean, +): string { + let sampledString = ''; + if (sampled !== undefined) { + sampledString = sampled ? '-1' : '-0'; + } + return `${traceId}-${spanId}${sampledString}`; +} From c9b33700607e61d0582d85ed04c8ede3ccd2a68b Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 29 Jun 2023 22:37:08 -0400 Subject: [PATCH 3/9] introduce getDynamicSamplingContextFromClient --- packages/core/src/baseclient.ts | 18 ++-------- .../src/tracing/dynamicSamplingContext.ts | 32 ++++++++++++++++++ packages/core/src/tracing/index.ts | 1 + packages/core/src/tracing/transaction.ts | 33 +++++++------------ 4 files changed, 47 insertions(+), 37 deletions(-) create mode 100644 packages/core/src/tracing/dynamicSamplingContext.ts diff --git a/packages/core/src/baseclient.ts b/packages/core/src/baseclient.ts index 191feef7fd6a..52c4301f94ee 100644 --- a/packages/core/src/baseclient.ts +++ b/packages/core/src/baseclient.ts @@ -30,7 +30,6 @@ import { addItemToEnvelope, checkOrSetAlreadyCaught, createAttachmentEnvelopeItem, - dropUndefinedKeys, isPlainObject, isPrimitive, isThenable, @@ -43,12 +42,12 @@ import { } from '@sentry/utils'; import { getEnvelopeEndpointWithUrlEncodedAuth } from './api'; -import { DEFAULT_ENVIRONMENT } from './constants'; import { createEventEnvelope, createSessionEnvelope } from './envelope'; import type { IntegrationIndex } from './integration'; import { setupIntegration, setupIntegrations } from './integration'; import type { Scope } from './scope'; import { updateSession } from './session'; +import { getDynamicSamplingContextFromClient } from './tracing/dynamicSamplingContext'; import { prepareEvent } from './utils/prepareEvent'; const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured."; @@ -531,20 +530,7 @@ export abstract class BaseClient implements Client { ...evt.contexts, }; - const { publicKey: public_key } = this.getDsn() || {}; - const { segment: user_segment } = (scope && scope.getUser()) || {}; - - let dynamicSamplingContext = dsc; - if (!dsc) { - dynamicSamplingContext = dropUndefinedKeys({ - environment: options.environment || DEFAULT_ENVIRONMENT, - release: options.release, - user_segment, - public_key, - trace_id, - }); - this.emit && this.emit('createDsc', dynamicSamplingContext); - } + const dynamicSamplingContext = dsc ? dsc : getDynamicSamplingContextFromClient(trace_id, this, scope); evt.sdkProcessingMetadata = { dynamicSamplingContext, diff --git a/packages/core/src/tracing/dynamicSamplingContext.ts b/packages/core/src/tracing/dynamicSamplingContext.ts new file mode 100644 index 000000000000..989d32b8ca61 --- /dev/null +++ b/packages/core/src/tracing/dynamicSamplingContext.ts @@ -0,0 +1,32 @@ +import type { Client, DynamicSamplingContext, Scope } from '@sentry/types'; +import { dropUndefinedKeys } from '@sentry/utils'; + +import { DEFAULT_ENVIRONMENT } from '../constants'; + +/** + * Creates a dynamic sampling context from a client. + * + * Dispatchs the `createDsc` lifecycle hook as a side effect. + */ +export function getDynamicSamplingContextFromClient( + trace_id: string, + client: Client, + scope?: Scope, +): DynamicSamplingContext { + const options = client.getOptions(); + + const { publicKey: public_key } = client.getDsn() || {}; + const { segment: user_segment } = (scope && scope.getUser()) || {}; + + const dsc = dropUndefinedKeys({ + environment: options.environment || DEFAULT_ENVIRONMENT, + release: options.release, + user_segment, + public_key, + trace_id, + }); + + client.emit && client.emit('createDsc', dsc); + + return dsc; +} diff --git a/packages/core/src/tracing/index.ts b/packages/core/src/tracing/index.ts index a0d2716fda49..7a1f5336fe1c 100644 --- a/packages/core/src/tracing/index.ts +++ b/packages/core/src/tracing/index.ts @@ -7,3 +7,4 @@ export { extractTraceparentData, getActiveTransaction } from './utils'; export { SpanStatus } from './spanstatus'; export type { SpanStatusType } from './span'; export { trace } from './trace'; +export { getDynamicSamplingContextFromClient } from './dynamicSamplingContext'; diff --git a/packages/core/src/tracing/transaction.ts b/packages/core/src/tracing/transaction.ts index d62f4c3b2833..e28fb7c83014 100644 --- a/packages/core/src/tracing/transaction.ts +++ b/packages/core/src/tracing/transaction.ts @@ -11,9 +11,9 @@ import type { } from '@sentry/types'; import { dropUndefinedKeys, logger } from '@sentry/utils'; -import { DEFAULT_ENVIRONMENT } from '../constants'; import type { Hub } from '../hub'; import { getCurrentHub } from '../hub'; +import { getDynamicSamplingContextFromClient } from './dynamicSamplingContext'; import { Span as SpanClass, SpanRecorder } from './span'; /** JSDoc */ @@ -245,33 +245,24 @@ export class Transaction extends SpanClass implements TransactionInterface { return this._frozenDynamicSamplingContext; } - const hub: Hub = this._hub || getCurrentHub(); - const client = hub && hub.getClient(); + const hub = this._hub || getCurrentHub(); + const client = hub.getClient(); if (!client) return {}; - const { environment, release } = client.getOptions() || {}; - const { publicKey: public_key } = client.getDsn() || {}; + const scope = hub.getScope(); + const dsc = getDynamicSamplingContextFromClient(this.traceId, client, scope); const maybeSampleRate = this.metadata.sampleRate; - const sample_rate = maybeSampleRate !== undefined ? maybeSampleRate.toString() : undefined; - - const { segment: user_segment } = hub.getScope().getUser() || {}; - - const source = this.metadata.source; + if (maybeSampleRate !== undefined) { + dsc.sample_rate = maybeSampleRate.toString(); + } // We don't want to have a transaction name in the DSC if the source is "url" because URLs might contain PII - const transaction = source && source !== 'url' ? this.name : undefined; - - const dsc = dropUndefinedKeys({ - environment: environment || DEFAULT_ENVIRONMENT, - release, - transaction, - user_segment, - public_key, - trace_id: this.traceId, - sample_rate, - }); + const source = this.metadata.source; + if (source && source !== 'url') { + dsc.transaction = this.name; + } // Uncomment if we want to make DSC immutable // this._frozenDynamicSamplingContext = dsc; From a7d6891bbab3f2293a4ead2fc7addbef38450da2 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 30 Jun 2023 10:45:37 -0400 Subject: [PATCH 4/9] Update packages/core/src/tracing/transaction.ts Co-authored-by: Francesco Novy --- packages/core/src/tracing/transaction.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/tracing/transaction.ts b/packages/core/src/tracing/transaction.ts index e28fb7c83014..d0e1474970ab 100644 --- a/packages/core/src/tracing/transaction.ts +++ b/packages/core/src/tracing/transaction.ts @@ -255,7 +255,7 @@ export class Transaction extends SpanClass implements TransactionInterface { const maybeSampleRate = this.metadata.sampleRate; if (maybeSampleRate !== undefined) { - dsc.sample_rate = maybeSampleRate.toString(); + dsc.sample_rate = `${maybeSampleRate}`; } // We don't want to have a transaction name in the DSC if the source is "url" because URLs might contain PII From dc5b6d81d7546ea88bfd4643e94fb2faad18121a Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 30 Jun 2023 11:05:41 -0400 Subject: [PATCH 5/9] fix test order --- packages/node/test/integrations/http.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/node/test/integrations/http.test.ts b/packages/node/test/integrations/http.test.ts index 3f5a87d15363..b4d4734e88bd 100644 --- a/packages/node/test/integrations/http.test.ts +++ b/packages/node/test/integrations/http.test.ts @@ -114,9 +114,9 @@ describe('tracing', () => { const baggageHeader = request.getHeader('baggage') as string; expect(baggageHeader).toEqual( - 'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,' + + 'sentry-environment=production,sentry-release=1.0.0,' + 'sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,' + - 'sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1', + 'sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1,sentry-transaction=dogpark', ); }); @@ -130,7 +130,7 @@ describe('tracing', () => { expect(baggageHeader).toEqual([ 'dog=great', - 'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1', + 'sentry-environment=production,sentry-release=1.0.0,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1,sentry-transaction=dogpark', ]); }); @@ -144,7 +144,7 @@ describe('tracing', () => { expect(baggageHeader).toEqual([ 'dog=great', - 'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1', + 'sentry-environment=production,sentry-release=1.0.0,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1,sentry-transaction=dogpark', ]); }); From b0bd64c3aa9763f4d14f6a94ae8baea38bd32f2f Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 30 Jun 2023 12:51:36 -0400 Subject: [PATCH 6/9] fix sentry trace --- packages/node/test/integrations/undici.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node/test/integrations/undici.test.ts b/packages/node/test/integrations/undici.test.ts index 46756cbe88cd..b719d579dcbe 100644 --- a/packages/node/test/integrations/undici.test.ts +++ b/packages/node/test/integrations/undici.test.ts @@ -204,7 +204,7 @@ conditionalTest({ min: 16 })('Undici integration', () => { expect(requestHeaders['sentry-trace']).toEqual(span?.toTraceparent()); expect(requestHeaders['baggage']).toEqual( - `sentry-environment=production,sentry-transaction=test-transaction,sentry-public_key=0,sentry-trace_id=${transaction.traceId},sentry-sample_rate=1`, + `sentry-environment=production,sentry-public_key=0,sentry-trace_id=${transaction.traceId},sentry-sample_rate=1,sentry-transaction=test-transaction`, ); }); From 4aa19fe17bcfc0eac4d017a7537e31a1caec9e89 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 30 Jun 2023 13:08:01 -0400 Subject: [PATCH 7/9] fix otel tests --- packages/opentelemetry-node/test/propagator.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/opentelemetry-node/test/propagator.test.ts b/packages/opentelemetry-node/test/propagator.test.ts index d5222e3103d4..bb747c23ee1f 100644 --- a/packages/opentelemetry-node/test/propagator.test.ts +++ b/packages/opentelemetry-node/test/propagator.test.ts @@ -85,7 +85,7 @@ describe('SentryPropagator', () => { spanId: '6e0c63257de34c92', sampled: true, }, - 'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b', + 'sentry-environment=production,sentry-release=1.0.0,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-transaction=sampled-transaction', 'd4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-1', ], [ @@ -101,7 +101,7 @@ describe('SentryPropagator', () => { spanId: '6e0c63257de34c92', sampled: false, }, - 'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=not-sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b', + 'sentry-environment=production,sentry-release=1.0.0,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-transaction=not-sampled-transaction', 'd4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-0', ], [ @@ -161,7 +161,7 @@ describe('SentryPropagator', () => { const baggage = propagation.createBaggage({ foo: { value: 'bar' } }); propagator.inject(propagation.setBaggage(context, baggage), carrier, defaultTextMapSetter); expect(carrier[SENTRY_BAGGAGE_HEADER]).toBe( - 'foo=bar,sentry-environment=production,sentry-release=1.0.0,sentry-transaction=sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b', + 'foo=bar,sentry-environment=production,sentry-release=1.0.0,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-transaction=sampled-transaction', ); }); @@ -232,7 +232,7 @@ describe('SentryPropagator', () => { it('sets defined dynamic sampling context on context', () => { const baggage = - 'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dsc-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b'; + 'sentry-environment=production,sentry-release=1.0.0,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-transaction=dsc-transaction'; carrier[SENTRY_BAGGAGE_HEADER] = baggage; const context = propagator.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter); expect(context.getValue(SENTRY_DYNAMIC_SAMPLING_CONTEXT_KEY)).toEqual({ From 9386b007fe09cd766f752f47fc4f29e8c021c8e7 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 30 Jun 2023 13:32:17 -0400 Subject: [PATCH 8/9] more transaction re-arrange --- .../suites/express/sentry-trace/baggage-header-assign/test.ts | 4 ++-- .../suites/express/sentry-trace/baggage-header-out/test.ts | 4 ++-- .../baggage-other-vendors-with-sentry-entries/test.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts index 65aea16c60a1..94760b2b1d09 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts @@ -77,7 +77,7 @@ test('Should populate and propagate sentry baggage if sentry-trace header does n host: 'somewhere.not.sentry', // TraceId changes, hence we only expect that the string contains the traceid key baggage: expect.stringContaining( - 'sentry-environment=prod,sentry-release=1.0,sentry-transaction=GET%20%2Ftest%2Fexpress,sentry-public_key=public,sentry-trace_id=', + 'sentry-environment=prod,sentry-release=1.0,sentry-public_key=public,sentry-trace_id=,sentry-transaction=GET%20%2Ftest%2Fexpress', ), }, }); @@ -96,7 +96,7 @@ test('Should populate Sentry and ignore 3rd party content if sentry-trace header host: 'somewhere.not.sentry', // TraceId changes, hence we only expect that the string contains the traceid key baggage: expect.stringContaining( - 'sentry-environment=prod,sentry-release=1.0,sentry-transaction=GET%20%2Ftest%2Fexpress,sentry-public_key=public,sentry-trace_id=', + 'sentry-environment=prod,sentry-release=1.0,sentry-public_key=public,sentry-trace_id=,sentry-transaction=GET%20%2Ftest%2Fexpress', ), }, }); diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/test.ts index 71defa704bba..bcf53c1f4a30 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/test.ts @@ -13,8 +13,8 @@ test('should attach a `baggage` header to an outgoing request.', async () => { test_data: { host: 'somewhere.not.sentry', baggage: - 'sentry-environment=prod,sentry-release=1.0,sentry-transaction=GET%20%2Ftest%2Fexpress,sentry-user_segment=SegmentA' + - ',sentry-public_key=public,sentry-trace_id=86f39e84263a4de99c326acab3bfe3bd,sentry-sample_rate=1', + 'sentry-environment=prod,sentry-release=1.0,sentry-user_segment=SegmentA,sentry-public_key=public' + + ',sentry-trace_id=86f39e84263a4de99c326acab3bfe3bd,sentry-sample_rate=1,sentry-transaction=GET%20%2Ftest%2Fexpress', }, }); }); diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors-with-sentry-entries/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors-with-sentry-entries/test.ts index 0c2c2d39c606..c20354944a6d 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors-with-sentry-entries/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/baggage-other-vendors-with-sentry-entries/test.ts @@ -35,7 +35,7 @@ test('should ignore sentry-values in `baggage` header of a third party vendor an baggage: [ 'other=vendor,foo=bar,third=party,sentry-release=9.9.9,sentry-environment=staging,sentry-sample_rate=0.54,last=item', expect.stringMatching( - /sentry-environment=prod,sentry-release=1\.0,sentry-transaction=GET%20%2Ftest%2Fexpress,sentry-public_key=public,sentry-trace_id=[0-9a-f]{32},sentry-sample_rate=1/, + /sentry-environment=prod,sentry-release=1\.0,sentry-public_key=public,sentry-trace_id=[0-9a-f]{32},sentry-sample_rate=1,sentry-transaction=GET%20%2Ftest%2Fexpress/, ), ], }, From a094b8e55e5bc727ed50b19b21bb8d6133119b40 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 30 Jun 2023 14:38:44 -0400 Subject: [PATCH 9/9] fix string matching to use regex --- packages/core/src/tracing/dynamicSamplingContext.ts | 2 +- .../express/sentry-trace/baggage-header-assign/test.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core/src/tracing/dynamicSamplingContext.ts b/packages/core/src/tracing/dynamicSamplingContext.ts index 989d32b8ca61..f8e8cd107c87 100644 --- a/packages/core/src/tracing/dynamicSamplingContext.ts +++ b/packages/core/src/tracing/dynamicSamplingContext.ts @@ -24,7 +24,7 @@ export function getDynamicSamplingContextFromClient( user_segment, public_key, trace_id, - }); + }) as DynamicSamplingContext; client.emit && client.emit('createDsc', dsc); diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts index 94760b2b1d09..79b98772fe39 100644 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts +++ b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts @@ -76,8 +76,8 @@ test('Should populate and propagate sentry baggage if sentry-trace header does n test_data: { host: 'somewhere.not.sentry', // TraceId changes, hence we only expect that the string contains the traceid key - baggage: expect.stringContaining( - 'sentry-environment=prod,sentry-release=1.0,sentry-public_key=public,sentry-trace_id=,sentry-transaction=GET%20%2Ftest%2Fexpress', + baggage: expect.stringMatching( + /sentry-environment=prod,sentry-release=1.0,sentry-public_key=public,sentry-trace_id=[\S]*,sentry-sample_rate=1,sentry-transaction=GET%20%2Ftest%2Fexpress/, ), }, }); @@ -95,8 +95,8 @@ test('Should populate Sentry and ignore 3rd party content if sentry-trace header test_data: { host: 'somewhere.not.sentry', // TraceId changes, hence we only expect that the string contains the traceid key - baggage: expect.stringContaining( - 'sentry-environment=prod,sentry-release=1.0,sentry-public_key=public,sentry-trace_id=,sentry-transaction=GET%20%2Ftest%2Fexpress', + baggage: expect.stringMatching( + /sentry-environment=prod,sentry-release=1.0,sentry-public_key=public,sentry-trace_id=[\S]*,sentry-sample_rate=1,sentry-transaction=GET%20%2Ftest%2Fexpress/, ), }, });