From 91596630fcaa0a220bd0dc57485f2456eba585ff Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 17 May 2024 09:02:00 +0200 Subject: [PATCH] fix(core): Add dsn to span envelope header --- packages/core/src/envelope.ts | 4 ++++ packages/core/test/lib/envelope.test.ts | 26 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/packages/core/src/envelope.ts b/packages/core/src/envelope.ts index 258216b290d4..d0e5148574f2 100644 --- a/packages/core/src/envelope.ts +++ b/packages/core/src/envelope.ts @@ -110,9 +110,13 @@ export function createSpanEnvelope(spans: SentrySpan[], client?: Client): SpanEn // different segments in one envelope const dsc = getDynamicSamplingContextFromSpan(spans[0]); + const dsn = client && client.getDsn(); + const tunnel = client && client.getOptions().tunnel; + const headers: SpanEnvelope[0] = { sent_at: new Date().toISOString(), ...(dscHasRequiredProps(dsc) && { trace: dsc }), + ...(!!tunnel && dsn && { dsn: dsnToString(dsn) }), }; const beforeSendSpan = client && client.getOptions().beforeSendSpan; diff --git a/packages/core/test/lib/envelope.test.ts b/packages/core/test/lib/envelope.test.ts index 7d5a2c5f7740..5652f48a545d 100644 --- a/packages/core/test/lib/envelope.test.ts +++ b/packages/core/test/lib/envelope.test.ts @@ -144,6 +144,32 @@ describe('createSpanEnvelope', () => { }); }); + it('adds `dsn` envelope header if tunnel is enabled', () => { + const options = getDefaultTestClientOptions({ dsn: 'https://username@domain/123', tunnel: 'http://tunnel' }); + const client = new TestClient(options); + + const spanEnvelope = createSpanEnvelope( + [new SentrySpan({ name: 'test', attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom' } })], + client, + ); + + const spanEnvelopeHeaders = spanEnvelope[0]; + expect(spanEnvelopeHeaders.dsn).toEqual('https://username@domain/123'); + }); + + it('does not add `dsn` envelope header if tunnel is not enabled', () => { + const options = getDefaultTestClientOptions({ dsn: 'https://username@domain/123' }); + const client = new TestClient(options); + + const spanEnvelope = createSpanEnvelope( + [new SentrySpan({ name: 'test', attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom' } })], + client, + ); + + const spanEnvelopeHeaders = spanEnvelope[0]; + expect(spanEnvelopeHeaders.dsn).toBeUndefined(); + }); + it("doesn't add a `trace` envelope header if there's no public key", () => { const options = getDefaultTestClientOptions({ tracesSampleRate: 1, dsn: 'https://domain/123' }); client = new TestClient(options);