Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): Add trackIncomingRequestsAsSessions option to http integration #14567

Merged
merged 2 commits into from
Dec 4, 2024
Merged
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
33 changes: 23 additions & 10 deletions packages/node/src/integrations/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 be ignored.
*/
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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -207,7 +215,12 @@ function getConfigWithDefaults(options: Partial<HttpOptions> = {}): HttpInstrume
},
responseHook: (span, res) => {
const client = getClient<NodeClient>();
if (client && client.getOptions().autoSessionTracking) {

if (
client &&
client.getOptions().autoSessionTracking !== false &&
options.trackIncomingRequestsAsSessions !== false
) {
setImmediate(() => {
client['_captureRequestSession']();
});
Expand Down
Loading