Skip to content

Commit 8b24731

Browse files
authored
feat(node): Add request parameter to httpIntegration ignore callbacks (#12930)
Add a second parameter (`request`) to the `httpIntegration`'s `ignoreIncomingRequests` and `ignoreOutgoingRequests` callbacks. This allows users to not only filter on the URL as previously but on the entire request objects.
1 parent bcebd87 commit 8b24731

File tree

4 files changed

+144
-52
lines changed

4 files changed

+144
-52
lines changed

dev-packages/node-integration-tests/suites/tracing/httpIntegration/server-ignoreIncomingRequests.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ Sentry.init({
99

1010
integrations: [
1111
Sentry.httpIntegration({
12-
ignoreIncomingRequests: url => {
13-
return url.includes('/liveness');
12+
ignoreIncomingRequests: (url, request) => {
13+
if (url.includes('/liveness')) {
14+
return true;
15+
}
16+
if (request.method === 'POST' && request.url.includes('readiness')) {
17+
return true;
18+
}
19+
return false;
1420
},
1521
}),
1622
],
@@ -33,6 +39,10 @@ app.get('/liveness', (_req, res) => {
3339
res.send({ response: 'liveness' });
3440
});
3541

42+
app.post('/readiness', (_req, res) => {
43+
res.send({ response: 'readiness' });
44+
});
45+
3646
Sentry.setupExpressErrorHandler(app);
3747

3848
startExpressServerAndSendPortToRunner(app);

dev-packages/node-integration-tests/suites/tracing/httpIntegration/server-ignoreOutgoingRequests.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ Sentry.init({
1010

1111
integrations: [
1212
Sentry.httpIntegration({
13-
ignoreOutgoingRequests: url => {
14-
return url.includes('example.com');
13+
ignoreOutgoingRequests: (url, request) => {
14+
if (url.includes('example.com')) {
15+
return true;
16+
}
17+
if (request.method === 'POST' && request.path === '/path') {
18+
return true;
19+
}
20+
return false;
1521
},
1622
}),
1723
],
@@ -37,6 +43,17 @@ app.get('/test', (_req, response) => {
3743
.end();
3844
});
3945

46+
app.post('/testPath', (_req, response) => {
47+
http
48+
.request('http://example.com/path', res => {
49+
res.on('data', () => {});
50+
res.on('end', () => {
51+
response.send({ response: 'done' });
52+
});
53+
})
54+
.end();
55+
});
56+
4057
Sentry.setupExpressErrorHandler(app);
4158

4259
startExpressServerAndSendPortToRunner(app);

dev-packages/node-integration-tests/suites/tracing/httpIntegration/test.ts

Lines changed: 102 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -76,58 +76,117 @@ describe('httpIntegration', () => {
7676
.makeRequest('get', '/test');
7777
});
7878

79-
test("doesn't create a root span for incoming requests ignored via `ignoreIncomingRequests`", done => {
80-
const runner = createRunner(__dirname, 'server-ignoreIncomingRequests.js')
81-
.expect({
82-
transaction: {
83-
contexts: {
84-
trace: {
85-
span_id: expect.any(String),
86-
trace_id: expect.any(String),
87-
data: {
88-
url: expect.stringMatching(/\/test$/),
89-
'http.response.status_code': 200,
79+
describe("doesn't create a root span for incoming requests ignored via `ignoreIncomingRequests`", () => {
80+
test('via the url param', done => {
81+
const runner = createRunner(__dirname, 'server-ignoreIncomingRequests.js')
82+
.expect({
83+
transaction: {
84+
contexts: {
85+
trace: {
86+
span_id: expect.any(String),
87+
trace_id: expect.any(String),
88+
data: {
89+
url: expect.stringMatching(/\/test$/),
90+
'http.response.status_code': 200,
91+
},
92+
op: 'http.server',
93+
status: 'ok',
9094
},
91-
op: 'http.server',
92-
status: 'ok',
9395
},
96+
transaction: 'GET /test',
9497
},
95-
transaction: 'GET /test',
96-
},
97-
})
98-
.start(done);
98+
})
99+
.start(done);
100+
101+
runner.makeRequest('get', '/liveness'); // should be ignored
102+
runner.makeRequest('get', '/test');
103+
});
104+
105+
test('via the request param', done => {
106+
const runner = createRunner(__dirname, 'server-ignoreIncomingRequests.js')
107+
.expect({
108+
transaction: {
109+
contexts: {
110+
trace: {
111+
span_id: expect.any(String),
112+
trace_id: expect.any(String),
113+
data: {
114+
url: expect.stringMatching(/\/test$/),
115+
'http.response.status_code': 200,
116+
},
117+
op: 'http.server',
118+
status: 'ok',
119+
},
120+
},
121+
transaction: 'GET /test',
122+
},
123+
})
124+
.start(done);
99125

100-
runner.makeRequest('get', '/liveness'); // should be ignored
101-
runner.makeRequest('get', '/test');
126+
runner.makeRequest('post', '/readiness'); // should be ignored
127+
runner.makeRequest('get', '/test');
128+
});
102129
});
103130

104-
test("doesn't create child spans for outgoing requests ignored via `ignoreOutgoingRequests`", done => {
105-
const runner = createRunner(__dirname, 'server-ignoreOutgoingRequests.js')
106-
.expect({
107-
transaction: {
108-
contexts: {
109-
trace: {
110-
span_id: expect.any(String),
111-
trace_id: expect.any(String),
112-
data: {
113-
url: expect.stringMatching(/\/test$/),
114-
'http.response.status_code': 200,
131+
describe("doesn't create child spans for outgoing requests ignored via `ignoreOutgoingRequests`", () => {
132+
test('via the url param', done => {
133+
const runner = createRunner(__dirname, 'server-ignoreOutgoingRequests.js')
134+
.expect({
135+
transaction: {
136+
contexts: {
137+
trace: {
138+
span_id: expect.any(String),
139+
trace_id: expect.any(String),
140+
data: {
141+
url: expect.stringMatching(/\/test$/),
142+
'http.response.status_code': 200,
143+
},
144+
op: 'http.server',
145+
status: 'ok',
115146
},
116-
op: 'http.server',
117-
status: 'ok',
118147
},
148+
transaction: 'GET /test',
149+
spans: [
150+
expect.objectContaining({ op: 'middleware.express', description: 'query' }),
151+
expect.objectContaining({ op: 'middleware.express', description: 'expressInit' }),
152+
expect.objectContaining({ op: 'middleware.express', description: 'corsMiddleware' }),
153+
expect.objectContaining({ op: 'request_handler.express', description: '/test' }),
154+
],
119155
},
120-
transaction: 'GET /test',
121-
spans: [
122-
expect.objectContaining({ op: 'middleware.express', description: 'query' }),
123-
expect.objectContaining({ op: 'middleware.express', description: 'expressInit' }),
124-
expect.objectContaining({ op: 'middleware.express', description: 'corsMiddleware' }),
125-
expect.objectContaining({ op: 'request_handler.express', description: '/test' }),
126-
],
127-
},
128-
})
129-
.start(done);
156+
})
157+
.start(done);
158+
159+
runner.makeRequest('get', '/test');
160+
});
161+
162+
test('via the request param', done => {
163+
const runner = createRunner(__dirname, 'server-ignoreOutgoingRequests.js')
164+
.expect({
165+
transaction: {
166+
contexts: {
167+
trace: {
168+
span_id: expect.any(String),
169+
trace_id: expect.any(String),
170+
data: {
171+
url: expect.stringMatching(/\/testPath$/),
172+
'http.response.status_code': 200,
173+
},
174+
op: 'http.server',
175+
status: 'ok',
176+
},
177+
},
178+
transaction: 'POST /testPath',
179+
spans: [
180+
expect.objectContaining({ op: 'middleware.express', description: 'query' }),
181+
expect.objectContaining({ op: 'middleware.express', description: 'expressInit' }),
182+
expect.objectContaining({ op: 'middleware.express', description: 'corsMiddleware' }),
183+
expect.objectContaining({ op: 'request_handler.express', description: '/testPath' }),
184+
],
185+
},
186+
})
187+
.start(done);
130188

131-
runner.makeRequest('get', '/test');
189+
runner.makeRequest('post', '/testPath');
190+
});
132191
});
133192
});

packages/node/src/integrations/http.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ClientRequest, ServerResponse } from 'node:http';
1+
import type { ClientRequest, IncomingMessage, RequestOptions, ServerResponse } from 'node:http';
22
import type { Span } from '@opentelemetry/api';
33
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
44
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
@@ -37,17 +37,23 @@ interface HttpOptions {
3737
*
3838
* The `url` param contains the entire URL, including query string (if any), protocol, host, etc. of the outgoing request.
3939
* For example: `'https://someService.com/users/details?id=123'`
40+
*
41+
* The `request` param contains the original {@type RequestOptions} object used to make the outgoing request.
42+
* You can use it to filter on additional properties like method, headers, etc.
4043
*/
41-
ignoreOutgoingRequests?: (url: string) => boolean;
44+
ignoreOutgoingRequests?: (url: string, request: RequestOptions) => boolean;
4245

4346
/**
4447
* Do not capture spans or breadcrumbs for incoming HTTP requests to URLs where the given callback returns `true`.
4548
* This controls both span & breadcrumb creation - spans will be non recording if tracing is disabled.
4649
*
4750
* The `urlPath` param consists of the URL path and query string (if any) of the incoming request.
4851
* For example: `'/users/details?id=123'`
52+
*
53+
* The `request` param contains the original {@type IncomingMessage} object of the incoming request.
54+
* You can use it to filter on additional properties like method, headers, etc.
4955
*/
50-
ignoreIncomingRequests?: (urlPath: string) => boolean;
56+
ignoreIncomingRequests?: (urlPath: string, request: IncomingMessage) => boolean;
5157

5258
/**
5359
* Additional instrumentation options that are passed to the underlying HttpInstrumentation.
@@ -101,7 +107,7 @@ export const instrumentHttp = Object.assign(
101107
}
102108

103109
const _ignoreOutgoingRequests = _httpOptions.ignoreOutgoingRequests;
104-
if (_ignoreOutgoingRequests && _ignoreOutgoingRequests(url)) {
110+
if (_ignoreOutgoingRequests && _ignoreOutgoingRequests(url, request)) {
105111
return true;
106112
}
107113

@@ -120,7 +126,7 @@ export const instrumentHttp = Object.assign(
120126
}
121127

122128
const _ignoreIncomingRequests = _httpOptions.ignoreIncomingRequests;
123-
if (urlPath && _ignoreIncomingRequests && _ignoreIncomingRequests(urlPath)) {
129+
if (urlPath && _ignoreIncomingRequests && _ignoreIncomingRequests(urlPath, request)) {
124130
return true;
125131
}
126132

0 commit comments

Comments
 (0)