Skip to content

Commit 770d76c

Browse files
committed
add baggage in envelope header integration test
1 parent 670aac4 commit 770d76c

File tree

6 files changed

+66
-6
lines changed

6 files changed

+66
-6
lines changed

packages/core/src/envelope.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ export function createEventEnvelope(
119119
if (eventType === 'transaction') {
120120
const baggage = dropUndefinedKeys(
121121
createBaggage({
122-
environment: event.environment && event.environment,
122+
environment: event.environment,
123123
release: event.release,
124124
transaction: event.transaction,
125125
userid: event.user && event.user.id,
126-
// TODO user.segment currently doesn't exist explicitly in interface User (just as a record key)
126+
// user.segment currently doesn't exist explicitly in interface User (just as a record key)
127127
usersegment: event.user && event.user.segment,
128128
} as BaggageObj),
129129
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as Sentry from '@sentry/browser';
2+
import { Integrations } from '@sentry/tracing';
3+
4+
window.Sentry = Sentry;
5+
6+
Sentry.init({
7+
dsn: 'https://[email protected]/1337',
8+
integrations: [new Integrations.BrowserTracing()],
9+
release: '1.0.0',
10+
environment: 'production',
11+
tracesSampleRate: 1,
12+
});
13+
14+
Sentry.configureScope(scope => scope.setUser({ id: 'user123', segment: 'segmentB' }));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
document.getElementById('start-transaction').addEventListener('click', () => {
2+
window.transaction = Sentry.startTransaction({ name: 'test-transaction' });
3+
Sentry.getCurrentHub().configureScope(scope => scope.setSpan(window.transaction));
4+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8" />
4+
</head>
5+
<body>
6+
<button id="start-transaction">Start Transaction</button>
7+
</body>
8+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect } from '@playwright/test';
2+
import { EventEnvelopeHeaders } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../utils/fixtures';
5+
import { envelopeHeaderRequestParser, getFirstSentryEnvelopeRequest } from '../../../utils/helpers';
6+
7+
sentryTest('should send baggage data in transaction envelope header', async ({ getLocalTestPath, page }) => {
8+
const url = await getLocalTestPath({ testDir: __dirname });
9+
10+
const pageloadTransaction = await getFirstSentryEnvelopeRequest<Event>(page, url);
11+
expect(pageloadTransaction).toBeDefined();
12+
13+
await page.click('#start-transaction');
14+
15+
const envHeader = await getFirstSentryEnvelopeRequest<EventEnvelopeHeaders>(page, url, envelopeHeaderRequestParser);
16+
17+
expect(envHeader.baggage).toBeDefined();
18+
expect(envHeader.baggage).toEqual(
19+
'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=test-transaction,sentry-userid=user123,sentry-usersegment=segmentB',
20+
);
21+
});

packages/integration-tests/utils/helpers.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Page, Request } from '@playwright/test';
2-
import { Event } from '@sentry/types';
2+
import { Event, EventEnvelopeHeaders } from '@sentry/types';
33

44
const envelopeUrlRegex = /\.sentry\.io\/api\/\d+\/envelope\//;
55

@@ -11,6 +11,14 @@ const envelopeRequestParser = (request: Request | null): Event => {
1111
return envelope.split('\n').map(line => JSON.parse(line))[2];
1212
};
1313

14+
export const envelopeHeaderRequestParser = (request: Request | null): EventEnvelopeHeaders => {
15+
// https://develop.sentry.dev/sdk/envelopes/
16+
const envelope = request?.postData() || '';
17+
18+
// First row of the envelop is the event payload.
19+
return envelope.split('\n').map(line => JSON.parse(line))[0];
20+
};
21+
1422
/**
1523
* Run script at the given path inside the test environment.
1624
*
@@ -122,10 +130,11 @@ async function getMultipleSentryEnvelopeRequests<T>(
122130
url?: string;
123131
timeout?: number;
124132
},
133+
requestParser: (req: Request) => T = envelopeRequestParser as (req: Request) => T,
125134
): Promise<T[]> {
126135
// TODO: This is not currently checking the type of envelope, just casting for now.
127136
// We can update this to include optional type-guarding when we have types for Envelope.
128-
return getMultipleRequests(page, count, envelopeUrlRegex, envelopeRequestParser, options) as Promise<T[]>;
137+
return getMultipleRequests(page, count, envelopeUrlRegex, requestParser, options) as Promise<T[]>;
129138
}
130139

131140
/**
@@ -136,8 +145,12 @@ async function getMultipleSentryEnvelopeRequests<T>(
136145
* @param {string} [url]
137146
* @return {*} {Promise<T>}
138147
*/
139-
async function getFirstSentryEnvelopeRequest<T>(page: Page, url?: string): Promise<T> {
140-
return (await getMultipleSentryEnvelopeRequests<T>(page, 1, { url }))[0];
148+
async function getFirstSentryEnvelopeRequest<T>(
149+
page: Page,
150+
url?: string,
151+
requestParser: (req: Request) => T = envelopeRequestParser as (req: Request) => T,
152+
): Promise<T> {
153+
return (await getMultipleSentryEnvelopeRequests<T>(page, 1, { url }, requestParser))[0];
141154
}
142155

143156
/**

0 commit comments

Comments
 (0)