Skip to content

Commit 78e47cb

Browse files
authored
fix(tracing): Only record transaction name changes if data is different (#5728)
Makes sure either transaction name or source has actually been changed before recording a name-change entry.
1 parent beb3c97 commit 78e47cb

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

packages/tracing/src/transaction.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,18 @@ export class Transaction extends SpanClass implements TransactionInterface {
7676
* JSDoc
7777
*/
7878
public setName(name: string, source: TransactionMetadata['source'] = 'custom'): void {
79+
// `source` could change without the name changing if we discover that an unparameterized route is actually
80+
// parameterized by virtue of having no parameters in its path
81+
if (name !== this.name || source !== this.metadata.source) {
82+
this.metadata.changes.push({
83+
source,
84+
timestamp: timestampInSeconds(),
85+
propagations: this.metadata.propagations,
86+
});
87+
}
88+
7989
this._name = name;
8090
this.metadata.source = source;
81-
this.metadata.changes.push({
82-
source,
83-
timestamp: timestampInSeconds(),
84-
propagations: this.metadata.propagations,
85-
});
8691
}
8792

8893
/**

packages/tracing/test/transaction.test.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('`Transaction` class', () => {
2424
expect(transaction.metadata.source).toEqual('custom');
2525
});
2626

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

@@ -62,6 +62,64 @@ describe('`Transaction` class', () => {
6262
propagations: 3,
6363
},
6464
]);
65+
66+
// Only change `source`
67+
transaction.setName('playground', 'route');
68+
69+
expect(transaction.metadata.changes).toEqual([
70+
{
71+
source: 'custom',
72+
timestamp: expect.any(Number),
73+
propagations: 0,
74+
},
75+
{
76+
source: 'custom',
77+
timestamp: expect.any(Number),
78+
propagations: 3,
79+
},
80+
{
81+
source: 'route',
82+
timestamp: expect.any(Number),
83+
propagations: 3,
84+
},
85+
]);
86+
});
87+
88+
it("doesn't update transaction name changes if no change in data", () => {
89+
const transaction = new Transaction({ name: 'dogpark' });
90+
expect(transaction.metadata.changes).toEqual([]);
91+
92+
transaction.name = 'ballpit';
93+
94+
expect(transaction.metadata.changes).toEqual([
95+
{
96+
source: 'custom',
97+
timestamp: expect.any(Number),
98+
propagations: 0,
99+
},
100+
]);
101+
102+
transaction.name = 'ballpit';
103+
104+
// Still only one entry
105+
expect(transaction.metadata.changes).toEqual([
106+
{
107+
source: 'custom',
108+
timestamp: expect.any(Number),
109+
propagations: 0,
110+
},
111+
]);
112+
113+
transaction.setName('ballpit', 'custom');
114+
115+
// Still only one entry
116+
expect(transaction.metadata.changes).toEqual([
117+
{
118+
source: 'custom',
119+
timestamp: expect.any(Number),
120+
propagations: 0,
121+
},
122+
]);
65123
});
66124

67125
describe('`setName` method', () => {
@@ -81,7 +139,7 @@ describe('`Transaction` class', () => {
81139
expect(transaction.metadata.source).toEqual('route');
82140
});
83141

84-
it('updates transaction metadata with correct variables needed', () => {
142+
it('updates transaction name changes with correct variables needed', () => {
85143
const transaction = new Transaction({ name: 'dogpark' });
86144
expect(transaction.metadata.changes).toEqual([]);
87145

0 commit comments

Comments
 (0)