Skip to content

Commit 9528b55

Browse files
committed
small refs
1 parent 6b4ad58 commit 9528b55

File tree

2 files changed

+80
-75
lines changed

2 files changed

+80
-75
lines changed

packages/node/src/integrations/http/index.ts

+78-74
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,13 @@ interface HttpOptions {
8282
};
8383
}
8484

85-
const instrumentSentryHttp = generateInstrumentOnce<{ breadcrumbs?: boolean }>(
85+
export const instrumentSentryHttp = generateInstrumentOnce<{ breadcrumbs?: boolean }>(
8686
`${INTEGRATION_NAME}.sentry`,
8787
options => {
8888
return new SentryHttpInstrumentation({ breadcrumbs: options?.breadcrumbs });
8989
},
9090
);
9191

92-
/**
93-
* We only preload this one.
94-
* If we preload both this and `instrumentSentryHttp`, it leads to weird issues with instrumentation.
95-
*/
9692
export const instrumentOtelHttp = generateInstrumentOnce<HttpInstrumentationConfig>(INTEGRATION_NAME, config => {
9793
const instrumentation = new HttpInstrumentation(config);
9894

@@ -111,82 +107,18 @@ export const instrumentOtelHttp = generateInstrumentOnce<HttpInstrumentationConf
111107
});
112108

113109
/**
114-
* Instrument the HTTP module.
110+
* Instrument the HTTP and HTTPS modules.
115111
*/
116112
const instrumentHttp = (options: HttpOptions = {}): void => {
117113
// This is the "regular" OTEL instrumentation that emits spans
118114
if (options.spans !== false) {
119-
const instrumentationConfig = {
120-
...options.instrumentation?._experimentalConfig,
121-
122-
disableIncomingRequestInstrumentation: options.disableIncomingRequestSpans,
123-
124-
ignoreOutgoingRequestHook: request => {
125-
const url = getRequestUrl(request);
126-
127-
if (!url) {
128-
return false;
129-
}
130-
131-
const _ignoreOutgoingRequests = options.ignoreOutgoingRequests;
132-
if (_ignoreOutgoingRequests && _ignoreOutgoingRequests(url, request)) {
133-
return true;
134-
}
135-
136-
return false;
137-
},
138-
139-
ignoreIncomingRequestHook: request => {
140-
// request.url is the only property that holds any information about the url
141-
// it only consists of the URL path and query string (if any)
142-
const urlPath = request.url;
143-
144-
const method = request.method?.toUpperCase();
145-
// We do not capture OPTIONS/HEAD requests as transactions
146-
if (method === 'OPTIONS' || method === 'HEAD') {
147-
return true;
148-
}
149-
150-
const _ignoreIncomingRequests = options.ignoreIncomingRequests;
151-
if (urlPath && _ignoreIncomingRequests && _ignoreIncomingRequests(urlPath, request)) {
152-
return true;
153-
}
154-
155-
return false;
156-
},
157-
158-
requireParentforOutgoingSpans: false,
159-
requireParentforIncomingSpans: false,
160-
requestHook: (span, req) => {
161-
addOriginToSpan(span, 'auto.http.otel.http');
162-
if (!_isClientRequest(req) && isKnownPrefetchRequest(req)) {
163-
span.setAttribute('sentry.http.prefetch', true);
164-
}
165-
166-
options.instrumentation?.requestHook?.(span, req);
167-
},
168-
responseHook: (span, res) => {
169-
const client = getClient<NodeClient>();
170-
if (client && client.getOptions().autoSessionTracking) {
171-
setImmediate(() => {
172-
client['_captureRequestSession']();
173-
});
174-
}
175-
176-
options.instrumentation?.responseHook?.(span, res);
177-
},
178-
applyCustomAttributesOnSpan: (
179-
span: Span,
180-
request: ClientRequest | HTTPModuleRequestIncomingMessage,
181-
response: HTTPModuleRequestIncomingMessage | ServerResponse,
182-
) => {
183-
options.instrumentation?.applyCustomAttributesOnSpan?.(span, request, response);
184-
},
185-
} satisfies HttpInstrumentationConfig;
186-
115+
const instrumentationConfig = getConfigWithDefaults(options);
187116
instrumentOtelHttp(instrumentationConfig);
188117
}
189118

119+
// This is the Sentry-specific instrumentation that isolates requests & creates breadcrumbs
120+
// Note that this _has_ to be wrapped after the OTEL instrumentation,
121+
// otherwise the isolation will not work correctly
190122
instrumentSentryHttp(options);
191123
};
192124

@@ -221,3 +153,75 @@ function isKnownPrefetchRequest(req: HTTPModuleRequestIncomingMessage): boolean
221153
// Currently only handles Next.js prefetch requests but may check other frameworks in the future.
222154
return req.headers['next-router-prefetch'] === '1';
223155
}
156+
157+
function getConfigWithDefaults(options: Partial<HttpOptions> = {}): HttpInstrumentationConfig {
158+
const instrumentationConfig = {
159+
...options.instrumentation?._experimentalConfig,
160+
161+
disableIncomingRequestInstrumentation: options.disableIncomingRequestSpans,
162+
163+
ignoreOutgoingRequestHook: request => {
164+
const url = getRequestUrl(request);
165+
166+
if (!url) {
167+
return false;
168+
}
169+
170+
const _ignoreOutgoingRequests = options.ignoreOutgoingRequests;
171+
if (_ignoreOutgoingRequests && _ignoreOutgoingRequests(url, request)) {
172+
return true;
173+
}
174+
175+
return false;
176+
},
177+
178+
ignoreIncomingRequestHook: request => {
179+
// request.url is the only property that holds any information about the url
180+
// it only consists of the URL path and query string (if any)
181+
const urlPath = request.url;
182+
183+
const method = request.method?.toUpperCase();
184+
// We do not capture OPTIONS/HEAD requests as transactions
185+
if (method === 'OPTIONS' || method === 'HEAD') {
186+
return true;
187+
}
188+
189+
const _ignoreIncomingRequests = options.ignoreIncomingRequests;
190+
if (urlPath && _ignoreIncomingRequests && _ignoreIncomingRequests(urlPath, request)) {
191+
return true;
192+
}
193+
194+
return false;
195+
},
196+
197+
requireParentforOutgoingSpans: false,
198+
requireParentforIncomingSpans: false,
199+
requestHook: (span, req) => {
200+
addOriginToSpan(span, 'auto.http.otel.http');
201+
if (!_isClientRequest(req) && isKnownPrefetchRequest(req)) {
202+
span.setAttribute('sentry.http.prefetch', true);
203+
}
204+
205+
options.instrumentation?.requestHook?.(span, req);
206+
},
207+
responseHook: (span, res) => {
208+
const client = getClient<NodeClient>();
209+
if (client && client.getOptions().autoSessionTracking) {
210+
setImmediate(() => {
211+
client['_captureRequestSession']();
212+
});
213+
}
214+
215+
options.instrumentation?.responseHook?.(span, res);
216+
},
217+
applyCustomAttributesOnSpan: (
218+
span: Span,
219+
request: ClientRequest | HTTPModuleRequestIncomingMessage,
220+
response: HTTPModuleRequestIncomingMessage | ServerResponse,
221+
) => {
222+
options.instrumentation?.applyCustomAttributesOnSpan?.(span, request, response);
223+
},
224+
} satisfies HttpInstrumentationConfig;
225+
226+
return instrumentationConfig;
227+
}

packages/node/src/integrations/tracing/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Integration } from '@sentry/types';
2-
import { instrumentOtelHttp } from '../http';
2+
import { instrumentOtelHttp, instrumentSentryHttp } from '../http';
33

44
import { amqplibIntegration, instrumentAmqplib } from './amqplib';
55
import { connectIntegration, instrumentConnect } from './connect';
@@ -55,6 +55,7 @@ export function getAutoPerformanceIntegrations(): Integration[] {
5555
export function getOpenTelemetryInstrumentationToPreload(): (((options?: any) => void) & { id: string })[] {
5656
return [
5757
instrumentOtelHttp,
58+
instrumentSentryHttp,
5859
instrumentExpress,
5960
instrumentConnect,
6061
instrumentFastify,

0 commit comments

Comments
 (0)