Skip to content

Commit cd78621

Browse files
committed
add other missing DSC values + adjust tests
1 parent a550b9d commit cd78621

File tree

13 files changed

+116
-73
lines changed

13 files changed

+116
-73
lines changed

packages/core/src/envelope.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function createEventEnvelopeHeaders(
101101
dsn: DsnComponents,
102102
): EventEnvelopeHeaders {
103103
const baggage = event.sdkProcessingMetadata && event.sdkProcessingMetadata.baggage;
104-
const { environment, release, transaction, userid, usersegment, samplerate } =
104+
const { environment, release, transaction, userid, usersegment, samplerate, publickey, traceid } =
105105
(baggage && getSentryBaggageItems(baggage)) || {};
106106

107107
return {
@@ -110,15 +110,10 @@ function createEventEnvelopeHeaders(
110110
...(sdkInfo && { sdk: sdkInfo }),
111111
...(!!tunnel && { dsn: dsnToString(dsn) }),
112112
...(event.type === 'transaction' &&
113-
// If we don't already have a trace context in the event, we can't get the trace id, which makes adding any other
114-
// trace data pointless
115-
event.contexts &&
116-
event.contexts.trace && {
113+
baggage && {
117114
trace: dropUndefinedKeys({
118-
// Trace context must be defined for transactions
119-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
120-
trace_id: (event.contexts!.trace as Record<string, unknown>).trace_id as string,
121-
public_key: dsn.publicKey,
115+
trace_id: traceid,
116+
public_key: publickey,
122117
sample_rate: samplerate,
123118
environment,
124119
release,

packages/core/test/lib/envelope.test.ts

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,23 @@ describe('createEventEnvelope', () => {
1414
expect(envelopeHeaders.trace).toBeUndefined();
1515
});
1616

17-
it('adds minimal trace data if event is a transaction and no other baggage-related data is available', () => {
18-
const event: Event = {
19-
type: 'transaction',
20-
contexts: {
21-
trace: {
22-
trace_id: '1234',
23-
},
24-
},
25-
};
26-
const envelopeHeaders = createEventEnvelope(event, testDsn)[0];
27-
28-
expect(envelopeHeaders).toBeDefined();
29-
expect(envelopeHeaders.trace).toEqual({ trace_id: '1234', public_key: 'pubKey123' });
30-
});
31-
3217
const testTable: Array<[string, Event, EventTraceContext]> = [
3318
[
34-
'adds one baggage item',
19+
'adds minimal baggage items',
3520
{
3621
type: 'transaction',
37-
contexts: {
38-
trace: {
39-
trace_id: '1234',
40-
},
41-
},
4222
sdkProcessingMetadata: {
43-
baggage: [{ release: '1.0.0' }, '', false],
23+
baggage: [{ traceid: '1234', publickey: 'pubKey123' }, '', false],
4424
},
4525
},
46-
{ release: '1.0.0', trace_id: '1234', public_key: 'pubKey123' },
26+
{ trace_id: '1234', public_key: 'pubKey123' },
4727
],
4828
[
49-
'adds two baggage items',
29+
'adds multiple baggage items',
5030
{
5131
type: 'transaction',
52-
contexts: {
53-
trace: {
54-
trace_id: '1234',
55-
},
56-
},
5732
sdkProcessingMetadata: {
58-
baggage: [{ environment: 'prod', release: '1.0.0' }, '', false],
33+
baggage: [{ environment: 'prod', release: '1.0.0', publickey: 'pubKey123', traceid: '1234' }, '', false],
5934
},
6035
},
6136
{ release: '1.0.0', environment: 'prod', trace_id: '1234', public_key: 'pubKey123' },
@@ -64,11 +39,6 @@ describe('createEventEnvelope', () => {
6439
'adds all baggage items',
6540
{
6641
type: 'transaction',
67-
contexts: {
68-
trace: {
69-
trace_id: '1234',
70-
},
71-
},
7242
sdkProcessingMetadata: {
7343
baggage: [
7444
{
@@ -78,6 +48,8 @@ describe('createEventEnvelope', () => {
7848
usersegment: 'segmentA',
7949
transaction: 'TX',
8050
samplerate: '0.95',
51+
publickey: 'pubKey123',
52+
traceid: '1234',
8153
},
8254
'',
8355
false,

packages/integration-tests/suites/tracing/browsertracing/meta/template.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<head>
33
<meta charset="utf-8" />
44
<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-1" />
5-
<meta name="baggage" content="sentry-release=2.1.12" />
5+
<meta
6+
name="baggage"
7+
content="sentry-release=2.1.12,sentry-publickey=public,sentry-traceid=123,sentry-samplerate=0.3232"
8+
/>
69
</head>
710
</html>

packages/integration-tests/suites/tracing/browsertracing/meta/test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ sentryTest(
3030

3131
expect(envHeader.trace).toBeDefined();
3232
expect(envHeader.trace).toEqual({
33-
public_key: 'public',
34-
trace_id: expect.any(String),
3533
release: '2.1.12',
34+
sample_rate: '0.3232',
35+
trace_id: '123',
36+
public_key: 'public',
3637
});
3738
},
3839
);

packages/integration-tests/suites/tracing/envelope-header/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ Sentry.init({
1313

1414
Sentry.configureScope(scope => {
1515
scope.setUser({ id: 'user123', segment: 'segmentB' });
16-
scope.setTransactionName('testTransactionBaggage');
16+
scope.setTransactionName('testTransactionDSC');
1717
});

packages/integration-tests/suites/tracing/envelope-header/test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ sentryTest(
1414
expect(envHeader.trace).toBeDefined();
1515
expect(envHeader.trace).toEqual({
1616
environment: 'production',
17-
// TODO comment back in once we properly support transaction and user data in Baggage
18-
// transaction: 'testTransactionBaggage',
19-
// user: {
20-
// id: 'user123',
21-
// segment: 'segmentB',
22-
// },
17+
transaction: expect.stringContaining('index.html'),
18+
user: {
19+
id: 'user123',
20+
segment: 'segmentB',
21+
},
2322
sample_rate: '1',
24-
public_key: 'public',
2523
trace_id: expect.any(String),
24+
public_key: 'public',
2625
});
2726
},
2827
);

packages/node-integration-tests/suites/express/sentry-trace/baggage-header-assign/test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ test('Should populate and propagate sentry baggage if sentry-trace header does n
7777
expect(response).toMatchObject({
7878
test_data: {
7979
host: 'somewhere.not.sentry',
80-
baggage: 'sentry-environment=prod,sentry-release=1.0,sentry-samplerate=1',
80+
baggage: expect.stringContaining(
81+
'sentry-environment=prod,sentry-release=1.0,sentry-transaction=GET%20%2Ftest%2Fexpress,sentry-samplerate=1,' +
82+
'sentry-publickey=public,sentry-traceid=',
83+
),
8184
},
8285
});
8386
});
@@ -93,7 +96,11 @@ test('Should populate Sentry and propagate 3rd party content if sentry-trace hea
9396
expect(response).toMatchObject({
9497
test_data: {
9598
host: 'somewhere.not.sentry',
96-
baggage: 'foo=bar,bar=baz,sentry-environment=prod,sentry-release=1.0,sentry-samplerate=1',
99+
// TraceId changes, hence we only expect that the string contains the traceid key
100+
baggage: expect.stringContaining(
101+
'foo=bar,bar=baz,sentry-environment=prod,sentry-release=1.0,sentry-transaction=GET%20%2Ftest%2Fexpress,' +
102+
'sentry-samplerate=1,sentry-publickey=public,sentry-traceid=',
103+
),
97104
},
98105
});
99106
});

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@ describe('tracing', () => {
2727
const hub = new Hub(new NodeClient(options));
2828
addExtensionMethods();
2929

30+
hub.configureScope(scope =>
31+
scope.setUser({
32+
id: 'uid123',
33+
segment: 'segmentA',
34+
}),
35+
);
36+
3037
// we need to mock both of these because the tracing handler relies on `@sentry/core` while the sampler relies on
3138
// `@sentry/hub`, and mocking breaks the link between the two
3239
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
3340
jest.spyOn(hubModule, 'getCurrentHub').mockReturnValue(hub);
3441

35-
const transaction = hub.startTransaction({ name: 'dogpark' });
42+
const transaction = hub.startTransaction({
43+
name: 'dogpark',
44+
traceId: '12312012123120121231201212312012',
45+
});
3646
hub.getScope()?.setSpan(transaction);
3747

3848
return transaction;
@@ -98,7 +108,11 @@ describe('tracing', () => {
98108
const baggageHeader = request.getHeader('baggage') as string;
99109

100110
expect(baggageHeader).toBeDefined();
101-
expect(baggageHeader).toEqual('sentry-environment=production,sentry-release=1.0.0,sentry-samplerate=1');
111+
expect(baggageHeader).toEqual(
112+
'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,sentry-userid=uid123,' +
113+
'sentry-usersegment=segmentA,sentry-samplerate=1,sentry-publickey=dogsarebadatkeepingsecrets,' +
114+
'sentry-traceid=12312012123120121231201212312012',
115+
);
102116
});
103117

104118
it('propagates 3rd party baggage header data to outgoing non-sentry requests', async () => {
@@ -110,7 +124,11 @@ describe('tracing', () => {
110124
const baggageHeader = request.getHeader('baggage') as string;
111125

112126
expect(baggageHeader).toBeDefined();
113-
expect(baggageHeader).toEqual('dog=great,sentry-environment=production,sentry-release=1.0.0,sentry-samplerate=1');
127+
expect(baggageHeader).toEqual(
128+
'dog=great,sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,' +
129+
'sentry-userid=uid123,sentry-usersegment=segmentA,sentry-samplerate=1,' +
130+
'sentry-publickey=dogsarebadatkeepingsecrets,sentry-traceid=12312012123120121231201212312012',
131+
);
114132
});
115133

116134
it("doesn't attach the sentry-trace header to outgoing sentry requests", () => {

packages/tracing/src/span.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable max-lines */
22
import { getCurrentHub } from '@sentry/hub';
3-
import { Baggage, Hub, Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
3+
import { Baggage, Hub, Primitive, Span as SpanInterface, SpanContext } from '@sentry/types';
44
import {
55
createBaggage,
66
dropUndefinedKeys,
@@ -12,6 +12,8 @@ import {
1212
uuid4,
1313
} from '@sentry/utils';
1414

15+
import { Transaction } from './transaction';
16+
1517
/**
1618
* Keeps track of finished spans for a given transaction
1719
* @internal
@@ -320,7 +322,7 @@ export class Span implements SpanInterface {
320322
// custom sentry-values in DSC (added by users in the future)
321323
const finalBaggage =
322324
!existingBaggage || (isBaggageMutable(existingBaggage) && isSentryBaggageEmpty(existingBaggage))
323-
? this._getBaggageWithSentryValues(existingBaggage)
325+
? this._populateBaggageWithSentryValues(existingBaggage)
324326
: existingBaggage;
325327

326328
return finalBaggage;
@@ -369,19 +371,35 @@ export class Span implements SpanInterface {
369371
*
370372
* @returns modified and immutable baggage object
371373
*/
372-
private _getBaggageWithSentryValues(baggage: Baggage = createBaggage({})): Baggage {
373-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
374-
const hub: Hub = ((this.transaction as any) && (this.transaction as any)._hub) || getCurrentHub();
375-
const client = hub.getClient();
374+
private _populateBaggageWithSentryValues(baggage: Baggage = createBaggage({})): Baggage {
375+
const hub: Hub = (this.transaction && this.transaction._hub) || getCurrentHub();
376+
const client = hub && hub.getClient();
376377

377378
const { environment, release } = (client && client.getOptions()) || {};
379+
const { publicKey } = (client && client.getDsn()) || {};
378380

379381
const metadata = this.transaction && this.transaction.metadata;
380382
const sampelRate = metadata && metadata.transactionSampling && metadata.transactionSampling.rate;
381383

384+
const traceId = this.traceId;
385+
386+
const transactionName = this.transaction && this.transaction.name;
387+
388+
let userId, userSegment;
389+
hub.withScope(scope => {
390+
const user = scope.getUser();
391+
userId = user && user.id;
392+
userSegment = user && user.segment;
393+
});
394+
382395
environment && setBaggageValue(baggage, 'environment', environment);
383396
release && setBaggageValue(baggage, 'release', release);
397+
transactionName && setBaggageValue(baggage, 'transaction', transactionName);
398+
userId && setBaggageValue(baggage, 'userid', userId);
399+
userSegment && setBaggageValue(baggage, 'usersegment', userSegment);
384400
sampelRate && setBaggageValue(baggage, 'samplerate', sampelRate.toString());
401+
publicKey && setBaggageValue(baggage, 'publickey', publicKey);
402+
traceId && setBaggageValue(baggage, 'traceid', traceId);
385403

386404
setBaggageImmutable(baggage);
387405

packages/tracing/src/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
1919
/**
2020
* The reference to the current hub.
2121
*/
22-
protected readonly _hub: Hub;
22+
public readonly _hub: Hub;
2323

2424
private _measurements: Measurements = {};
2525

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BrowserClient } from '@sentry/browser';
22
import { Hub, makeMain } from '@sentry/hub';
3-
import type { Baggage, BaggageObj, BaseTransportOptions, ClientOptions } from '@sentry/types';
3+
import type { Baggage, BaggageObj, BaseTransportOptions, ClientOptions, DsnComponents } from '@sentry/types';
44
import {
55
getGlobalObject,
66
InstrumentHandlerCallback,
@@ -433,6 +433,12 @@ describe('BrowserTracing', () => {
433433
environment: 'production',
434434
} as ClientOptions<BaseTransportOptions>;
435435
};
436+
437+
hub.getClient()!.getDsn = () => {
438+
return {
439+
publicKey: 'pubKey',
440+
} as DsnComponents;
441+
};
436442
});
437443

438444
it('uses the tracing data for pageload transactions', () => {
@@ -498,7 +504,13 @@ describe('BrowserTracing', () => {
498504
expect(transaction.parentSpanId).toBeUndefined();
499505
expect(baggage).toBeDefined();
500506
expect(baggage[0]).toBeDefined();
501-
expect(baggage[0]).toEqual({ release: '1.0.0', environment: 'production' });
507+
expect(baggage[0]).toEqual({
508+
release: '1.0.0',
509+
environment: 'production',
510+
publickey: 'pubKey',
511+
traceid: expect.any(String),
512+
transaction: 'blank',
513+
});
502514
expect(baggage[1]).toBeDefined();
503515
expect(baggage[1]).toEqual('');
504516
});

packages/tracing/test/span.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ describe('Span', () => {
393393
});
394394
});
395395

396-
describe('getBaggage and _getBaggageWithSentryValues', () => {
396+
describe('getBaggage and _populateBaggageWithSentryValues', () => {
397397
beforeEach(() => {
398398
hub.getClient()!.getOptions = () => {
399399
return {
@@ -428,7 +428,10 @@ describe('Span', () => {
428428
const transaction = new Transaction(
429429
{
430430
name: 'tx',
431-
metadata: { baggage: createBaggage({}, '', true) },
431+
metadata: {
432+
baggage: createBaggage({}, '', true),
433+
transactionSampling: { rate: 0.56, method: 'client_rate' },
434+
},
432435
},
433436
hub,
434437
);
@@ -441,7 +444,13 @@ describe('Span', () => {
441444

442445
expect(hubSpy).toHaveBeenCalledTimes(1);
443446
expect(baggage && isSentryBaggageEmpty(baggage)).toBe(false);
444-
expect(baggage && getSentryBaggageItems(baggage)).toStrictEqual({ release: '1.0.1', environment: 'production' });
447+
expect(baggage && getSentryBaggageItems(baggage)).toStrictEqual({
448+
release: '1.0.1',
449+
environment: 'production',
450+
transaction: 'tx',
451+
samplerate: '0.56',
452+
traceid: expect.any(String),
453+
});
445454
expect(baggage && getThirdPartyBaggage(baggage)).toStrictEqual('');
446455
});
447456
});

packages/types/src/baggage.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
export type AllowedBaggageKeys = 'environment' | 'release' | 'userid' | 'transaction' | 'usersegment' | 'samplerate';
1+
export type AllowedBaggageKeys =
2+
| 'environment'
3+
| 'release'
4+
| 'transaction'
5+
| 'userid'
6+
| 'usersegment'
7+
| 'samplerate'
8+
| 'traceid'
9+
| 'publickey';
10+
211
export type BaggageObj = Partial<Record<AllowedBaggageKeys, string> & Record<string, string>>;
312

413
/**

0 commit comments

Comments
 (0)