Skip to content

Commit fcd2935

Browse files
authored
feat(node): Add trackIncomingRequestsAsSessions option to http integration (#14567)
1 parent 7f36772 commit fcd2935

File tree

1 file changed

+23
-10
lines changed
  • packages/node/src/integrations/http

1 file changed

+23
-10
lines changed

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

+23-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ClientRequest, IncomingMessage, RequestOptions, ServerResponse } f
22
import { diag } from '@opentelemetry/api';
33
import type { HttpInstrumentationConfig } from '@opentelemetry/instrumentation-http';
44
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
5-
import type { IntegrationFn, Span } from '@sentry/core';
5+
import type { Span } from '@sentry/core';
66
import { defineIntegration } from '@sentry/core';
77
import { getClient } from '@sentry/opentelemetry';
88
import { generateInstrumentOnce } from '../../otel/instrument';
@@ -30,6 +30,16 @@ interface HttpOptions {
3030
*/
3131
spans?: boolean;
3232

33+
/**
34+
* Whether the integration should create [Sessions](https://docs.sentry.io/product/releases/health/#sessions) for incoming requests to track the health and crash-free rate of your releases in Sentry.
35+
* Read more about Release Health: https://docs.sentry.io/product/releases/health/
36+
*
37+
* Defaults to `true`.
38+
*
39+
* Note: If `autoSessionTracking` is set to `false` in `Sentry.init()` or the Client owning this integration, this option will be ignored.
40+
*/
41+
trackIncomingRequestsAsSessions?: boolean;
42+
3343
/**
3444
* Do not capture spans or breadcrumbs for outgoing HTTP requests to URLs where the given callback returns `true`.
3545
* This controls both span & breadcrumb creation - spans will be non recording if tracing is disabled.
@@ -123,20 +133,18 @@ const instrumentHttp = (options: HttpOptions = {}): void => {
123133
instrumentSentryHttp(options);
124134
};
125135

126-
const _httpIntegration = ((options: HttpOptions = {}) => {
136+
/**
137+
* The http integration instruments Node's internal http and https modules.
138+
* It creates breadcrumbs and spans for outgoing HTTP requests which will be attached to the currently active span.
139+
*/
140+
export const httpIntegration = defineIntegration((options: HttpOptions = {}) => {
127141
return {
128142
name: INTEGRATION_NAME,
129143
setupOnce() {
130144
instrumentHttp(options);
131145
},
132146
};
133-
}) satisfies IntegrationFn;
134-
135-
/**
136-
* The http integration instruments Node's internal http and https modules.
137-
* It creates breadcrumbs and spans for outgoing HTTP requests which will be attached to the currently active span.
138-
*/
139-
export const httpIntegration = defineIntegration(_httpIntegration);
147+
});
140148

141149
/**
142150
* Determines if @param req is a ClientRequest, meaning the request was created within the express app
@@ -207,7 +215,12 @@ function getConfigWithDefaults(options: Partial<HttpOptions> = {}): HttpInstrume
207215
},
208216
responseHook: (span, res) => {
209217
const client = getClient<NodeClient>();
210-
if (client && client.getOptions().autoSessionTracking) {
218+
219+
if (
220+
client &&
221+
client.getOptions().autoSessionTracking !== false &&
222+
options.trackIncomingRequestsAsSessions !== false
223+
) {
211224
setImmediate(() => {
212225
client['_captureRequestSession']();
213226
});

0 commit comments

Comments
 (0)