Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add sampling decision to trace envelope header #8483

Merged
merged 7 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/browser-integration-tests/suites/replay/dsc/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ sentryTest(
trace_id: expect.any(String),
public_key: 'public',
replay_id: replay.session?.id,
sampled: 'true',
});
},
);
Expand Down Expand Up @@ -93,6 +94,7 @@ sentryTest(
sample_rate: '1',
trace_id: expect.any(String),
public_key: 'public',
sampled: 'true',
});
},
);
Expand Down Expand Up @@ -152,6 +154,7 @@ sentryTest(
trace_id: expect.any(String),
public_key: 'public',
replay_id: replay.session?.id,
sampled: 'true',
});
},
);
Expand Down Expand Up @@ -199,6 +202,7 @@ sentryTest(
sample_rate: '1',
trace_id: expect.any(String),
public_key: 'public',
sampled: 'true',
});
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sentryTest(
transaction: expect.stringContaining('/index.html'),
trace_id: expect.any(String),
public_key: 'public',
sampled: 'true',
});
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sentryTest(
sample_rate: '1',
trace_id: expect.any(String),
public_key: 'public',
sampled: 'true',
});
},
);
6 changes: 4 additions & 2 deletions packages/core/src/tracing/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,13 @@ export class Transaction extends SpanClass implements TransactionInterface {
dsc.transaction = this.name;
}

if (this.sampled !== undefined) {
dsc.sampled = String(this.sampled);
}

// Uncomment if we want to make DSC immutable
// this._frozenDynamicSamplingContext = dsc;

client.emit && client.emit('createDsc', dsc);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this because getDynamicSamplingContextFromClient already emits createDsc on the client as a side effect, so no need to duplicate it.


return dsc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ test('should attach a `baggage` header to an outgoing request.', async () => {
host: 'somewhere.not.sentry',
baggage:
'sentry-environment=prod,sentry-release=1.0,sentry-user_segment=SegmentA,sentry-public_key=public' +
',sentry-trace_id=86f39e84263a4de99c326acab3bfe3bd,sentry-sample_rate=1,sentry-transaction=GET%20%2Ftest%2Fexpress',
',sentry-trace_id=86f39e84263a4de99c326acab3bfe3bd,sentry-sample_rate=1,sentry-transaction=GET%20%2Ftest%2Fexpress' +
',sentry-sampled=true',
},
});
});
17 changes: 9 additions & 8 deletions packages/node/test/integrations/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ describe('tracing', () => {
expect(baggageHeader).toEqual(
'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-transaction=dogpark',
'sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1,' +
'sentry-transaction=dogpark,sentry-sampled=true',
);
});

Expand All @@ -143,10 +144,10 @@ describe('tracing', () => {
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,sentry-transaction=dogpark',
]);
expect(baggageHeader[0]).toEqual('dog=great');
expect(baggageHeader[1]).toEqual(
'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-transaction=dogpark,sentry-sampled=true',
);
});

it('adds the transaction name to the the baggage header if a valid transaction source is set', async () => {
Expand All @@ -159,7 +160,7 @@ 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-transaction=dogpark',
'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-transaction=dogpark,sentry-sampled=true',
]);
});

Expand All @@ -173,7 +174,7 @@ 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-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1,sentry-sampled=true',
]);
});

Expand All @@ -191,7 +192,7 @@ describe('tracing', () => {
expect(parts[2]).toEqual('1');

expect(baggageHeader).toEqual(
'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-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1,sentry-sampled=true',
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/node/test/integrations/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ conditionalTest({ min: 16 })('Undici integration', () => {

expect(requestHeaders['sentry-trace'].includes(propagationContext.traceId)).toBe(true);
expect(requestHeaders['baggage']).toEqual(
`sentry-environment=production,sentry-public_key=0,sentry-trace_id=${propagationContext.traceId}`,
`sentry-environment=production,sentry-public_key=0,sentry-trace_id=${propagationContext.traceId},sentry-sample_rate=1,sentry-transaction=test-transaction,sentry-sampled=true`,
);
});

Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-node/test/propagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('SentryPropagator', () => {
spanId: '6e0c63257de34c92',
sampled: true,
},
'sentry-environment=production,sentry-release=1.0.0,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-transaction=sampled-transaction',
'sentry-environment=production,sentry-release=1.0.0,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-transaction=sampled-transaction,sentry-sampled=true',
'd4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-1',
],
[
Expand All @@ -101,7 +101,7 @@ describe('SentryPropagator', () => {
spanId: '6e0c63257de34c92',
sampled: false,
},
'sentry-environment=production,sentry-release=1.0.0,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-transaction=not-sampled-transaction',
'sentry-environment=production,sentry-release=1.0.0,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-transaction=not-sampled-transaction,sentry-sampled=false',
'd4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-0',
],
[
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('SentryPropagator', () => {
const baggage = propagation.createBaggage({ foo: { value: 'bar' } });
propagator.inject(propagation.setBaggage(context, baggage), carrier, defaultTextMapSetter);
expect(carrier[SENTRY_BAGGAGE_HEADER]).toBe(
'foo=bar,sentry-environment=production,sentry-release=1.0.0,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-transaction=sampled-transaction',
'foo=bar,sentry-environment=production,sentry-release=1.0.0,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-transaction=sampled-transaction,sentry-sampled=true',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ conditionalTest({ min: 10 })('BrowserTracing', () => {
release: '1.0.0',
environment: 'production',
public_key: 'pubKey',
sampled: 'false',
trace_id: expect.not.stringMatching('12312012123120121231201212312012'),
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type DynamicSamplingContext = {
transaction?: string;
user_segment?: string;
replay_id?: string;
sampled?: string;
};

export type EnvelopeItemType =
Expand Down