Skip to content

Commit 5ac833a

Browse files
author
Luca Forstner
authored
Merge branch 'lforst-request-span-hook' into lforst-nextjs-client-prefetch-op
2 parents 0cdb568 + 3eea48c commit 5ac833a

File tree

6 files changed

+87
-9
lines changed

6 files changed

+87
-9
lines changed

.size-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ module.exports = [
139139
path: 'packages/vue/build/esm/index.js',
140140
import: createImport('init', 'browserTracingIntegration'),
141141
gzip: true,
142-
limit: '39.5 KB',
142+
limit: '40 KB',
143143
},
144144
// Svelte SDK (ESM)
145145
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
integrations: [
8+
Sentry.browserTracingIntegration({
9+
idleTimeout: 1000,
10+
onRequestSpanStart(span, { headers }) {
11+
span.setAttribute('hook.called.headers', headers.get('foo'));
12+
},
13+
}),
14+
],
15+
tracesSampleRate: 1,
16+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fetch('http://sentry-test-site-fetch.example/', {
2+
headers: {
3+
foo: 'fetch',
4+
},
5+
});
6+
7+
const xhr = new XMLHttpRequest();
8+
9+
xhr.open('GET', 'http://sentry-test-site-xhr.example/');
10+
xhr.setRequestHeader('foo', 'xhr');
11+
xhr.send();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/core';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { getMultipleSentryEnvelopeRequests, shouldSkipTracingTest } from '../../../../utils/helpers';
6+
7+
sentryTest('should call onRequestSpanStart hook', async ({ browserName, getLocalTestUrl, page }) => {
8+
const supportedBrowsers = ['chromium', 'firefox'];
9+
10+
if (shouldSkipTracingTest() || !supportedBrowsers.includes(browserName)) {
11+
sentryTest.skip();
12+
}
13+
14+
await page.route('http://sentry-test-site-fetch.example/', async route => {
15+
await route.fulfill({
16+
status: 200,
17+
contentType: 'application/json',
18+
body: '',
19+
});
20+
});
21+
await page.route('http://sentry-test-site-xhr.example/', async route => {
22+
await route.fulfill({
23+
status: 200,
24+
contentType: 'application/json',
25+
body: '',
26+
});
27+
});
28+
29+
const url = await getLocalTestUrl({ testDir: __dirname });
30+
31+
const envelopes = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { url, timeout: 10000 });
32+
33+
const tracingEvent = envelopes[envelopes.length - 1]; // last envelope contains tracing data on all browsers
34+
35+
expect(tracingEvent.spans).toContainEqual(
36+
expect.objectContaining({
37+
op: 'http.client',
38+
data: {
39+
'hook.called.headers': 'fetch',
40+
},
41+
}),
42+
);
43+
44+
expect(tracingEvent.spans).toContainEqual(
45+
expect.objectContaining({
46+
op: 'http.client',
47+
data: {
48+
'hook.called.headers': 'xhr',
49+
},
50+
}),
51+
);
52+
});

packages/browser/src/tracing/browserTracingIntegration.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,11 @@ export interface BrowserTracingOptions {
197197
shouldCreateSpanForRequest?(this: void, url: string): boolean;
198198

199199
/**
200-
* Is called when spans are started for outgoing requests.
200+
* This callback is invoked directly after a span is started for an outgoing fetch or XHR request.
201+
* You can use it to annotate the span with additional data or attributes, for example by setting
202+
* attributes based on the passed request headers.
201203
*/
202-
onRequestSpanStart(span: Span, requestInformation: { headers?: WebFetchHeaders }): void;
204+
onRequestSpanStart?(span: Span, requestInformation: { headers?: WebFetchHeaders }): void;
203205
}
204206

205207
const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {

packages/browser/src/tracing/request.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export interface RequestInstrumentationOptions {
102102
/**
103103
* Is called when spans are started for outgoing requests.
104104
*/
105-
onRequestSpanStart(span: Span, requestInformation: { headers?: WebFetchHeaders }): void;
105+
onRequestSpanStart?(span: Span, requestInformation: { headers?: WebFetchHeaders }): void;
106106
}
107107

108108
const responseToSpanId = new WeakMap<object, string>();
@@ -113,9 +113,6 @@ export const defaultRequestInstrumentationOptions: RequestInstrumentationOptions
113113
traceXHR: true,
114114
enableHTTPTimings: true,
115115
trackFetchStreamPerformance: false,
116-
onRequestSpanStart() {
117-
// noop
118-
},
119116
};
120117

121118
/** Registers span creators for xhr and fetch requests */
@@ -191,7 +188,7 @@ export function instrumentOutgoingRequests(client: Client, _options?: Partial<Re
191188
addHTTPTimings(createdSpan);
192189
}
193190

194-
onRequestSpanStart(createdSpan, { headers: handlerData.headers });
191+
onRequestSpanStart?.(createdSpan, { headers: handlerData.headers });
195192
}
196193
});
197194
}
@@ -210,7 +207,7 @@ export function instrumentOutgoingRequests(client: Client, _options?: Partial<Re
210207
} catch {
211208
// noop
212209
}
213-
onRequestSpanStart(createdSpan, { headers });
210+
onRequestSpanStart?.(createdSpan, { headers });
214211
}
215212
});
216213
}

0 commit comments

Comments
 (0)