Skip to content
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

feat(node): fetch breadcrumbs without tracing #14018

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const DEPENDENTS: Dependent[] = [
ignoreExports: [
// not supported in bun:
'NodeClient',
// Doesn't have these events
'fetchBreadcrumbsIntegration',
// Bun doesn't emit the required diagnostics_channel events
'processThreadBreadcrumbIntegration',
'childProcessIntegration',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
tracePropagationTargets: [/\/v0/, 'v1'],
integrations: [Sentry.nativeNodeFetchIntegration({ breadcrumbs: false })],
transport: loggingTransport,
tracesSampleRate: 0.0,
});

async function run(): Promise<void> {
Sentry.addBreadcrumb({ message: 'manual breadcrumb' });

// Since fetch is lazy loaded, we need to wait a bit until it's fully instrumented
await new Promise(resolve => setTimeout(resolve, 100));
await fetch(`${process.env.SERVER_URL}/api/v0`).then(res => res.text());
await fetch(`${process.env.SERVER_URL}/api/v1`).then(res => res.text());
await fetch(`${process.env.SERVER_URL}/api/v2`).then(res => res.text());
await fetch(`${process.env.SERVER_URL}/api/v3`).then(res => res.text());

Sentry.captureException(new Error('foo'));
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
tracePropagationTargets: [/\/v0/, 'v1'],
integrations: [],
transport: loggingTransport,
tracesSampleRate: 0.0,
fetchBreadcrumbs: false,
});

async function run(): Promise<void> {
Sentry.addBreadcrumb({ message: 'manual breadcrumb' });

// Since fetch is lazy loaded, we need to wait a bit until it's fully instrumented
await new Promise(resolve => setTimeout(resolve, 100));
await fetch(`${process.env.SERVER_URL}/api/v0`).then(res => res.text());
await fetch(`${process.env.SERVER_URL}/api/v1`).then(res => res.text());
await fetch(`${process.env.SERVER_URL}/api/v2`).then(res => res.text());
await fetch(`${process.env.SERVER_URL}/api/v3`).then(res => res.text());

Sentry.captureException(new Error('foo'));
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
run();
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,62 @@ conditionalTest({ min: 18 })('outgoing fetch', () => {
.start(closeTestServer);
});
});

test('outgoing fetch requests should not create breadcrumbs when disabled', done => {
createTestServer(done)
.start()
.then(([SERVER_URL, closeTestServer]) => {
createRunner(__dirname, 'scenario-disabled.ts')
.withEnv({ SERVER_URL })
.ensureNoErrorOutput()
.expect({
event: {
breadcrumbs: [
{
message: 'manual breadcrumb',
timestamp: expect.any(Number),
},
],
exception: {
values: [
{
type: 'Error',
value: 'foo',
},
],
},
},
})
.start(closeTestServer);
});
});

