Skip to content

fix(node): Export initOpenTelemetry #11158

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

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export {
hapiIntegration,
setupHapiErrorHandler,
spotlightIntegration,
initOpenTelemetry,
} from '@sentry/node';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export {
hapiIntegration,
setupHapiErrorHandler,
spotlightIntegration,
initOpenTelemetry,
} from '@sentry/node';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/google-cloud-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export {
hapiIntegration,
setupHapiErrorHandler,
spotlightIntegration,
initOpenTelemetry,
} from '@sentry/node';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/node-experimental/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { hapiIntegration, setupHapiErrorHandler } from './integrations/tracing/h
export { spotlightIntegration } from './integrations/spotlight';

export { init, getDefaultIntegrations } from './sdk/init';
export { initOpenTelemetry } from './sdk/initOtel';
export { getAutoPerformanceIntegrations } from './integrations/tracing';
export { getSentryRelease, defaultStackParser } from './sdk/api';
export { createGetModuleFromFilename } from './utils/module';
Expand Down
4 changes: 2 additions & 2 deletions packages/node-experimental/src/sdk/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { makeNodeTransport } from '../transports';
import type { NodeClientOptions, NodeOptions } from '../types';
import { defaultStackParser, getSentryRelease } from './api';
import { NodeClient } from './client';
import { initOtel } from './initOtel';
import { initOpenTelemetry } from './initOtel';

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

validateOpenTelemetrySetup();
Expand Down
15 changes: 2 additions & 13 deletions packages/node-experimental/src/sdk/initOtel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,17 @@ import { DiagLogLevel, diag } from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { SDK_VERSION, getClient } from '@sentry/core';
import { SDK_VERSION } from '@sentry/core';
import { SentryPropagator, SentrySampler, SentrySpanProcessor, setupEventContextTrace } from '@sentry/opentelemetry';
import { logger } from '@sentry/utils';

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

/**
* Initialize OpenTelemetry for Node.
*/
export function initOtel(): void {
const client = getClient<NodeClient>();

if (!client) {
DEBUG_BUILD &&
logger.warn(
'No client available, skipping OpenTelemetry setup. This probably means that `Sentry.init()` was not called before `initOtel()`.',
);
return;
}

export function initOpenTelemetry(client: NodeClient): void {
if (client.getOptions().debug) {
const otelLogger = new Proxy(logger as typeof logger & { verbose: (typeof logger)['debug'] }, {
get(target, prop, receiver) {
Expand Down
23 changes: 11 additions & 12 deletions packages/node-experimental/test/sdk/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import type { Event, EventHint } from '@sentry/types';
import type { Scope } from '@sentry/types';

import { setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
import { NodeClient } from '../../src/sdk/client';
import { initOtel } from '../../src/sdk/initOtel';
import { NodeClient, initOpenTelemetry } from '../../src';
import { getDefaultNodeClientOptions } from '../helpers/getDefaultNodeClientOptions';
import { cleanupOtel } from '../helpers/mockSdkInit';

Expand Down Expand Up @@ -79,7 +78,7 @@ describe('NodeClient', () => {
const client = new NodeClient(options);
setCurrentClient(client);
client.init();
initOtel();
initOpenTelemetry(client);

withIsolationScope(isolationScope => {
isolationScope.setRequestSession({ status: 'ok' });
Expand All @@ -96,7 +95,7 @@ describe('NodeClient', () => {
const client = new NodeClient(options);
setCurrentClient(client);
client.init();
initOtel();
initOpenTelemetry(client);

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

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

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

// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
Expand Down Expand Up @@ -187,7 +186,7 @@ describe('NodeClient', () => {
const client = new NodeClient(options);
setCurrentClient(client);
client.init();
initOtel();
initOpenTelemetry(client);

// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
Expand Down Expand Up @@ -223,7 +222,7 @@ describe('NodeClient', () => {
const client = new NodeClient(options);
setCurrentClient(client);
client.init();
initOtel();
initOpenTelemetry(client);

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

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

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

withIsolationScope(isolationScope => {
isolationScope.setRequestSession({ status: 'ok' });
Expand Down