Skip to content

Commit 91fda65

Browse files
committed
inherit thidparty tracestate when given
1 parent 934be68 commit 91fda65

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

packages/tracing/src/transaction.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ export class Transaction extends SpanClass implements TransactionInterface {
4040
}
4141

4242
this.name = transactionContext.name || '';
43-
43+
this.metadata = transactionContext.metadata || {};
4444
this._trimEnd = transactionContext.trimEnd;
4545

46-
this.metadata = transactionContext.metadata || {};
47-
this.metadata.tracestate = this.metadata.tracestate || {};
48-
this.metadata.tracestate.sentry = this.metadata.tracestate.sentry || this._getNewTracestate(this._hub);
46+
// create a new sentry tracestate value if we didn't inherit one
47+
if (!this.metadata.tracestate?.sentry) {
48+
this.metadata.tracestate = { ...this.metadata.tracestate, sentry: this._getNewTracestate(this._hub) };
49+
}
4950

5051
// this is because transactions are also spans, and spans have a transaction pointer
5152
this.transaction = this;

packages/tracing/test/hub.test.ts

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,48 +30,71 @@ describe('Hub', () => {
3030
});
3131

3232
describe('transaction creation', () => {
33+
const hub = new Hub(
34+
new BrowserClient({
35+
dsn: 'https://[email protected]/12312012',
36+
environment: 'dogpark',
37+
release: 'off.leash.trail',
38+
}),
39+
);
40+
3341
it('uses inherited values when given in transaction context', () => {
3442
const transactionContext = {
35-
name: 'dogpark',
43+
name: 'FETCH /ball',
3644
traceId: '12312012123120121231201212312012',
3745
parentSpanId: '1121201211212012',
38-
metadata: { tracestate: { sentry: 'sentry=doGsaREgReaT' } },
46+
metadata: { tracestate: { sentry: 'sentry=doGsaREgReaT', thirdparty: 'maisey=silly;charlie=goofy' } },
3947
};
40-
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
48+
4149
const transaction = hub.startTransaction(transactionContext);
4250

43-
expect(transaction).toEqual(
44-
expect.objectContaining({
45-
name: 'dogpark',
46-
traceId: '12312012123120121231201212312012',
47-
parentSpanId: '1121201211212012',
48-
metadata: expect.objectContaining({ tracestate: { sentry: 'sentry=doGsaREgReaT' } }),
49-
}),
50-
);
51+
expect(transaction).toEqual(expect.objectContaining(transactionContext));
5152
});
5253

53-
it('creates a new tracestate value if not given one in transaction context', () => {
54-
const environment = 'dogpark';
55-
const release = 'off.leash.park';
56-
const hub = new Hub(
57-
new BrowserClient({
58-
dsn: 'https://[email protected]/12312012',
59-
release,
60-
environment,
61-
}),
62-
);
54+
it('creates a new tracestate value if no tracestate data in transaction context', () => {
6355
const transaction = hub.startTransaction({ name: 'FETCH /ball' });
6456

6557
const b64Value = computeTracestateValue({
6658
traceId: transaction.traceId,
67-
environment,
68-
release,
59+
environment: 'dogpark',
60+
release: 'off.leash.trail',
6961
publicKey: 'dogsarebadatkeepingsecrets',
7062
});
7163

7264
expect(transaction.metadata?.tracestate?.sentry).toEqual(`sentry=${b64Value}`);
7365
});
7466

67+
it('creates a new tracestate value if tracestate data in transaction context only contains third party data', () => {
68+
const transactionContext = {
69+
name: 'FETCH /ball',
70+
traceId: '12312012123120121231201212312012',
71+
parentSpanId: '1121201211212012',
72+
metadata: { tracestate: { thirdparty: 'maisey=silly;charlie=goofy' } },
73+
};
74+
75+
const transaction = hub.startTransaction(transactionContext);
76+
77+
const b64Value = computeTracestateValue({
78+
traceId: transaction.traceId,
79+
environment: 'dogpark',
80+
release: 'off.leash.trail',
81+
publicKey: 'dogsarebadatkeepingsecrets',
82+
});
83+
84+
expect(transaction).toEqual(
85+
expect.objectContaining({
86+
metadata: {
87+
tracestate: {
88+
// a new value for `sentry` is created
89+
sentry: `sentry=${b64Value}`,
90+
// the third-party value isn't lost
91+
thirdparty: 'maisey=silly;charlie=goofy',
92+
},
93+
},
94+
}),
95+
);
96+
});
97+
7598
it('uses default environment if none given', () => {
7699
const release = 'off.leash.park';
77100
initSDK({

0 commit comments

Comments
 (0)