From 2c08f4db0664e14933bce23928a473234bc6c55e Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 4 Dec 2024 12:04:40 +0000 Subject: [PATCH 1/2] feat(node): Add `trackIncomingRequestsAsSessions` option to http integration --- packages/node/src/integrations/http/index.ts | 33 ++++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/node/src/integrations/http/index.ts b/packages/node/src/integrations/http/index.ts index 12b9564737a0..b9aa5f3635bc 100644 --- a/packages/node/src/integrations/http/index.ts +++ b/packages/node/src/integrations/http/index.ts @@ -2,7 +2,7 @@ import type { ClientRequest, IncomingMessage, RequestOptions, ServerResponse } f import { diag } from '@opentelemetry/api'; import type { HttpInstrumentationConfig } from '@opentelemetry/instrumentation-http'; import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; -import type { IntegrationFn, Span } from '@sentry/core'; +import type { Span } from '@sentry/core'; import { defineIntegration } from '@sentry/core'; import { getClient } from '@sentry/opentelemetry'; import { generateInstrumentOnce } from '../../otel/instrument'; @@ -30,6 +30,16 @@ interface HttpOptions { */ spans?: boolean; + /** + * 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. + * Read more about Release Health: https://docs.sentry.io/product/releases/health/ + * + * Defaults to `true`. + * + * Note: If `autoSessionTracking` is set to `false` in `Sentry.init()` or the Client owning this integration, this option will also default to `false`. + */ + trackIncomingRequestsAsSessions?: boolean; + /** * Do not capture spans or breadcrumbs for outgoing HTTP requests to URLs where the given callback returns `true`. * This controls both span & breadcrumb creation - spans will be non recording if tracing is disabled. @@ -123,20 +133,18 @@ const instrumentHttp = (options: HttpOptions = {}): void => { instrumentSentryHttp(options); }; -const _httpIntegration = ((options: HttpOptions = {}) => { +/** + * The http integration instruments Node's internal http and https modules. + * It creates breadcrumbs and spans for outgoing HTTP requests which will be attached to the currently active span. + */ +export const httpIntegration = defineIntegration((options: HttpOptions = {}) => { return { name: INTEGRATION_NAME, setupOnce() { instrumentHttp(options); }, }; -}) satisfies IntegrationFn; - -/** - * The http integration instruments Node's internal http and https modules. - * It creates breadcrumbs and spans for outgoing HTTP requests which will be attached to the currently active span. - */ -export const httpIntegration = defineIntegration(_httpIntegration); +}); /** * Determines if @param req is a ClientRequest, meaning the request was created within the express app @@ -207,7 +215,12 @@ function getConfigWithDefaults(options: Partial = {}): HttpInstrume }, responseHook: (span, res) => { const client = getClient(); - if (client && client.getOptions().autoSessionTracking) { + + if ( + client && + client.getOptions().autoSessionTracking !== false && + options.trackIncomingRequestsAsSessions !== false + ) { setImmediate(() => { client['_captureRequestSession'](); }); From f7a5c9cf08880bf131691e51dd33f680357f140b Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 4 Dec 2024 13:32:50 +0100 Subject: [PATCH 2/2] Update packages/node/src/integrations/http/index.ts Co-authored-by: Francesco Novy --- packages/node/src/integrations/http/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node/src/integrations/http/index.ts b/packages/node/src/integrations/http/index.ts index b9aa5f3635bc..10f0583b5ac6 100644 --- a/packages/node/src/integrations/http/index.ts +++ b/packages/node/src/integrations/http/index.ts @@ -36,7 +36,7 @@ interface HttpOptions { * * Defaults to `true`. * - * Note: If `autoSessionTracking` is set to `false` in `Sentry.init()` or the Client owning this integration, this option will also default to `false`. + * Note: If `autoSessionTracking` is set to `false` in `Sentry.init()` or the Client owning this integration, this option will be ignored. */ trackIncomingRequestsAsSessions?: boolean;