test('outgoing fetch requests should not create breadcrumbs when legacy disabled', done => {
createTestServer(done)
.start()
.then(([SERVER_URL, closeTestServer]) => {
createRunner(__dirname, 'scenario-disabled-legacy.ts')
.withEnv({ SERVER_URL })
.ensureNoErrorOutput()
.expect({
event: {
breadcrumbs: [
{
message: 'manual breadcrumb',
timestamp: expect.any(Number),
},
],
exception: {
values: [
{
type: 'Error',
value: 'foo',
},
],
},
},
})
.start(closeTestServer);
});
});
});
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export {
mysql2Integration,
mysqlIntegration,
nativeNodeFetchIntegration,
fetchBreadcrumbsIntegration,
// eslint-disable-next-line deprecation/deprecation
nestIntegration,
NodeClient,
Expand Down
1 change: 1 addition & 0 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export {
consoleIntegration,
httpIntegration,
nativeNodeFetchIntegration,
fetchBreadcrumbsIntegration,
onUncaughtExceptionIntegration,
onUnhandledRejectionIntegration,
modulesIntegration,
Expand Down
1 change: 1 addition & 0 deletions packages/google-cloud-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export {
consoleIntegration,
httpIntegration,
nativeNodeFetchIntegration,
fetchBreadcrumbsIntegration,
onUncaughtExceptionIntegration,
onUnhandledRejectionIntegration,
modulesIntegration,
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export { dataloaderIntegration } from './integrations/tracing/dataloader';
export { amqplibIntegration } from './integrations/tracing/amqplib';
// eslint-disable-next-line deprecation/deprecation
export { processThreadBreadcrumbIntegration, childProcessIntegration } from './integrations/childProcess';
export { fetchBreadcrumbsIntegration } from './integrations/fetch-breadcrumbs';

export { SentryContextManager } from './otel/contextManager';
export { generateInstrumentOnce } from './otel/instrument';
Expand Down
122 changes: 122 additions & 0 deletions packages/node/src/integrations/fetch-breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import type { UndiciRequest, UndiciResponse } from '@opentelemetry/instrumentation-undici';
import type { Integration, IntegrationFn, SanitizedRequestData } from '@sentry/core';
import {
addBreadcrumb,
defineIntegration,
getBreadcrumbLogLevelFromHttpStatusCode,
getSanitizedUrlString,
parseUrl,
} from '@sentry/core';

import * as diagnosticsChannel from 'diagnostics_channel';
import type { NodeClient } from '../sdk/client';

type OldIntegration = Integration & { breadcrumbsDisabled: boolean };

interface NodeFetchOptions {
/**
* Do not capture breadcrumbs for outgoing fetch requests to URLs where the given callback returns `true`.
*/
ignore?: (url: string) => boolean;
}

const _fetchBreadcrumbsIntegration = ((options: NodeFetchOptions = {}) => {
function onRequestHeaders({ request, response }: { request: UndiciRequest; response: UndiciResponse }): void {
if (options.ignore) {
const url = getAbsoluteUrl(request.origin, request.path);
const shouldIgnore = options.ignore(url);

if (shouldIgnore) {
return;
}
}

addRequestBreadcrumb(request, response);
}

return {
name: 'FetchBreadcrumbs',
setup: (client: NodeClient) => {
if (client.getOptions().fetchBreadcrumbs === false) {
return;
}

// We need to ensure all other integrations have been setup first
setImmediate(() => {
const oldIntegration = client.getIntegrationByName<OldIntegration>('NodeFetch');
if (oldIntegration?.breadcrumbsDisabled) {
return;
}

diagnosticsChannel
.channel('undici:request:headers')
// eslint-disable-next-line deprecation/deprecation
.subscribe(onRequestHeaders as diagnosticsChannel.ChannelListener);
});
},
};
}) satisfies IntegrationFn;

export const fetchBreadcrumbsIntegration = defineIntegration(_fetchBreadcrumbsIntegration);

/** Add a breadcrumb for outgoing requests. */
function addRequestBreadcrumb(request: UndiciRequest, response: UndiciResponse): void {
const data = getBreadcrumbData(request);
const statusCode = response.statusCode;
const level = getBreadcrumbLogLevelFromHttpStatusCode(statusCode);

addBreadcrumb(
{
category: 'http',
data: {
status_code: statusCode,
...data,
},
type: 'http',
level,
},
{
event: 'response',
request,
response,
},
);
}

function getBreadcrumbData(request: UndiciRequest): Partial<SanitizedRequestData> {
try {
const url = new URL(request.path, request.origin);
const parsedUrl = parseUrl(url.toString());

const data: Partial<SanitizedRequestData> = {
url: getSanitizedUrlString(parsedUrl),
'http.method': request.method || 'GET',
};

if (parsedUrl.search) {
data['http.query'] = parsedUrl.search;
}
if (parsedUrl.hash) {
data['http.fragment'] = parsedUrl.hash;
}

return data;
} catch {
return {};
}
}

// Matching the behavior of the base instrumentation
function getAbsoluteUrl(origin: string, path: string = '/'): string {
const url = `${origin}`;

if (url.endsWith('/') && path.startsWith('/')) {
return `${url}${path.slice(1)}`;
}

if (!url.endsWith('/') && !path.startsWith('/')) {
return `${url}/${path.slice(1)}`;
}

return `${url}${path}`;
}
73 changes: 13 additions & 60 deletions packages/node/src/integrations/node-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import type { UndiciRequest, UndiciResponse } from '@opentelemetry/instrumentation-undici';
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
import type { IntegrationFn, SanitizedRequestData } from '@sentry/core';
import type { IntegrationFn } from '@sentry/core';
import {
LRUMap,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
addBreadcrumb,
defineIntegration,
getBreadcrumbLogLevelFromHttpStatusCode,
getClient,
getSanitizedUrlString,
getTraceData,
hasTracingEnabled,
parseUrl,
} from '@sentry/core';
import { shouldPropagateTraceForUrl } from '@sentry/opentelemetry';

interface NodeFetchOptions {
/**
* @deprecated Use `fetchBreadcrumbs` init option instead.
* ```js
* Sentry.init({
* dsn: '__DSN__',
* fetchBreadcrumbs: false,
* })
* ```
*
* Whether breadcrumbs should be recorded for requests.
* Defaults to true
*
* Defaults to `true`
*/
breadcrumbs?: boolean;

Expand All @@ -31,7 +35,6 @@ interface NodeFetchOptions {
}

const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => {
const _breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs;
const _ignoreOutgoingRequests = options.ignoreOutgoingRequests;

return {
Expand Down Expand Up @@ -77,67 +80,17 @@ const _nativeNodeFetchIntegration = ((options: NodeFetchOptions = {}) => {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.node_fetch',
};
},
responseHook: (_, { request, response }) => {
if (_breadcrumbs) {
addRequestBreadcrumb(request, response);
}
},
});

registerInstrumentations({ instrumentations: [instrumentation] });
},
// eslint-disable-next-line deprecation/deprecation
breadcrumbsDisabled: options.breadcrumbs === false,
};
}) satisfies IntegrationFn;

export const nativeNodeFetchIntegration = defineIntegration(_nativeNodeFetchIntegration);

/** Add a breadcrumb for outgoing requests. */
function addRequestBreadcrumb(request: UndiciRequest, response: UndiciResponse): void {
const data = getBreadcrumbData(request);
const statusCode = response.statusCode;
const level = getBreadcrumbLogLevelFromHttpStatusCode(statusCode);

addBreadcrumb(
{
category: 'http',
data: {
status_code: statusCode,
...data,
},
type: 'http',
level,
},
{
event: 'response',
request,
response,
},
);
}

function getBreadcrumbData(request: UndiciRequest): Partial<SanitizedRequestData> {
try {
const url = new URL(request.path, request.origin);
const parsedUrl = parseUrl(url.toString());

const data: Partial<SanitizedRequestData> = {
url: getSanitizedUrlString(parsedUrl),
'http.method': request.method || 'GET',
};

if (parsedUrl.search) {
data['http.query'] = parsedUrl.search;
}
if (parsedUrl.hash) {
data['http.fragment'] = parsedUrl.hash;
}

return data;
} catch {
return {};
}
}

// Matching the behavior of the base instrumentation
function getAbsoluteUrl(origin: string, path: string = '/'): string {
const url = `${origin}`;
Expand Down
Loading
Loading