Skip to content

Commit b9c060a

Browse files
authored
fix(node): Export initOpenTelemetry (#11158)
Closes #11118 This PR exports what used to be called `initOtel` so that the `NodeClient` can be used without calling `init`. If OpenTelemetry is only required for performance feature, we should also only enable it if the sample rate is above zero to save memory.
1 parent 08b86e2 commit b9c060a

File tree

7 files changed

+19
-27
lines changed

7 files changed

+19
-27
lines changed

packages/aws-serverless/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export {
8888
hapiIntegration,
8989
setupHapiErrorHandler,
9090
spotlightIntegration,
91+
initOpenTelemetry,
9192
} from '@sentry/node';
9293

9394
export {

packages/bun/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export {
109109
hapiIntegration,
110110
setupHapiErrorHandler,
111111
spotlightIntegration,
112+
initOpenTelemetry,
112113
} from '@sentry/node';
113114

114115
export {

packages/google-cloud-serverless/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export {
8888
hapiIntegration,
8989
setupHapiErrorHandler,
9090
spotlightIntegration,
91+
initOpenTelemetry,
9192
} from '@sentry/node';
9293

9394
export {

packages/node-experimental/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export { hapiIntegration, setupHapiErrorHandler } from './integrations/tracing/h
2424
export { spotlightIntegration } from './integrations/spotlight';
2525

2626
export { init, getDefaultIntegrations } from './sdk/init';
27+
export { initOpenTelemetry } from './sdk/initOtel';
2728
export { getAutoPerformanceIntegrations } from './integrations/tracing';
2829
export { getSentryRelease, defaultStackParser } from './sdk/api';
2930
export { createGetModuleFromFilename } from './utils/module';

packages/node-experimental/src/sdk/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { makeNodeTransport } from '../transports';
3737
import type { NodeClientOptions, NodeOptions } from '../types';
3838
import { defaultStackParser, getSentryRelease } from './api';
3939
import { NodeClient } from './client';
40-
import { initOtel } from './initOtel';
40+
import { initOpenTelemetry } from './initOtel';
4141

4242
function getCjsOnlyIntegrations(isCjs = typeof require !== 'undefined'): Integration[] {
4343
return isCjs ? [modulesIntegration()] : [];
@@ -122,7 +122,7 @@ export function init(options: NodeOptions | undefined = {}): void {
122122
// If users opt-out of this, they _have_ to set up OpenTelemetry themselves
123123
// There is no way to use this SDK without OpenTelemetry!
124124
if (!options.skipOpenTelemetrySetup) {
125-
initOtel();
125+
initOpenTelemetry(client);
126126
}
127127

128128
validateOpenTelemetrySetup();

packages/node-experimental/src/sdk/initOtel.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,17 @@ import { DiagLogLevel, diag } from '@opentelemetry/api';
22
import { Resource } from '@opentelemetry/resources';
33
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
44
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
5-
import { SDK_VERSION, getClient } from '@sentry/core';
5+
import { SDK_VERSION } from '@sentry/core';
66
import { SentryPropagator, SentrySampler, SentrySpanProcessor, setupEventContextTrace } from '@sentry/opentelemetry';
77
import { logger } from '@sentry/utils';
88

9-
import { DEBUG_BUILD } from '../debug-build';
109
import { SentryContextManager } from '../otel/contextManager';
1110
import type { NodeClient } from './client';
1211

1312
/**
1413
* Initialize OpenTelemetry for Node.
1514
*/
16-
export function initOtel(): void {
17-
const client = getClient<NodeClient>();
18-
19-
if (!client) {
20-
DEBUG_BUILD &&
21-
logger.warn(
22-
'No client available, skipping OpenTelemetry setup. This probably means that `Sentry.init()` was not called before `initOtel()`.',
23-
);
24-
return;
25-
}
26-
15+
export function initOpenTelemetry(client: NodeClient): void {
2716
if (client.getOptions().debug) {
2817
const otelLogger = new Proxy(logger as typeof logger & { verbose: (typeof logger)['debug'] }, {
2918
get(target, prop, receiver) {

packages/node-experimental/test/sdk/client.test.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import type { Event, EventHint } from '@sentry/types';
1313
import type { Scope } from '@sentry/types';
1414

1515
import { setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
16-
import { NodeClient } from '../../src/sdk/client';
17-
import { initOtel } from '../../src/sdk/initOtel';
16+
import { NodeClient, initOpenTelemetry } from '../../src';
1817
import { getDefaultNodeClientOptions } from '../helpers/getDefaultNodeClientOptions';
1918
import { cleanupOtel } from '../helpers/mockSdkInit';
2019

@@ -79,7 +78,7 @@ describe('NodeClient', () => {
7978
const client = new NodeClient(options);
8079
setCurrentClient(client);
8180
client.init();
82-
initOtel();
81+
initOpenTelemetry(client);
8382

8483
withIsolationScope(isolationScope => {
8584
isolationScope.setRequestSession({ status: 'ok' });
@@ -96,7 +95,7 @@ describe('NodeClient', () => {
9695
const client = new NodeClient(options);
9796
setCurrentClient(client);
9897
client.init();
99-
initOtel();
98+
initOpenTelemetry(client);
10099

101100
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
102101
// by the`requestHandler`)
@@ -117,7 +116,7 @@ describe('NodeClient', () => {
117116
const client = new NodeClient(options);
118117
setCurrentClient(client);
119118
client.init();
120-
initOtel();
119+
initOpenTelemetry(client);
121120

122121
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
123122
// by the`requestHandler`)
@@ -138,7 +137,7 @@ describe('NodeClient', () => {
138137
const client = new NodeClient(options);
139138
setCurrentClient(client);
140139
client.init();
141-
initOtel();
140+
initOpenTelemetry(client);
142141

143142
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
144143
// by the`requestHandler`)
@@ -159,7 +158,7 @@ describe('NodeClient', () => {
159158
const client = new NodeClient(options);
160159
setCurrentClient(client);
161160
client.init();
162-
initOtel();
161+
initOpenTelemetry(client);
163162

164163
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
165164
// by the`requestHandler`)
@@ -187,7 +186,7 @@ describe('NodeClient', () => {
187186
const client = new NodeClient(options);
188187
setCurrentClient(client);
189188
client.init();
190-
initOtel();
189+
initOpenTelemetry(client);
191190

192191
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
193192
// by the`requestHandler`)
@@ -223,7 +222,7 @@ describe('NodeClient', () => {
223222
const client = new NodeClient(options);
224223
setCurrentClient(client);
225224
client.init();
226-
initOtel();
225+
initOpenTelemetry(client);
227226

228227
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
229228
// by the`requestHandler`)
@@ -244,7 +243,7 @@ describe('NodeClient', () => {
244243
const client = new NodeClient(options);
245244
setCurrentClient(client);
246245
client.init();
247-
initOtel();
246+
initOpenTelemetry(client);
248247

249248
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
250249
// by the`requestHandler`)
@@ -263,7 +262,7 @@ describe('NodeClient', () => {
263262
const client = new NodeClient(options);
264263
setCurrentClient(client);
265264
client.init();
266-
initOtel();
265+
initOpenTelemetry(client);
267266

268267
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
269268
// by the`requestHandler`)
@@ -284,7 +283,7 @@ describe('NodeClient', () => {
284283
const client = new NodeClient(options);
285284
setCurrentClient(client);
286285
client.init();
287-
initOtel();
286+
initOpenTelemetry(client);
288287

289288
withIsolationScope(isolationScope => {
290289
isolationScope.setRequestSession({ status: 'ok' });

0 commit comments

Comments
 (0)