From 9a0ab65ce77602d2ca18725e43e15d44428a11c8 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Tue, 13 Sep 2022 17:33:34 +0200 Subject: [PATCH 1/9] ref: Use previous source when logging name changes --- packages/tracing/src/transaction.ts | 5 +++-- packages/types/src/transaction.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/tracing/src/transaction.ts b/packages/tracing/src/transaction.ts index cc796d1949ff..85f103758254 100644 --- a/packages/tracing/src/transaction.ts +++ b/packages/tracing/src/transaction.ts @@ -78,9 +78,10 @@ export class Transaction extends SpanClass implements TransactionInterface { public setName(name: string, source: TransactionMetadata['source'] = 'custom'): void { // `source` could change without the name changing if we discover that an unparameterized route is actually // parameterized by virtue of having no parameters in its path - if (name !== this.name || source !== this.metadata.source) { + if (this.metadata.source && (name !== this.name || source !== this.metadata.source)) { this.metadata.changes.push({ - source, + // log previous source + source: this.metadata.source, timestamp: timestampInSeconds(), propagations: this.metadata.propagations, }); diff --git a/packages/types/src/transaction.ts b/packages/types/src/transaction.ts index 2c083611b03a..ad50c9946961 100644 --- a/packages/types/src/transaction.ts +++ b/packages/types/src/transaction.ts @@ -186,7 +186,7 @@ export interface TransactionNameChange { */ timestamp: number; - /** New source applied for transaction name change */ + /** Previous source before transaction name change */ source: TransactionSource; /** Number of propagations since start of transaction */ From fd4695bea31e063ff439ee7207414974188ab67f Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 14 Sep 2022 11:59:37 +0200 Subject: [PATCH 2/9] adjust source --- packages/tracing/src/transaction.ts | 4 ++-- packages/tracing/test/transaction.test.ts | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/tracing/src/transaction.ts b/packages/tracing/src/transaction.ts index 85f103758254..747030f483f4 100644 --- a/packages/tracing/src/transaction.ts +++ b/packages/tracing/src/transaction.ts @@ -78,10 +78,10 @@ export class Transaction extends SpanClass implements TransactionInterface { public setName(name: string, source: TransactionMetadata['source'] = 'custom'): void { // `source` could change without the name changing if we discover that an unparameterized route is actually // parameterized by virtue of having no parameters in its path - if (this.metadata.source && (name !== this.name || source !== this.metadata.source)) { + if (name !== this.name || source !== this.metadata.source) { this.metadata.changes.push({ // log previous source - source: this.metadata.source, + source: this.metadata.source || source, timestamp: timestampInSeconds(), propagations: this.metadata.propagations, }); diff --git a/packages/tracing/test/transaction.test.ts b/packages/tracing/test/transaction.test.ts index 204d118f7e8d..c6599cc1a994 100644 --- a/packages/tracing/test/transaction.test.ts +++ b/packages/tracing/test/transaction.test.ts @@ -25,14 +25,14 @@ describe('`Transaction` class', () => { }); it('updates transaction name changes with correct variables needed', () => { - const transaction = new Transaction({ name: 'dogpark' }); + const transaction = new Transaction({ name: 'dogpark', metadata: { source: 'url' } }); expect(transaction.metadata.changes).toEqual([]); transaction.name = 'ballpit'; expect(transaction.metadata.changes).toEqual([ { - source: 'custom', + source: 'url', timestamp: expect.any(Number), propagations: 0, }, @@ -42,7 +42,7 @@ describe('`Transaction` class', () => { expect(transaction.metadata.changes).toEqual([ { - source: 'custom', + source: 'url', timestamp: expect.any(Number), propagations: 0, }, @@ -52,7 +52,7 @@ describe('`Transaction` class', () => { expect(transaction.metadata.changes).toEqual([ { - source: 'custom', + source: 'url', timestamp: expect.any(Number), propagations: 0, }, @@ -68,7 +68,7 @@ describe('`Transaction` class', () => { expect(transaction.metadata.changes).toEqual([ { - source: 'custom', + source: 'url', timestamp: expect.any(Number), propagations: 0, }, @@ -78,7 +78,7 @@ describe('`Transaction` class', () => { propagations: 3, }, { - source: 'route', + source: 'custom', timestamp: expect.any(Number), propagations: 3, }, @@ -140,14 +140,14 @@ describe('`Transaction` class', () => { }); it('updates transaction name changes with correct variables needed', () => { - const transaction = new Transaction({ name: 'dogpark' }); + const transaction = new Transaction({ name: 'dogpark', metadata: { source: 'url' } }); expect(transaction.metadata.changes).toEqual([]); transaction.name = 'ballpit'; expect(transaction.metadata.changes).toEqual([ { - source: 'custom', + source: 'url', timestamp: expect.any(Number), propagations: 0, }, @@ -157,7 +157,7 @@ describe('`Transaction` class', () => { expect(transaction.metadata.changes).toEqual([ { - source: 'custom', + source: 'url', timestamp: expect.any(Number), propagations: 0, }, @@ -167,12 +167,12 @@ describe('`Transaction` class', () => { expect(transaction.metadata.changes).toEqual([ { - source: 'custom', + source: 'url', timestamp: expect.any(Number), propagations: 0, }, { - source: 'task', + source: 'custom', timestamp: expect.any(Number), propagations: 3, }, From 40b9f17f521e3c8960378b322fef5e23786abbc7 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 14 Sep 2022 13:23:36 +0200 Subject: [PATCH 3/9] add url --- .../node-integration-tests/suites/express/tracing/test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/node-integration-tests/suites/express/tracing/test.ts b/packages/node-integration-tests/suites/express/tracing/test.ts index 1e74dd509b68..aaf00a899051 100644 --- a/packages/node-integration-tests/suites/express/tracing/test.ts +++ b/packages/node-integration-tests/suites/express/tracing/test.ts @@ -41,7 +41,7 @@ test('should set a correct transaction name for routes specified in RegEx', asyn changes: [ { propagations: 0, - source: 'route', + source: 'url', timestamp: expect.any(Number), }, ], @@ -77,7 +77,7 @@ test.each([['array1'], ['array5']])( changes: [ { propagations: 0, - source: 'route', + source: 'url', timestamp: expect.any(Number), }, ], @@ -121,7 +121,7 @@ test.each([ changes: [ { propagations: 0, - source: 'route', + source: 'url', timestamp: expect.any(Number), }, ], From a738342a2b8976e5529bea3b9166c38ebea58a6c Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 14 Sep 2022 13:30:23 +0200 Subject: [PATCH 4/9] make source required --- packages/hub/src/exports.ts | 8 +------- .../node-integration-tests/suites/express/tracing/test.ts | 2 +- packages/tracing/src/transaction.ts | 3 ++- packages/types/src/transaction.ts | 2 +- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/hub/src/exports.ts b/packages/hub/src/exports.ts index 0fb2345fd3ec..97a70d327b8a 100644 --- a/packages/hub/src/exports.ts +++ b/packages/hub/src/exports.ts @@ -181,11 +181,5 @@ export function startTransaction( context: TransactionContext, customSamplingContext?: CustomSamplingContext, ): ReturnType { - return getCurrentHub().startTransaction( - { - metadata: { source: 'custom' }, - ...context, - }, - customSamplingContext, - ); + return getCurrentHub().startTransaction({ ...context }, customSamplingContext); } diff --git a/packages/node-integration-tests/suites/express/tracing/test.ts b/packages/node-integration-tests/suites/express/tracing/test.ts index aaf00a899051..cf9baa12641d 100644 --- a/packages/node-integration-tests/suites/express/tracing/test.ts +++ b/packages/node-integration-tests/suites/express/tracing/test.ts @@ -41,7 +41,7 @@ test('should set a correct transaction name for routes specified in RegEx', asyn changes: [ { propagations: 0, - source: 'url', + source: 'route', timestamp: expect.any(Number), }, ], diff --git a/packages/tracing/src/transaction.ts b/packages/tracing/src/transaction.ts index 747030f483f4..2a017ef15450 100644 --- a/packages/tracing/src/transaction.ts +++ b/packages/tracing/src/transaction.ts @@ -50,6 +50,7 @@ export class Transaction extends SpanClass implements TransactionInterface { this._name = transactionContext.name || ''; this.metadata = { + source: 'custom', ...transactionContext.metadata, spanMetadata: {}, changes: [], @@ -81,7 +82,7 @@ export class Transaction extends SpanClass implements TransactionInterface { if (name !== this.name || source !== this.metadata.source) { this.metadata.changes.push({ // log previous source - source: this.metadata.source || source, + source: this.metadata.source, timestamp: timestampInSeconds(), propagations: this.metadata.propagations, }); diff --git a/packages/types/src/transaction.ts b/packages/types/src/transaction.ts index ad50c9946961..e425a7fbe791 100644 --- a/packages/types/src/transaction.ts +++ b/packages/types/src/transaction.ts @@ -146,7 +146,7 @@ export interface TransactionMetadata { requestPath?: string; /** Information on how a transaction name was generated. */ - source?: TransactionSource; + source: TransactionSource; /** Metadata for the transaction's spans, keyed by spanId */ spanMetadata: { [spanId: string]: { [key: string]: unknown } }; From 5cd5ae39434aa71e87d2bbff5f389fb255065723 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 14 Sep 2022 16:51:01 +0200 Subject: [PATCH 5/9] delete test --- packages/hub/test/exports.test.ts | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/packages/hub/test/exports.test.ts b/packages/hub/test/exports.test.ts index 350aad7e856f..e289588dacac 100644 --- a/packages/hub/test/exports.test.ts +++ b/packages/hub/test/exports.test.ts @@ -186,35 +186,6 @@ describe('Top Level API', () => { }); }); - describe('startTransaction', () => { - beforeEach(() => { - global.__SENTRY__ = { - hub: undefined, - extensions: { - startTransaction: (context: any) => ({ - name: context.name, - // Spread rather than assign in case it's undefined - metadata: { ...context.metadata }, - }), - }, - }; - }); - - it("sets source to `'custom'` if no source provided", () => { - const transaction = startTransaction({ name: 'dogpark' }); - - expect(transaction.name).toEqual('dogpark'); - expect(transaction.metadata.source).toEqual('custom'); - }); - - it('uses given `source` value', () => { - const transaction = startTransaction({ name: 'dogpark', metadata: { source: 'route' } }); - - expect(transaction.name).toEqual('dogpark'); - expect(transaction.metadata.source).toEqual('route'); - }); - }); - test('Clear Scope', () => { const client: any = new TestClient({}); getCurrentHub().withScope(() => { From 8ce4f15fe88e5d575a50b61846fb944db725c371 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 15 Sep 2022 11:21:17 +0200 Subject: [PATCH 6/9] remove start transaction export --- packages/hub/test/exports.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/hub/test/exports.test.ts b/packages/hub/test/exports.test.ts index e289588dacac..af24b4623a43 100644 --- a/packages/hub/test/exports.test.ts +++ b/packages/hub/test/exports.test.ts @@ -10,7 +10,6 @@ import { setTag, setTags, setUser, - startTransaction, withScope, } from '../src/exports'; From bc02e2eaae707827267382ecd97bd9dd25bbbb74 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 15 Sep 2022 11:46:02 +0200 Subject: [PATCH 7/9] test fixes --- .../suites/express/tracing/test.ts | 2 +- packages/node/test/integrations/http.test.ts | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/node-integration-tests/suites/express/tracing/test.ts b/packages/node-integration-tests/suites/express/tracing/test.ts index cf9baa12641d..aaf00a899051 100644 --- a/packages/node-integration-tests/suites/express/tracing/test.ts +++ b/packages/node-integration-tests/suites/express/tracing/test.ts @@ -41,7 +41,7 @@ test('should set a correct transaction name for routes specified in RegEx', asyn changes: [ { propagations: 0, - source: 'route', + source: 'url', timestamp: expect.any(Number), }, ], diff --git a/packages/node/test/integrations/http.test.ts b/packages/node/test/integrations/http.test.ts index 3f48b648b5bc..c401c7584574 100644 --- a/packages/node/test/integrations/http.test.ts +++ b/packages/node/test/integrations/http.test.ts @@ -119,7 +119,7 @@ describe('tracing', () => { const baggageHeader = request.getHeader('baggage') as string; expect(baggageHeader).toEqual( - 'sentry-environment=production,sentry-release=1.0.0,' + + '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', ); @@ -135,14 +135,14 @@ describe('tracing', () => { expect(baggageHeader).toEqual([ 'dog=great', - '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-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', ]); }); it('adds the transaction name to the the baggage header if a valid transaction source is set', async () => { nock('http://dogs.are.great').get('/').reply(200); - createTransactionOnScope({}, { metadata: { source: 'custom' } }); + createTransactionOnScope({}, { metadata: { source: 'route' } }); const request = http.get({ host: 'http://dogs.are.great/', headers: { baggage: 'dog=great' } }); const baggageHeader = request.getHeader('baggage') as string; @@ -153,6 +153,20 @@ describe('tracing', () => { ]); }); + it('does not add the transaction name to the the baggage header if url transaction source is set', async () => { + nock('http://dogs.are.great').get('/').reply(200); + + createTransactionOnScope({}, { metadata: { source: 'url' } }); + + const request = http.get({ host: 'http://dogs.are.great/', headers: { baggage: 'dog=great' } }); + const baggageHeader = request.getHeader('baggage') as string; + + expect(baggageHeader).toEqual([ + 'dog=great', + 'sentry-environment=production,sentry-release=1.0.0,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1', + ]); + }); + it("doesn't attach the sentry-trace header to outgoing sentry requests", () => { nock('http://squirrelchasers.ingest.sentry.io').get('/api/12312012/store/').reply(200); From 0e030b80b0cb32031e5e6e04b867414661cf5e74 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 15 Sep 2022 13:07:25 +0200 Subject: [PATCH 8/9] delete source --- .../baggage-header-out-bad-tx-name/server.ts | 40 ------------------- .../baggage-header-out-bad-tx-name/test.ts | 19 --------- 2 files changed, 59 deletions(-) delete mode 100644 packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/server.ts delete mode 100644 packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/test.ts diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/server.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/server.ts deleted file mode 100644 index 15d660a418ec..000000000000 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/server.ts +++ /dev/null @@ -1,40 +0,0 @@ -import * as Sentry from '@sentry/node'; -import * as Tracing from '@sentry/tracing'; -import cors from 'cors'; -import express from 'express'; -import http from 'http'; - -const app = express(); - -export type TestAPIResponse = { test_data: { host: string; 'sentry-trace': string; baggage: string } }; - -Sentry.init({ - dsn: 'https://public@dsn.ingest.sentry.io/1337', - release: '1.0', - environment: 'prod', - integrations: [new Sentry.Integrations.Http({ tracing: true }), new Tracing.Integrations.Express({ app })], - tracesSampleRate: 1.0, -}); - -Sentry.setUser({ id: 'user123', segment: 'SegmentA' }); - -app.use(Sentry.Handlers.requestHandler()); -app.use(Sentry.Handlers.tracingHandler()); - -app.use(cors()); - -app.get('/test/express', (_req, res) => { - const transaction = Sentry.getCurrentHub().getScope()?.getTransaction(); - if (transaction) { - transaction.traceId = '86f39e84263a4de99c326acab3bfe3bd'; - transaction.metadata.source = undefined; - } - const headers = http.get('http://somewhere.not.sentry/').getHeaders(); - - // Responding with the headers outgoing request headers back to the assertions. - res.send({ test_data: headers }); -}); - -app.use(Sentry.Handlers.errorHandler()); - -export default app; diff --git a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/test.ts b/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/test.ts deleted file mode 100644 index b4fa037a9d5c..000000000000 --- a/packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/test.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as path from 'path'; - -import { TestEnv } from '../../../../utils/index'; -import { TestAPIResponse } from '../server'; - -test('Does not include transaction name if transaction source is not set', async () => { - const env = await TestEnv.init(__dirname, `${path.resolve(__dirname, '.')}/server.ts`); - - const response = (await env.getAPIResponse(`${env.url}/express`)) as TestAPIResponse; - const baggageString = response.test_data.baggage; - - expect(response).toBeDefined(); - expect(response).toMatchObject({ - test_data: { - host: 'somewhere.not.sentry', - }, - }); - expect(baggageString).not.toContain('sentry-transaction='); -}); From d60d92c3a4ca32dbe4c90d866169e298d438ae9d Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 15 Sep 2022 13:34:19 +0200 Subject: [PATCH 9/9] adjust more tests --- .../test/browser/browsertracing.test.ts | 4 +-- packages/tracing/test/span.test.ts | 28 ++----------------- packages/tracing/test/transaction.test.ts | 4 +-- 3 files changed, 7 insertions(+), 29 deletions(-) diff --git a/packages/tracing/test/browser/browsertracing.test.ts b/packages/tracing/test/browser/browsertracing.test.ts index 57c7f2eaf112..2fc24fa1d670 100644 --- a/packages/tracing/test/browser/browsertracing.test.ts +++ b/packages/tracing/test/browser/browsertracing.test.ts @@ -228,7 +228,7 @@ describe('BrowserTracing', () => { expect(mockBeforeNavigation).toHaveBeenCalledTimes(1); }); - it("doesn't set transaction name source if name is not changed", () => { + it('sets transaction name source to default `custom` if name is not changed', () => { const mockBeforeNavigation = jest.fn(ctx => ({ ...ctx, })); @@ -239,7 +239,7 @@ describe('BrowserTracing', () => { const transaction = getActiveTransaction(hub) as IdleTransaction; expect(transaction).toBeDefined(); expect(transaction.name).toBe('a/path'); - expect(transaction.metadata.source).toBeUndefined(); + expect(transaction.metadata.source).toBe('custom'); expect(mockBeforeNavigation).toHaveBeenCalledTimes(1); }); diff --git a/packages/tracing/test/span.test.ts b/packages/tracing/test/span.test.ts index 9c7655d910b9..91dae2ce05c3 100644 --- a/packages/tracing/test/span.test.ts +++ b/packages/tracing/test/span.test.ts @@ -440,26 +440,23 @@ describe('Span', () => { environment: 'production', sample_rate: '0.56', trace_id: expect.any(String), + transaction: 'tx', }); }); describe('Including transaction name in DSC', () => { - test.each([ - ['is not included if transaction source is not set', undefined], - ['is not included if transaction source is url', 'url'], - ])('%s', (_: string, source) => { + test('is not included if transaction source is url', () => { const transaction = new Transaction( { name: 'tx', metadata: { - ...(source && { source: source as TransactionSource }), + source: 'url', }, }, hub, ); const dsc = transaction.getDynamicSamplingContext()!; - expect(dsc.transaction).toBeUndefined(); }); @@ -485,25 +482,6 @@ describe('Span', () => { }); describe('Transaction source', () => { - test('is not included by default', () => { - const spy = jest.spyOn(hub as any, 'captureEvent') as any; - const transaction = hub.startTransaction({ name: 'test', sampled: true }); - expect(spy).toHaveBeenCalledTimes(0); - - transaction.finish(); - - expect(spy).toHaveBeenCalledTimes(1); - expect(spy).toHaveBeenLastCalledWith( - expect.not.objectContaining({ - transaction_info: { - source: expect.any(String), - changes: [], - propagations: 0, - }, - }), - ); - }); - test('is included when transaction metadata is set', () => { const spy = jest.spyOn(hub as any, 'captureEvent') as any; const transaction = hub.startTransaction({ name: 'test', sampled: true }); diff --git a/packages/tracing/test/transaction.test.ts b/packages/tracing/test/transaction.test.ts index c6599cc1a994..ebf8ddc41533 100644 --- a/packages/tracing/test/transaction.test.ts +++ b/packages/tracing/test/transaction.test.ts @@ -9,11 +9,11 @@ describe('`Transaction` class', () => { expect(transaction.metadata.source).toEqual('route'); }); - it("doesn't set source in constructor if not provided", () => { + it("sets source to be `'custom'` in constructor if not provided", () => { const transaction = new Transaction({ name: 'dogpark' }); expect(transaction.name).toEqual('dogpark'); - expect(transaction.metadata.source).toBeUndefined(); + expect(transaction.metadata.source).toBe('custom'); }); it("sets source to `'custom'` when assigning to `name` property", () => {