Skip to content

Commit 6341794

Browse files
mydealforst
authored andcommitted
feat(node)!: Avoid http spans by default for custom OTEL setups (#14678)
With this PR, the default value for the `spans` option in the `httpIntegration` is changed to `false`, if `skipOpenTelemetrySetup: true` is configured. This is what you'd expect as a user, you do not want Sentry to register any OTEL instrumentation and emit any spans in this scenario. Closes #14675
1 parent 4b4f9d6 commit 6341794

File tree

7 files changed

+40
-4
lines changed

7 files changed

+40
-4
lines changed

dev-packages/e2e-tests/test-applications/node-otel-custom-sampler/src/instrument.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Sentry.init({
1414
skipOpenTelemetrySetup: true,
1515
// By defining _any_ sample rate, tracing integrations will be added by default
1616
tracesSampleRate: 0,
17+
integrations: [Sentry.httpIntegration({ spans: true })],
1718
});
1819

1920
const provider = new NodeTracerProvider({

dev-packages/e2e-tests/test-applications/node-otel-sdk-node/src/instrument.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const sentryClient = Sentry.init({
1414
tracesSampleRate: 1,
1515

1616
skipOpenTelemetrySetup: true,
17+
integrations: [Sentry.httpIntegration({ spans: true })],
1718
});
1819

1920
const sdk = new opentelemetry.NodeSDK({

dev-packages/e2e-tests/test-applications/node-otel-without-tracing/src/instrument.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ Sentry.init({
1515
debug: !!process.env.DEBUG,
1616
tunnel: `http://localhost:3031/`, // proxy server
1717
// Tracing is completely disabled
18-
19-
integrations: [Sentry.httpIntegration({ spans: false })],
20-
2118
// Custom OTEL setup
2219
skipOpenTelemetrySetup: true,
2320
});

docs/migration/draft-v9-migration-guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,4 @@
158158
- Deprecated `addOpenTelemetryInstrumentation`. Use the `openTelemetryInstrumentations` option in `Sentry.init()` or your custom Sentry Client instead.
159159
- Deprecated `registerEsmLoaderHooks.include` and `registerEsmLoaderHooks.exclude`. Set `onlyIncludeInstrumentedModules: true` instead.
160160
- `registerEsmLoaderHooks` will only accept `true | false | undefined` in the future. The SDK will default to wrapping modules that are used as part of OpenTelemetry Instrumentation.
161+
- `httpIntegration({ spans: false })` is configured by default if `skipOpenTelemetrySetup: true` is set. You can still overwrite this if desired.

docs/migration/v8-to-v9.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ If you need to support older browsers, we recommend transpiling your code using
7878

7979
## 2. Behavior Changes
8080

81+
### `@sentry/node`
82+
83+
- When `skipOpenTelemetrySetup: true` is configured, `httpIntegration({ spans: false })` will be configured by default. This means that you no longer have to specify this yourself in this scenario. With this change, no spans are emitted once `skipOpenTelemetrySetup: true` is configured, without any further configuration being needed.
84+
85+
### Uncategorized (TODO)
86+
8187
- Next.js withSentryConfig returning Promise
8288
- `request` on sdk processing metadata will be ignored going forward
8389
- respect sourcemap generation settings

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getClient } from '@sentry/opentelemetry';
88
import { generateInstrumentOnce } from '../../otel/instrument';
99
import type { NodeClient } from '../../sdk/client';
1010
import type { HTTPModuleRequestIncomingMessage } from '../../transports/http-module';
11+
import type { NodeClientOptions } from '../../types';
1112
import { addOriginToSpan } from '../../utils/addOriginToSpan';
1213
import { getRequestUrl } from '../../utils/getRequestUrl';
1314
import { SentryHttpInstrumentation } from './SentryHttpInstrumentation';
@@ -27,6 +28,8 @@ interface HttpOptions {
2728
* If set to false, do not emit any spans.
2829
* This will ensure that the default HttpInstrumentation from OpenTelemetry is not setup,
2930
* only the Sentry-specific instrumentation for request isolation is applied.
31+
*
32+
* If `skipOpenTelemetrySetup: true` is configured, this defaults to `false`, otherwise it defaults to `true`.
3033
*/
3134
spans?: boolean;
3235

@@ -118,12 +121,21 @@ export const instrumentOtelHttp = generateInstrumentOnce<HttpInstrumentationConf
118121
return instrumentation;
119122
});
120123

124+
/** Exported only for tests. */
125+
export function _shouldInstrumentSpans(options: HttpOptions, clientOptions: Partial<NodeClientOptions> = {}): boolean {
126+
// If `spans` is passed in, it takes precedence
127+
// Else, we by default emit spans, unless `skipOpenTelemetrySetup` is set to `true`
128+
return typeof options.spans === 'boolean' ? options.spans : !clientOptions.skipOpenTelemetrySetup;
129+
}
130+
121131
/**
122132
* Instrument the HTTP and HTTPS modules.
123133
*/
124134
const instrumentHttp = (options: HttpOptions = {}): void => {
135+
const instrumentSpans = _shouldInstrumentSpans(options, getClient<NodeClient>()?.getOptions());
136+
125137
// This is the "regular" OTEL instrumentation that emits spans
126-
if (options.spans !== false) {
138+
if (instrumentSpans) {
127139
const instrumentationConfig = getConfigWithDefaults(options);
128140
instrumentOtelHttp(instrumentationConfig);
129141
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { _shouldInstrumentSpans } from '../../src/integrations/http';
2+
3+
describe('httpIntegration', () => {
4+
describe('_shouldInstrumentSpans', () => {
5+
it.each([
6+
[{}, {}, true],
7+
[{ spans: true }, {}, true],
8+
[{ spans: false }, {}, false],
9+
[{ spans: true }, { skipOpenTelemetrySetup: true }, true],
10+
[{ spans: false }, { skipOpenTelemetrySetup: true }, false],
11+
[{}, { skipOpenTelemetrySetup: true }, false],
12+
[{}, { skipOpenTelemetrySetup: false }, true],
13+
])('returns the correct value for options=%p and clientOptions=%p', (options, clientOptions, expected) => {
14+
const actual = _shouldInstrumentSpans(options, clientOptions);
15+
expect(actual).toBe(expected);
16+
});
17+
});
18+
});

0 commit comments

Comments
 (0)