Skip to content

Commit 51bc032

Browse files
authored
ref(tracing): Use previous source when logging name changes (#5733)
Use previous source instead of current one when tracking name changes. To make sure this is always accurate, set the default source in the transaction constructor of `custom`.
1 parent 347fbe4 commit 51bc032

File tree

11 files changed

+44
-145
lines changed

11 files changed

+44
-145
lines changed

packages/hub/src/exports.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,5 @@ export function startTransaction(
181181
context: TransactionContext,
182182
customSamplingContext?: CustomSamplingContext,
183183
): ReturnType<Hub['startTransaction']> {
184-
return getCurrentHub().startTransaction(
185-
{
186-
metadata: { source: 'custom' },
187-
...context,
188-
},
189-
customSamplingContext,
190-
);
184+
return getCurrentHub().startTransaction({ ...context }, customSamplingContext);
191185
}

packages/hub/test/exports.test.ts

-30
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
setTag,
1111
setTags,
1212
setUser,
13-
startTransaction,
1413
withScope,
1514
} from '../src/exports';
1615

@@ -186,35 +185,6 @@ describe('Top Level API', () => {
186185
});
187186
});
188187

189-
describe('startTransaction', () => {
190-
beforeEach(() => {
191-
global.__SENTRY__ = {
192-
hub: undefined,
193-
extensions: {
194-
startTransaction: (context: any) => ({
195-
name: context.name,
196-
// Spread rather than assign in case it's undefined
197-
metadata: { ...context.metadata },
198-
}),
199-
},
200-
};
201-
});
202-
203-
it("sets source to `'custom'` if no source provided", () => {
204-
const transaction = startTransaction({ name: 'dogpark' });
205-
206-
expect(transaction.name).toEqual('dogpark');
207-
expect(transaction.metadata.source).toEqual('custom');
208-
});
209-
210-
it('uses given `source` value', () => {
211-
const transaction = startTransaction({ name: 'dogpark', metadata: { source: 'route' } });
212-
213-
expect(transaction.name).toEqual('dogpark');
214-
expect(transaction.metadata.source).toEqual('route');
215-
});
216-
});
217-
218188
test('Clear Scope', () => {
219189
const client: any = new TestClient({});
220190
getCurrentHub().withScope(() => {

packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/server.ts

-40
This file was deleted.

packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out-bad-tx-name/test.ts

-19
This file was deleted.

packages/node-integration-tests/suites/express/tracing/test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test('should set a correct transaction name for routes specified in RegEx', asyn
4141
changes: [
4242
{
4343
propagations: 0,
44-
source: 'route',
44+
source: 'url',
4545
timestamp: expect.any(Number),
4646
},
4747
],
@@ -77,7 +77,7 @@ test.each([['array1'], ['array5']])(
7777
changes: [
7878
{
7979
propagations: 0,
80-
source: 'route',
80+
source: 'url',
8181
timestamp: expect.any(Number),
8282
},
8383
],
@@ -121,7 +121,7 @@ test.each([
121121
changes: [
122122
{
123123
propagations: 0,
124-
source: 'route',
124+
source: 'url',
125125
timestamp: expect.any(Number),
126126
},
127127
],

packages/node/test/integrations/http.test.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe('tracing', () => {
119119
const baggageHeader = request.getHeader('baggage') as string;
120120

121121
expect(baggageHeader).toEqual(
122-
'sentry-environment=production,sentry-release=1.0.0,' +
122+
'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,' +
123123
'sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,' +
124124
'sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1',
125125
);
@@ -135,14 +135,14 @@ describe('tracing', () => {
135135

136136
expect(baggageHeader).toEqual([
137137
'dog=great',
138-
'sentry-environment=production,sentry-release=1.0.0,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1',
138+
'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',
139139
]);
140140
});
141141

142142
it('adds the transaction name to the the baggage header if a valid transaction source is set', async () => {
143143
nock('http://dogs.are.great').get('/').reply(200);
144144

145-
createTransactionOnScope({}, { metadata: { source: 'custom' } });
145+
createTransactionOnScope({}, { metadata: { source: 'route' } });
146146

147147
const request = http.get({ host: 'http://dogs.are.great/', headers: { baggage: 'dog=great' } });
148148
const baggageHeader = request.getHeader('baggage') as string;
@@ -153,6 +153,20 @@ describe('tracing', () => {
153153
]);
154154
});
155155

156+
it('does not add the transaction name to the the baggage header if url transaction source is set', async () => {
157+
nock('http://dogs.are.great').get('/').reply(200);
158+
159+
createTransactionOnScope({}, { metadata: { source: 'url' } });
160+
161+
const request = http.get({ host: 'http://dogs.are.great/', headers: { baggage: 'dog=great' } });
162+
const baggageHeader = request.getHeader('baggage') as string;
163+
164+
expect(baggageHeader).toEqual([
165+
'dog=great',
166+
'sentry-environment=production,sentry-release=1.0.0,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1',
167+
]);
168+
});
169+
156170
it("doesn't attach the sentry-trace header to outgoing sentry requests", () => {
157171
nock('http://squirrelchasers.ingest.sentry.io').get('/api/12312012/store/').reply(200);
158172

packages/tracing/src/transaction.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
4444
this._name = transactionContext.name || '';
4545

4646
this.metadata = {
47+
source: 'custom',
4748
...transactionContext.metadata,
4849
spanMetadata: {},
4950
changes: [],
@@ -82,7 +83,8 @@ export class Transaction extends SpanClass implements TransactionInterface {
8283
// parameterized by virtue of having no parameters in its path
8384
if (name !== this.name || source !== this.metadata.source) {
8485
this.metadata.changes.push({
85-
source,
86+
// log previous source
87+
source: this.metadata.source,
8688
timestamp: timestampInSeconds(),
8789
propagations: this.metadata.propagations,
8890
});

packages/tracing/test/browser/browsertracing.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ describe('BrowserTracing', () => {
228228
expect(mockBeforeNavigation).toHaveBeenCalledTimes(1);
229229
});
230230

231-
it("doesn't set transaction name source if name is not changed", () => {
231+
it('sets transaction name source to default `custom` if name is not changed', () => {
232232
const mockBeforeNavigation = jest.fn(ctx => ({
233233
...ctx,
234234
}));
@@ -239,7 +239,7 @@ describe('BrowserTracing', () => {
239239
const transaction = getActiveTransaction(hub) as IdleTransaction;
240240
expect(transaction).toBeDefined();
241241
expect(transaction.name).toBe('a/path');
242-
expect(transaction.metadata.source).toBeUndefined();
242+
expect(transaction.metadata.source).toBe('custom');
243243

244244
expect(mockBeforeNavigation).toHaveBeenCalledTimes(1);
245245
});

packages/tracing/test/span.test.ts

+3-25
Original file line numberDiff line numberDiff line change
@@ -440,26 +440,23 @@ describe('Span', () => {
440440
environment: 'production',
441441
sample_rate: '0.56',
442442
trace_id: expect.any(String),
443+
transaction: 'tx',
443444
});
444445
});
445446

446447
describe('Including transaction name in DSC', () => {
447-
test.each([
448-
['is not included if transaction source is not set', undefined],
449-
['is not included if transaction source is url', 'url'],
450-
])('%s', (_: string, source) => {
448+
test('is not included if transaction source is url', () => {
451449
const transaction = new Transaction(
452450
{
453451
name: 'tx',
454452
metadata: {
455-
...(source && { source: source as TransactionSource }),
453+
source: 'url',
456454
},
457455
},
458456
hub,
459457
);
460458

461459
const dsc = transaction.getDynamicSamplingContext()!;
462-
463460
expect(dsc.transaction).toBeUndefined();
464461
});
465462

@@ -485,25 +482,6 @@ describe('Span', () => {
485482
});
486483

487484
describe('Transaction source', () => {
488-
test('is not included by default', () => {
489-
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
490-
const transaction = hub.startTransaction({ name: 'test', sampled: true });
491-
expect(spy).toHaveBeenCalledTimes(0);
492-
493-
transaction.finish();
494-
495-
expect(spy).toHaveBeenCalledTimes(1);
496-
expect(spy).toHaveBeenLastCalledWith(
497-
expect.not.objectContaining({
498-
transaction_info: {
499-
source: expect.any(String),
500-
changes: [],
501-
propagations: 0,
502-
},
503-
}),
504-
);
505-
});
506-
507485
test('is included when transaction metadata is set', () => {
508486
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
509487
const transaction = hub.startTransaction({ name: 'test', sampled: true });

packages/tracing/test/transaction.test.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ describe('`Transaction` class', () => {
99
expect(transaction.metadata.source).toEqual('route');
1010
});
1111

12-
it("doesn't set source in constructor if not provided", () => {
12+
it("sets source to be `'custom'` in constructor if not provided", () => {
1313
const transaction = new Transaction({ name: 'dogpark' });
1414

1515
expect(transaction.name).toEqual('dogpark');
16-
expect(transaction.metadata.source).toBeUndefined();
16+
expect(transaction.metadata.source).toBe('custom');
1717
});
1818

1919
it("sets source to `'custom'` when assigning to `name` property", () => {
@@ -25,14 +25,14 @@ describe('`Transaction` class', () => {
2525
});
2626

2727
it('updates transaction name changes with correct variables needed', () => {
28-
const transaction = new Transaction({ name: 'dogpark' });
28+
const transaction = new Transaction({ name: 'dogpark', metadata: { source: 'url' } });
2929
expect(transaction.metadata.changes).toEqual([]);
3030

3131
transaction.name = 'ballpit';
3232

3333
expect(transaction.metadata.changes).toEqual([
3434
{
35-
source: 'custom',
35+
source: 'url',
3636
timestamp: expect.any(Number),
3737
propagations: 0,
3838
},
@@ -42,7 +42,7 @@ describe('`Transaction` class', () => {
4242

4343
expect(transaction.metadata.changes).toEqual([
4444
{
45-
source: 'custom',
45+
source: 'url',
4646
timestamp: expect.any(Number),
4747
propagations: 0,
4848
},
@@ -52,7 +52,7 @@ describe('`Transaction` class', () => {
5252

5353
expect(transaction.metadata.changes).toEqual([
5454
{
55-
source: 'custom',
55+
source: 'url',
5656
timestamp: expect.any(Number),
5757
propagations: 0,
5858
},
@@ -68,7 +68,7 @@ describe('`Transaction` class', () => {
6868

6969
expect(transaction.metadata.changes).toEqual([
7070
{
71-
source: 'custom',
71+
source: 'url',
7272
timestamp: expect.any(Number),
7373
propagations: 0,
7474
},
@@ -78,7 +78,7 @@ describe('`Transaction` class', () => {
7878
propagations: 3,
7979
},
8080
{
81-
source: 'route',
81+
source: 'custom',
8282
timestamp: expect.any(Number),
8383
propagations: 3,
8484
},
@@ -140,14 +140,14 @@ describe('`Transaction` class', () => {
140140
});
141141

142142
it('updates transaction name changes with correct variables needed', () => {
143-
const transaction = new Transaction({ name: 'dogpark' });
143+
const transaction = new Transaction({ name: 'dogpark', metadata: { source: 'url' } });
144144
expect(transaction.metadata.changes).toEqual([]);
145145

146146
transaction.name = 'ballpit';
147147

148148
expect(transaction.metadata.changes).toEqual([
149149
{
150-
source: 'custom',
150+
source: 'url',
151151
timestamp: expect.any(Number),
152152
propagations: 0,
153153
},
@@ -157,7 +157,7 @@ describe('`Transaction` class', () => {
157157

158158
expect(transaction.metadata.changes).toEqual([
159159
{
160-
source: 'custom',
160+
source: 'url',
161161
timestamp: expect.any(Number),
162162
propagations: 0,
163163
},
@@ -167,12 +167,12 @@ describe('`Transaction` class', () => {
167167

168168
expect(transaction.metadata.changes).toEqual([
169169
{
170-
source: 'custom',
170+
source: 'url',
171171
timestamp: expect.any(Number),
172172
propagations: 0,
173173
},
174174
{
175-
source: 'task',
175+
source: 'custom',
176176
timestamp: expect.any(Number),
177177
propagations: 3,
178178
},

0 commit comments

Comments
 (0)