Skip to content

Commit 5b88771

Browse files
committed
adjust trace context structure
1 parent 794e03d commit 5b88771

File tree

7 files changed

+61
-45
lines changed

7 files changed

+61
-45
lines changed

packages/core/src/envelope.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ import {
1111
SessionEnvelope,
1212
SessionItem,
1313
} from '@sentry/types';
14-
import {
15-
createBaggage,
16-
createEnvelope,
17-
dropUndefinedKeys,
18-
dsnToString,
19-
getSentryBaggageItems,
20-
isBaggageEmpty,
21-
} from '@sentry/utils';
14+
import { createEnvelope, dropUndefinedKeys, dsnToString } from '@sentry/utils';
2215

2316
/** Extract sdk info from from the API metadata */
2417
function getSdkMetadataForEnvelopeHeader(metadata?: SdkMetadata): SdkInfo | undefined {
@@ -127,24 +120,28 @@ function createEventEnvelopeHeaders(
127120
tunnel: string | undefined,
128121
dsn: DsnComponents,
129122
): EventEnvelopeHeaders {
130-
const baggage =
131-
event.type === 'transaction' &&
132-
createBaggage(
133-
dropUndefinedKeys({
134-
environment: event.environment,
135-
release: event.release,
136-
transaction: event.transaction,
137-
userid: event.user && event.user.id,
138-
// user.segment currently doesn't exist explicitly in interface User (just as a record key)
139-
usersegment: event.user && event.user.segment,
140-
}),
141-
);
142-
143123
return {
144124
event_id: event.event_id as string,
145125
sent_at: new Date().toISOString(),
146126
...(sdkInfo && { sdk: sdkInfo }),
147127
...(!!tunnel && { dsn: dsnToString(dsn) }),
148-
...(baggage && !isBaggageEmpty(baggage) && { trace: getSentryBaggageItems(baggage) }),
128+
...(event.type === 'transaction' && {
129+
// TODO: Grab this from baggage
130+
trace: dropUndefinedKeys({
131+
// Trace context must be defined for transactions
132+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
133+
trace_id: event.contexts!.trace.trace_id,
134+
environment: event.environment,
135+
release: event.release,
136+
transaction: event.transaction,
137+
user:
138+
event.user &&
139+
dropUndefinedKeys({
140+
id: event.user.id,
141+
segment: event.user.segment,
142+
}),
143+
public_key: dsn.publicKey,
144+
}),
145+
}),
149146
};
150147
}

packages/integration-tests/suites/tracing/baggage/test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { EventEnvelopeHeaders } from '@sentry/types';
44
import { sentryTest } from '../../../utils/fixtures';
55
import { envelopeHeaderRequestParser, getFirstSentryEnvelopeRequest } from '../../../utils/helpers';
66

7-
sentryTest('should send baggage data in transaction envelope header', async ({ getLocalTestPath, page }) => {
7+
sentryTest('should send trace context data in transaction envelope header', async ({ getLocalTestPath, page }) => {
88
const url = await getLocalTestPath({ testDir: __dirname });
99

1010
const envHeader = await getFirstSentryEnvelopeRequest<EventEnvelopeHeaders>(page, url, envelopeHeaderRequestParser);
@@ -13,7 +13,9 @@ sentryTest('should send baggage data in transaction envelope header', async ({ g
1313
expect(envHeader.trace).toEqual({
1414
environment: 'production',
1515
transaction: 'testTransactionBaggage',
16-
userid: 'user123',
17-
usersegment: 'segmentB',
16+
user: {
17+
id: 'user123',
18+
segment: 'segmentB',
19+
},
1820
});
1921
});

packages/types/src/baggage.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export type AllowedBaggageKeys = 'environment' | 'release' | 'userid' | 'transaction' | 'usersegment';
2+
export type BaggageObj = Partial<Record<AllowedBaggageKeys, string> & Record<string, string>>;
3+
4+
/**
5+
* The baggage data structure represents key,value pairs based on the baggage
6+
* spec: https://www.w3.org/TR/baggage
7+
*
8+
* It is expected that users interact with baggage using the helpers methods:
9+
* `createBaggage`, `getBaggageValue`, and `setBaggageValue`.
10+
*
11+
* Internally, the baggage data structure is a tuple of length 2, separating baggage values
12+
* based on if they are related to Sentry or not. If the baggage values are
13+
* set/used by sentry, they will be stored in an object to be easily accessed.
14+
* If they are not, they are kept as a string to be only accessed when serialized
15+
* at baggage propagation time.
16+
*/
17+
export type Baggage = [BaggageObj, string];

packages/types/src/envelope.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
import { ClientReport } from './clientreport';
2+
import { DsnComponents } from './dsn';
23
import { Event } from './event';
34
import { SdkInfo } from './sdkinfo';
45
import { Session, SessionAggregates } from './session';
5-
import { TransactionSamplingMethod } from './transaction';
6+
import { Transaction, TransactionSamplingMethod } from './transaction';
67
import { UserFeedback } from './user';
78

89
// Based on: https://develop.sentry.dev/sdk/envelopes/
910

11+
// Based on https://github.com/getsentry/relay/blob/b23b8d3b2360a54aaa4d19ecae0231201f31df5e/relay-sampling/src/lib.rs#L685-L707
12+
export type EventTraceContext = {
13+
trace_id: Transaction['traceId'];
14+
public_key?: DsnComponents['publicKey'];
15+
release?: string;
16+
user?: {
17+
id?: string;
18+
segment?: string;
19+
};
20+
environment?: string;
21+
transaction?: string;
22+
};
23+
1024
export type EnvelopeItemType =
1125
| 'client_report'
1226
| 'user_report'
@@ -59,7 +73,7 @@ export type SessionItem =
5973
| BaseEnvelopeItem<SessionAggregatesItemHeaders, SessionAggregates>;
6074
export type ClientReportItem = BaseEnvelopeItem<ClientReportItemHeaders, ClientReport>;
6175

62-
export type EventEnvelopeHeaders = { event_id: string; sent_at: string; trace?: Record<string, unknown> };
76+
export type EventEnvelopeHeaders = { event_id: string; sent_at: string; trace?: EventTraceContext };
6377
type SessionEnvelopeHeaders = { sent_at: string };
6478
type ClientReportEnvelopeHeaders = BaseEnvelopeHeaders;
6579

packages/types/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type { Attachment } from './attachment';
2+
export type { AllowedBaggageKeys, Baggage, BaggageObj } from './baggage';
23
export type { Breadcrumb, BreadcrumbHint } from './breadcrumb';
34
export type { Client } from './client';
45
export type { ClientReport, Outcome, EventDropReason } from './clientreport';

packages/types/src/user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface User {
55
ip_address?: string;
66
email?: string;
77
username?: string;
8+
segment?: string;
89
}
910

1011
export interface UserFeedback {

packages/utils/src/baggage.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
1+
import { Baggage, BaggageObj } from '@sentry/types';
2+
13
import { IS_DEBUG_BUILD } from './flags';
24
import { logger } from './logger';
35

4-
export type AllowedBaggageKeys = 'environment' | 'release' | 'userid' | 'transaction' | 'usersegment';
5-
export type BaggageObj = Partial<Record<AllowedBaggageKeys, string> & Record<string, string>>;
6-
7-
/**
8-
* The baggage data structure represents key,value pairs based on the baggage
9-
* spec: https://www.w3.org/TR/baggage
10-
*
11-
* It is expected that users interact with baggage using the helpers methods:
12-
* `createBaggage`, `getBaggageValue`, and `setBaggageValue`.
13-
*
14-
* Internally, the baggage data structure is a tuple of length 2, separating baggage values
15-
* based on if they are related to Sentry or not. If the baggage values are
16-
* set/used by sentry, they will be stored in an object to be easily accessed.
17-
* If they are not, they are kept as a string to be only accessed when serialized
18-
* at baggage propagation time.
19-
*/
20-
export type Baggage = [BaggageObj, string];
21-
226
export const BAGGAGE_HEADER_NAME = 'baggage';
237

248
export const SENTRY_BAGGAGE_KEY_PREFIX = 'sentry-';

0 commit comments

Comments
 (0)