Skip to content

Commit cc1cb7b

Browse files
authored
feat(node): Ensure manual OTEL setup works (#12214)
Based on the great feedback in #12191, this does some small adjustments to ensure that you actually can use the Node SDK properly with a custom OTEL setup: ## 1. Ensure we do not run `validateOpenTelemetrySetup()` when `skipOpenTelemetrySetup` is configured. Today, this is impossible to fix because you need a client to create the sampler, and you need the sampler to satisfy the validation, but the validation runs in `init()`. So either you call init() before doing your manual setup, which means you get the warning, or you do the manual setup first, but then you can't actually add the sampler, and still get the warning. This change means that users that configure `skipOpenTelemetrySetup` can manually call `Sentry.validateOpenTelemetrySetup()` if they want to get the validation, else there will be no validation automtically. ## 2. Export `SentryContextManager` from `@sentry/node` This is easier to use than to use the primitive `wrapContextManagerClass` from `@sentry/opentelemetry`. ## 3. Ensure we always run `setupEventContextTrace`, not tied to `skipOpenTelemetrySetup` There is no reason to tie this together, this now just always runs in `init()` - it's just an event processor!
1 parent 8fa393c commit cc1cb7b

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

dev-packages/e2e-tests/test-applications/node-exports-test-app/scripts/consistentExports.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const NODE_EXPORTS_IGNORE = [
1818
'setNodeAsyncContextStrategy',
1919
'getDefaultIntegrationsWithoutPerformance',
2020
'initWithoutDefaultIntegrations',
21+
'SentryContextManager',
22+
'validateOpenTelemetrySetup',
2123
];
2224

2325
const nodeExports = Object.keys(SentryNode).filter(e => !NODE_EXPORTS_IGNORE.includes(e));

packages/node/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ export { koaIntegration, setupKoaErrorHandler } from './integrations/tracing/koa
2626
export { connectIntegration, setupConnectErrorHandler } from './integrations/tracing/connect';
2727
export { spotlightIntegration } from './integrations/spotlight';
2828

29+
export { SentryContextManager } from './otel/contextManager';
2930
export {
3031
init,
3132
getDefaultIntegrations,
3233
getDefaultIntegrationsWithoutPerformance,
3334
initWithoutDefaultIntegrations,
35+
validateOpenTelemetrySetup,
3436
} from './sdk/init';
3537
export { initOpenTelemetry } from './sdk/initOtel';
3638
export { getAutoPerformanceIntegrations } from './integrations/tracing';

packages/node/src/sdk/init.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import {
1111
requestDataIntegration,
1212
startSession,
1313
} from '@sentry/core';
14-
import { openTelemetrySetupCheck, setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
14+
import {
15+
openTelemetrySetupCheck,
16+
setOpenTelemetryContextAsyncContextStrategy,
17+
setupEventContextTrace,
18+
} from '@sentry/opentelemetry';
1519
import type { Client, Integration, Options } from '@sentry/types';
1620
import {
1721
GLOBAL_OBJ,
@@ -196,12 +200,16 @@ function _init(
196200
// There is no way to use this SDK without OpenTelemetry!
197201
if (!options.skipOpenTelemetrySetup) {
198202
initOpenTelemetry(client);
203+
validateOpenTelemetrySetup();
199204
}
200205

201-
validateOpenTelemetrySetup();
206+
setupEventContextTrace(client);
202207
}
203208

204-
function validateOpenTelemetrySetup(): void {
209+
/**
210+
* Validate that your OpenTelemetry setup is correct.
211+
*/
212+
export function validateOpenTelemetrySetup(): void {
205213
if (!DEBUG_BUILD) {
206214
return;
207215
}

packages/node/src/sdk/initOtel.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
SEMRESATTRS_SERVICE_VERSION,
88
} from '@opentelemetry/semantic-conventions';
99
import { SDK_VERSION } from '@sentry/core';
10-
import { SentryPropagator, SentrySampler, SentrySpanProcessor, setupEventContextTrace } from '@sentry/opentelemetry';
10+
import { SentryPropagator, SentrySampler, SentrySpanProcessor } from '@sentry/opentelemetry';
1111
import { logger } from '@sentry/utils';
1212

1313
import { SentryContextManager } from '../otel/contextManager';
@@ -28,8 +28,6 @@ export function initOpenTelemetry(client: NodeClient): void {
2828
diag.setLogger(otelLogger, DiagLogLevel.DEBUG);
2929
}
3030

31-
setupEventContextTrace(client);
32-
3331
const provider = setupOtel(client);
3432
client.traceProvider = provider;
3533
}

0 commit comments

Comments
 (0)