GeezerVison is showing github.com — feat(core): Add sentry.kind, url.path and url.scheme to fetch and XHR spans by sentry-junior[bot] · Pull Request #26638 · getsentry/sentry-javascript
feat(core): Add sentry.kind, url.path and url.scheme to fetch and XHR spans
#26638
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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b420f90
feat(core): Add Node's http.client span attributes to server fetch spans
sentry-junior[bot] Nov 19, 2026
cd73f02
test(core): Fold the serverAttributes fetch test into the url attribu…
sentry-junior[bot] Nov 19, 2026
7238cbf
feat(browser): Set sentry.kind, url.path and url.scheme on browser fe…
sentry-junior[bot] Nov 19, 2026
b85c9ec
test(react-router): Ignore the browser's http.client span in the __ma…
sentry-junior[bot] Nov 19, 2026
043bdbc
ref(core): Drop comment on the fetch breadcrumb method attribute
sentry-junior[bot] Nov 19, 2026
10a4082
test(bun): Run the shared fetch suites with @sentry/bun
sentry-junior[bot] Nov 20, 2026
bcbbb8a
fix(browser): Set url.path and url.scheme on relative fetch spans
sentry-junior[bot] Nov 20, 2026
25da22e
fix(core): Set url.path and url.scheme on relative fetch spans at start
sentry-junior[bot] Nov 20, 2026
f4878dd
ref(core): Simplify relative fetch URL resolution
sentry-junior[bot] Nov 20, 2026
File filter
Filter by extension
.mjs
(1)
.ts
(18)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -157,20 +157,6 @@ const NO_NATIVE_NODE_FETCH_INTEGRATION = [
'suites/tracing/requests/fetch-no-tracing-no-spans/test.ts',
];
// The `fetchIntegration` of `@sentry/bun` comes from `@sentry/core`. Its spans, breadcrumbs and
// `sentry-trace` headers differ from the ones `@sentry/node` creates, for example the span origin
// is `auto.http.fetch`, not `auto.http.node_fetch`, and the spans have no `url.path`, so
// `ignoreSpans` cannot match them on it.
const FETCH_INTEGRATION_DIFFERS = [
'suites/tracing/double-baggage/spans-parent/test.ts',
'suites/tracing/http-client-spans/fetch-basic/test.ts',
'suites/tracing/http-client-spans/fetch-error/test.ts',
'suites/tracing/http-client-spans/fetch-strip-query/test.ts',
'suites/tracing/ignoreSpans/continued-trace-http-client/test.ts',
'suites/tracing/requests/fetch-breadcrumbs/test.ts',
'suites/tracing/requests/fetch-sampled-no-active-span/test.ts',
];
// `@sentry/bun` creates `http.server` spans with `bunHttpServerIntegration`, not `httpIntegration`.
// Its spans differ: response headers become attributes, there is no `http.response.status_text`,
// and the span name has no route. A server that sets `emit` back to the `emit` it had before its
Expand Down
Expand Up
@@ -213,17 +199,22 @@ export const SENTRY_NODE_EXCLUDE = [...NO_HTTP_SERVER_SPANS, ...NO_FETCH_INSTRUM
// servers without those.
const BUN_DROPS_PIPELINED_REQUESTS = ['suites/tunnel/hardening/test.ts'];
// Before Bun 1.4, `require('../package.json')` in an ES module of `proxy` fails with `Cannot find
// module '../package.json' from ''` when `alias-sentry-bun.ts` wraps `Module._resolveFilename`.
// Excluded in `node-suites-sentry-bun` only before Bun 1.4.
export const SENTRY_BUN_BEFORE_1_4_EXCLUDE = ['suites/proxy/test.ts'];
// - `proxy`: `require('../package.json')` in an ES module fails with `Cannot find module
// '../package.json' from ''` when `alias-sentry-bun.ts` wraps `Module._resolveFilename`.
// - The other two make an outgoing `node:http` request, which gets no `sentry-trace` or `baggage`
// header before Bun 1.4 (see `BUN_1_3_NO_HTTP_CLIENT_CHANNEL`).
export const SENTRY_BUN_BEFORE_1_4_EXCLUDE = [
'suites/proxy/test.ts',
'suites/tracing/double-baggage/spans-parent/test.ts',
'suites/tracing/ignoreSpans/continued-trace-http-client/test.ts',
];
export const SENTRY_BUN_EXCLUDE = [
...BUN_DROPS_PIPELINED_REQUESTS,
...HTTP_SERVER_SPANS_DIFFER,
...NO_EXPRESS_INSTRUMENTATION,
...NO_NATIVE_NODE_FETCH_INTEGRATION,
...FETCH_INTEGRATION_DIFFERS,
];
// The build project (`node-suites-bun-build`) runs the suites of `NO_AUTO_INSTRUMENTATION` with the
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -38,6 +38,7 @@ it('Tracing headers', async ({ signal }) => {
category : 'fetch' ,
data : {
method : 'GET' ,
'http.request.method' : 'GET' ,
status_code : 200 ,
url : expect . stringContaining ( 'http://localhost:' ) ,
} ,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -135,16 +135,6 @@ const NO_CLIENT_REPORTS = [
'suites/tracing/sampling/test.ts',
];
// The `fetchIntegration` of `@sentry/deno` creates other spans and breadcrumbs than `@sentry/node`:
// the span origin is `auto.http.fetch`, not `auto.http.node_fetch`, the spans have fewer
// attributes, and the breadcrumb category is `fetch`, not `http`.
const FETCH_INTEGRATION_DIFFERS = [
'suites/tracing/http-client-spans/fetch-basic/test.ts',
'suites/tracing/http-client-spans/fetch-error/test.ts',
'suites/tracing/http-client-spans/fetch-strip-query/test.ts',
'suites/tracing/requests/fetch-breadcrumbs/test.ts',
];
// The stack frames of `@sentry/deno` have no `module`, and some have other `function` names. Their
// `post_context` also ends with an extra empty line, and in CommonJS scenarios the line numbers can
// be off.
Expand All
@@ -171,7 +161,6 @@ export const NODE_SUITES_EXCLUDE = [
...NO_NODE_DEFAULT_INTEGRATION,
...GLOBAL_HANDLERS_DIFFER,
...NO_CLIENT_REPORTS,
...FETCH_INTEGRATION_DIFFERS,
...STACK_FRAMES_DIFFER,
...REQUEST_IP_DIFFERS,
];
Original file line number
Diff line number
Diff line change
Expand Up
@@ -128,7 +128,7 @@ test('Should trace outgoing fetch requests inside middleware', async ({ request
) ;
// Only Node.js has the undici diagnostics channel. On the other runtimes the fetch integration of the runtime
// SDK creates this span, which has no `sentry.kind`, `url.path` and `url.scheme` .
// SDK creates this span.
const isNode = getRuntime ( ) === 'node' ;
// In some builds (especially webpack) the fetch span is not a child of the middleware segment but a
Expand All
@@ -154,7 +154,7 @@ test('Should trace outgoing fetch requests inside middleware', async ({ request
expect ( fetchSpan . attributes [ 'server.port' ] ?. value ) . toBe ( 3030 ) ;
expect ( fetchSpan . attributes [ 'url.domain' ] ?. value ) . toBe ( 'localhost' ) ;
expect ( fetchSpan . attributes [ 'url.full' ] ?. value ) . toBe ( 'http://localhost:3030/' ) ;
expect ( fetchSpan . attributes [ 'sentry.kind' ] ?. value ) . toBe ( isNode ? 'client' : undefined ) ;
expect ( fetchSpan . attributes [ 'url.path' ] ?. value ) . toBe ( isNode ? '/' : undefined ) ;
expect ( fetchSpan . attributes [ 'url.scheme' ] ?. value ) . toBe ( isNode ? 'http' : undefined ) ;
expect ( fetchSpan . attributes [ 'sentry.kind' ] ?. value ) . toBe ( 'client' ) ;
expect ( fetchSpan . attributes [ 'url.path' ] ?. value ) . toBe ( '/' ) ;
expect ( fetchSpan . attributes [ 'url.scheme' ] ?. value ) . toBe ( 'http' ) ;
} ) ;
Original file line number
Diff line number
Diff line change
Expand Up
@@ -25,9 +25,10 @@ test.describe('low-quality span filter', () => {
// Force the server to flush any in-flight spans before we assert
await page.evaluate(() => fetch('/__sentry-flush'));
// The browser's own fetch of /__manifest is an http.client span, which is expected.
const targetIsManifest = (span: SerializedStreamedSpan) => {
const urlPath = span.attributes['url.path']?.value;
return typeof urlPath === 'string' && urlPath.includes('/__manifest');
return getSpanOp(span) !== 'http.client' && typeof urlPath === 'string' && urlPath.includes('/__manifest');
};
expect(streamedSpans.some(targetIsManifest)).toBe(false);
});
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -30,9 +30,10 @@ test.describe('low-quality span filter', () => {
// Force the server to flush any in-flight spans before we assert
await page.evaluate(() => fetch('/__sentry-flush'));
// The browser's own fetch of /__manifest is an http.client span, which is expected.
const targetIsManifest = (span: SerializedStreamedSpan) => {
const urlPath = span.attributes['url.path']?.value;
return typeof urlPath === 'string' && urlPath.includes('/__manifest');
return getSpanOp(span) !== 'http.client' && typeof urlPath === 'string' && urlPath.includes('/__manifest');
};
expect(streamedSpans.some(targetIsManifest)).toBe(false);
});
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
import { createTestServer } from '@sentry-internal/test-utils';
import { afterAll, describe, expect } from 'vitest';
import { EXPECTED_FETCH_SPAN_ORIGIN } from '../../../../utils';
import { cleanupChildProcesses, createCjsTests } from '../../../../utils/runner';
describe('outgoing fetch spans', () => {
Expand Down
Expand Up
@@ -35,13 +36,13 @@ describe('outgoing fetch spans', () => {
expect.objectContaining({
description: expect.stringMatching(/GET .*\/api\/v0/),
op: 'http.client',
origin: 'auto.http.node_fetch' ,
origin: EXPECTED_FETCH_SPAN_ORIGIN ,
status: 'ok',
}),
expect.objectContaining({
description: expect.stringMatching(/GET .*\/api\/v1/),
op: 'http.client',
origin: 'auto.http.node_fetch' ,
origin: EXPECTED_FETCH_SPAN_ORIGIN ,
status: 'not_found',
data: expect.objectContaining({
'http.response.status_code': 404,
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
import { HTTP_CLIENT } from '@sentry/conventions/op';
import { SENTRY_OP, SENTRY_ORIGIN, URL_FULL } from '@sentry/conventions/attributes';
import { afterAll, describe, expect } from 'vitest';
import { EXPECTED_FETCH_SPAN_ORIGIN } from '../../../../utils';
import { cleanupChildProcesses, createCjsTests } from '../../../../utils/runner';
describe('outgoing fetch spans - error', () => {
Expand All
@@ -22,7 +23,7 @@ describe('outgoing fetch spans - error', () => {
attributes: expect.objectContaining({
[URL_FULL]: { type: 'string', value: expect.stringMatching(/http:\/\/localhost:\d+\//) },
[SENTRY_OP]: { type: 'string', value: HTTP_CLIENT },
[SENTRY_ORIGIN]: { type: 'string', value: 'auto.http.node_fetch' },
[SENTRY_ORIGIN]: { type: 'string', value: EXPECTED_FETCH_SPAN_ORIGIN },
}),
}),
]),
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -17,6 +17,7 @@ import {
} from '@sentry/conventions/attributes';
import { createTestServer } from '@sentry-internal/test-utils';
import { afterAll, describe, expect } from 'vitest';
import { EXPECTED_FETCH_SPAN_ORIGIN, INSTRUMENTS_UNDICI_FETCH } from '../../../../utils';
import { cleanupChildProcesses, createCjsTests } from '../../../../utils/runner';
describe('outgoing fetch spans - strip query', () => {
Expand All
@@ -25,7 +26,9 @@ describe('outgoing fetch spans - strip query', () => {
});
createCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => {
test('strips and handles query params in spans of outgoing fetch requests', async () => {
async function runStripQueryTest(
expectSpanAttributes: (serverUrl: string) => Record<string, unknown>,
): Promise<void> {
expect.assertions(4);
const [SERVER_URL, closeTestServer] = await createTestServer()
Expand All
@@ -45,21 +48,17 @@ describe('outgoing fetch spans - strip query', () => {
expect(spans).toHaveLength(1);
expect(spans[0]).toMatchObject({
attributes: {
[URL_FULL]: { type: 'string', value: `${SERVER_URL}/api/v0/users?id=1` },
[URL_PATH]: { type: 'string', value: '/api/v0/users' },
[URL_QUERY]: { type: 'string', value: 'id=1' },
[URL_SCHEME]: { type: 'string', value: 'http' },
[HTTP_REQUEST_METHOD]: { type: 'string', value: 'GET' },
'http.request.method_original': { type: 'string', value: 'GET' },
[HTTP_RESPONSE_STATUS_CODE]: { type: 'integer', value: 200 },
[NETWORK_PEER_ADDRESS]: { type: 'string', value: '::1' },
[NETWORK_PEER_PORT]: { type: 'integer', value: expect.any(Number) },
[SENTRY_KIND]: { type: 'string', value: 'client' },
[SERVER_PORT]: { type: 'integer', value: expect.any(Number) },
[USER_AGENT_ORIGINAL]: { type: 'string', value: 'node' },
[SENTRY_OP]: { type: 'string', value: HTTP_CLIENT },
[SENTRY_ORIGIN]: { type: 'string', value: 'auto.http.node_fetch' },
[SENTRY_ORIGIN]: { type: 'string', value: EXPECTED_FETCH_SPAN_ORIGIN },
[SERVER_ADDRESS]: { type: 'string', value: 'localhost' },
...expectSpanAttributes(SERVER_URL),
},
name: 'GET localhost',
status: 'ok',
Expand All
@@ -72,6 +71,30 @@ describe('outgoing fetch spans - strip query', () => {
.start()
.completed();
closeTestServer();
});
}
// undici drops the fragment and sees the connection and the default headers.
test.runIf(INSTRUMENTS_UNDICI_FETCH)(
'strips and handles query params in spans of outgoing fetch requests (undici)',
async () => {
await runStripQueryTest(serverUrl => ({
[URL_FULL]: { type: 'string', value: `${serverUrl}/api/v0/users?id=1` },
'http.request.method_original': { type: 'string', value: 'GET' },
[NETWORK_PEER_ADDRESS]: { type: 'string', value: '::1' },
[NETWORK_PEER_PORT]: { type: 'integer', value: expect.any(Number) },
[USER_AGENT_ORIGINAL]: { type: 'string', value: 'node' },
}));
},
);
// A patched global `fetch` keeps the fragment, and gets the method already normalized.
test.skipIf(INSTRUMENTS_UNDICI_FETCH)(
'strips and handles query params in spans of outgoing fetch requests (patched global fetch)',
async () => {
await runStripQueryTest(serverUrl => ({
[URL_FULL]: { type: 'string', value: `${serverUrl}/api/v0/users?id=1#fragment` },
}));
},
);
});
});
Original file line number
Diff line number
Diff line change
Expand Up
@@ -11,8 +11,9 @@ Sentry.init({
// Ensure this gets a correct hint
beforeBreadcrumb(breadcrumb, hint) {
breadcrumb.data = breadcrumb.data || {};
const req = hint?.request;
breadcrumb.data.ADDED_PATH = req?.path;
// `@sentry/node` passes the undici request. The SDKs that patch the global `fetch` pass its arguments.
const path = hint?.request ? hint.request.path : hint?.input && new URL(String(hint.input[0])).pathname;
breadcrumb.data.ADDED_PATH = path;
return breadcrumb;
},
});
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
import { createTestServer } from '@sentry-internal/test-utils';
import { describe, expect } from 'vitest';
import { EXPECTED_FETCH_BREADCRUMB_CATEGORY } from '../../../../utils';
import { createEsmAndCjsTests } from '../../../../utils/runner';
describe('outgoing fetch', () => {
Expand All
@@ -17,7 +18,7 @@ describe('outgoing fetch', () => {
timestamp: expect.any(Number),
},
{
category: 'http' ,
category: EXPECTED_FETCH_BREADCRUMB_CATEGORY ,
data: {
'http.request.method': 'GET',
url: `${SERVER_URL}/api/v0`,
Expand All
@@ -28,7 +29,7 @@ describe('outgoing fetch', () => {
type: 'http',
},
{
category: 'http' ,
category: EXPECTED_FETCH_BREADCRUMB_CATEGORY ,
data: {
'http.request.method': 'GET',
url: `${SERVER_URL}/api/v1`,
Expand All
@@ -39,7 +40,7 @@ describe('outgoing fetch', () => {
type: 'http',
},
{
category: 'http' ,
category: EXPECTED_FETCH_BREADCRUMB_CATEGORY ,
data: {
'http.request.method': 'GET',
url: `${SERVER_URL}/api/v2`,
Expand All
@@ -50,7 +51,7 @@ describe('outgoing fetch', () => {
type: 'http',
},
{
category: 'http' ,
category: EXPECTED_FETCH_BREADCRUMB_CATEGORY ,
data: {
'http.request.method': 'GET',
url: `${SERVER_URL}/api/v3`,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -20,6 +20,19 @@ const BUN_VERSION = RUNTIME === 'bun' ? execFileSync('bun', ['--version'], { enc
*/
export const EXPECTED_SDK_NAME = process.env.EXPECTED_SDK_NAME || 'sentry.javascript.node';
/**
* Whether the scenarios run `@sentry/node`, which instruments `fetch` through undici's diagnostics
* channels. The SDKs of the other runtimes patch the global `fetch` instead: their spans have another
* origin and no connection data (`network.peer.*`), and their breadcrumbs have another category.
*/
export const INSTRUMENTS_UNDICI_FETCH = EXPECTED_SDK_NAME === 'sentry.javascript.node';
/** The `sentry.origin` of outgoing `fetch` spans. */
export const EXPECTED_FETCH_SPAN_ORIGIN = INSTRUMENTS_UNDICI_FETCH ? 'auto.http.node_fetch' : 'auto.http.fetch';
/** The `category` of outgoing `fetch` breadcrumbs. */
export const EXPECTED_FETCH_BREADCRUMB_CATEGORY = INSTRUMENTS_UNDICI_FETCH ? 'http' : 'fetch';
export type TestServerConfig = {
url: string;
server: http.Server;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -37,12 +37,15 @@ import { WINDOW } from '../helpers';
import { baggageHeaderHasSentryValues, createHeadersSafely, getFullURL, isPerformanceResourceTiming } from './utils';
import {
HTTP_REQUEST_METHOD,
SENTRY_KIND,
SENTRY_OP,
SERVER_ADDRESS,
URL_DOMAIN,
URL_FRAGMENT,
URL_FULL,
URL_PATH,
URL_QUERY,
URL_SCHEME,
SENTRY_ORIGIN,
} from '@sentry/conventions/attributes';
import { HTTP_CLIENT } from '@sentry/conventions/op';
Expand Down
Expand Up
@@ -421,6 +424,9 @@ function xhrCallback(
[URL_DOMAIN]: domain,
[SENTRY_ORIGIN]: 'auto.http.browser',
[SENTRY_OP]: HTTP_CLIENT,
[SENTRY_KIND]: 'client',
[URL_PATH]: host ? parsedUrl?.path || '/' : undefined,
[URL_SCHEME]: host ? parsedUrl?.protocol : undefined,
[URL_QUERY]: filterCollectedUrlQuery(getUrlQuery(parsedUrl?.search)),
[URL_FRAGMENT]: getUrlFragment(parsedUrl?.hash),
},
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -2,7 +2,15 @@ import type { Client } from '@sentry/core';
import * as utils from '@sentry/core';
import * as coreBrowser from '@sentry/core/browser';
import * as browserUtils from '@sentry/browser-utils';
import { HTTP_REQUEST_METHOD, SERVER_ADDRESS, URL_DOMAIN, URL_FULL } from '@sentry/conventions/attributes';
import {
HTTP_REQUEST_METHOD,
SENTRY_KIND,
SERVER_ADDRESS,
URL_DOMAIN,
URL_FULL,
URL_PATH,
URL_SCHEME,
} from '@sentry/conventions/attributes';
import type { MockInstance } from 'vitest';
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { BrowserClient } from '../../src/client';
Expand Down
Expand Up
@@ -232,6 +240,7 @@ describe('instrumentOutgoingRequests', () => {
let fetchHandler: ((data: utils.HandlerDataFetch) => void) | undefined;
let requestSpan: utils.Span | undefined;
let startAttributes: utils.SpanAttributes | undefined;
vi.spyOn(utils, 'addFetchInstrumentationHandler').mockImplementation(handler => {
fetchHandler = handler;
Expand All
@@ -245,6 +254,7 @@ describe('instrumentOutgoingRequests', () => {
enableHTTPTimings: false,
onRequestSpanStart: span => {
requestSpan = span;
startAttributes = utils.spanToJSON(span).attributes;
},
});
fetchHandler?.({
Expand All
@@ -256,6 +266,9 @@ describe('instrumentOutgoingRequests', () => {
const requestSpanJson = utils.spanToJSON(requestSpan!);
expect(requestSpanJson.name).toBe('GET app.example.com');
expect(requestSpanJson.attributes[URL_DOMAIN]).toBe('app.example.com');
// Set when the span starts, so `ignoreSpans` and `tracesSampler` can match on them.
expect(startAttributes?.[URL_PATH]).toBe('/rest/v1/users');
expect(startAttributes?.[URL_SCHEME]).toBe('https');
vi.unstubAllGlobals();
});
Expand Down
Expand Up
@@ -295,6 +308,9 @@ describe('instrumentOutgoingRequests', () => {
const requestSpanJson = utils.spanToJSON(requestSpan!);
expect(requestSpanJson.name).toBe('GET app.example.com');
expect(requestSpanJson.attributes[URL_DOMAIN]).toBe('app.example.com');
expect(requestSpanJson.attributes[SENTRY_KIND]).toBe('client');
expect(requestSpanJson.attributes[URL_PATH]).toBe('/rest/v1/users');
expect(requestSpanJson.attributes[URL_SCHEME]).toBe('https');
vi.unstubAllGlobals();
});
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.