Skip to content
Merged
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
@@ -0,0 +1,26 @@
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
tracesSampleRate: 1.0,
transport: loggingTransport,
integrations: [
Sentry.nativeNodeFetchIntegration({
requestHook: (span, req) => {
span.setAttribute('sentry.request.hook', req.path);
},
responseHook: (span, { response, request }) => {
span.setAttribute('sentry.response.hook.path', request.path);
span.setAttribute('sentry.response.hook.status_code', response.statusCode);
},
}),
],
});

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Sentry.startSpan({ name: 'test_transaction' }, async () => {
await fetch(`${process.env.SERVER_URL}/api/v0`);
await fetch(`${process.env.SERVER_URL}/api/v1`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expect, test } from 'vitest';
import { createRunner } from '../../../../utils/runner';
import { createTestServer } from '../../../../utils/server';

test('adds requestHook and responseHook attributes to spans of outgoing fetch requests', async () => {
expect.assertions(3);

const [SERVER_URL, closeTestServer] = await createTestServer()
.get('/api/v0', () => {
// Just ensure we're called
expect(true).toBe(true);
})
.get(
'/api/v1',
() => {
// Just ensure we're called
expect(true).toBe(true);
},
404,
)
.start();

await createRunner(__dirname, 'scenario.ts')
.withEnv({ SERVER_URL })
.expect({
transaction: {
transaction: 'test_transaction',
spans: [
expect.objectContaining({
description: expect.stringMatching(/GET .*\/api\/v0/),
op: 'http.client',
origin: 'auto.http.otel.node_fetch',
status: 'ok',
data: expect.objectContaining({
'sentry.request.hook': '/api/v0',
'sentry.response.hook.path': '/api/v0',
'sentry.response.hook.status_code': 200,
}),
}),
expect.objectContaining({
description: expect.stringMatching(/GET .*\/api\/v1/),
op: 'http.client',
origin: 'auto.http.otel.node_fetch',
status: 'not_found',
data: expect.objectContaining({
'sentry.request.hook': '/api/v1',
'sentry.response.hook.path': '/api/v1',
'sentry.response.hook.status_code': 404,
'http.response.status_code': 404,
}),
}),
],
},
})
.start()
.completed();
closeTestServer();
});
4 changes: 3 additions & 1 deletion packages/node/src/integrations/node-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { NodeClientOptions } from '../types';

const INTEGRATION_NAME = 'NodeFetch';

interface NodeFetchOptions {
interface NodeFetchOptions extends Pick<UndiciInstrumentationConfig, 'requestHook' | 'responseHook'> {
/**
* Whether breadcrumbs should be recorded for requests.
* Defaults to true
Expand Down Expand Up @@ -106,6 +106,8 @@ function getConfigWithDefaults(options: Partial<NodeFetchOptions> = {}): UndiciI
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.node_fetch',
};
},
requestHook: options.requestHook,
responseHook: options.responseHook,
} satisfies UndiciInstrumentationConfig;

return instrumentationConfig;
Expand Down
Loading