Skip to content

Commit 3b1d836

Browse files
authored
feat(opentelemetry): Merge node-experimental changes into opentelemetry (#10689)
This PR moves the formerly experimental stuff that we did in node-experimental into the opentelemetry package. This means that the generic OTEL stuff is there and can in theory also be used with otel-browser (if a user would want that), and our node package will eventually be built on top of that. Mostly, this means we can delete a bunch of code!
1 parent 22ebb3b commit 3b1d836

23 files changed

+539
-450
lines changed

packages/node-experimental/src/index.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@ export { getAutoPerformanceIntegrations } from './integrations/getAutoPerformanc
1616
export * as Handlers from './sdk/handlers';
1717
export type { Span } from './types';
1818

19-
export { startSpan, startSpanManual, startInactiveSpan, getActiveSpan } from '@sentry/opentelemetry';
20-
export {
21-
getClient,
22-
captureException,
23-
captureEvent,
24-
captureMessage,
25-
withActiveSpan,
26-
} from './sdk/api';
19+
export { startSpan, startSpanManual, startInactiveSpan, getActiveSpan, withActiveSpan } from '@sentry/opentelemetry';
20+
export { getClient } from './sdk/api';
2721
// eslint-disable-next-line deprecation/deprecation
2822
export { getCurrentHub } from './sdk/hub';
2923

@@ -86,6 +80,9 @@ export {
8680
getIsolationScope,
8781
withScope,
8882
withIsolationScope,
83+
captureException,
84+
captureEvent,
85+
captureMessage,
8986
} from '@sentry/node';
9087

9188
export type {

packages/node-experimental/src/otel/asyncContextStrategy.ts

Lines changed: 0 additions & 116 deletions
This file was deleted.
Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
import type { Context } from '@opentelemetry/api';
21
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
3-
import { getCurrentScope, getIsolationScope } from '@sentry/core';
4-
import { setHubOnContext } from '@sentry/opentelemetry';
5-
import type { Scope } from '@sentry/types';
6-
import { getCurrentHub } from '../sdk/hub';
7-
8-
import type { CurrentScopes } from './../sdk/types';
9-
import {
10-
SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY,
11-
SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY,
12-
SENTRY_FORK_SET_SCOPE_CONTEXT_KEY,
13-
getScopesFromContext,
14-
setScopesOnContext,
15-
} from './../utils/contextData';
2+
import { wrapContextManagerClass } from '@sentry/opentelemetry';
163

174
/**
185
* This is a custom ContextManager for OpenTelemetry, which extends the default AsyncLocalStorageContextManager.
@@ -21,46 +8,4 @@ import {
218
* Note that we currently only support AsyncHooks with this,
229
* but since this should work for Node 14+ anyhow that should be good enough.
2310
*/
24-
export class SentryContextManager extends AsyncLocalStorageContextManager {
25-
/**
26-
* Overwrite with() of the original AsyncLocalStorageContextManager
27-
* to ensure we also create a new hub per context.
28-
*/
29-
public with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(
30-
context: Context,
31-
fn: F,
32-
thisArg?: ThisParameterType<F>,
33-
...args: A
34-
): ReturnType<F> {
35-
const currentScopes = getScopesFromContext(context);
36-
const currentScope = currentScopes?.scope || getCurrentScope();
37-
const currentIsolationScope = currentScopes?.isolationScope || getIsolationScope();
38-
39-
const shouldForkIsolationScope = context.getValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY) === true;
40-
const scope = context.getValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY) as Scope | undefined;
41-
const isolationScope = context.getValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY) as Scope | undefined;
42-
43-
const newCurrentScope = scope || currentScope.clone();
44-
const newIsolationScope =
45-
isolationScope || (shouldForkIsolationScope ? currentIsolationScope.clone() : currentIsolationScope);
46-
const scopes: CurrentScopes = { scope: newCurrentScope, isolationScope: newIsolationScope };
47-
48-
const mockHub = {
49-
// eslint-disable-next-line deprecation/deprecation
50-
...getCurrentHub(),
51-
getScope: () => newCurrentScope,
52-
getIsolationScope: () => newIsolationScope,
53-
};
54-
55-
const ctx1 = setHubOnContext(context, mockHub);
56-
const ctx2 = setScopesOnContext(ctx1, scopes);
57-
58-
// Remove the unneeded values again
59-
const ctx3 = ctx2
60-
.deleteValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY)
61-
.deleteValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY)
62-
.deleteValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY);
63-
64-
return super.with(ctx3, fn, thisArg, ...args);
65-
}
66-
}
11+
export const SentryContextManager = wrapContextManagerClass(AsyncLocalStorageContextManager);
Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
// PUBLIC APIS
22

3-
import type { Span } from '@opentelemetry/api';
4-
import { context, trace } from '@opentelemetry/api';
53
import { getCurrentScope } from '@sentry/core';
6-
import type { CaptureContext, Client, Event, EventHint, Scope, SeverityLevel } from '@sentry/types';
7-
8-
import type { ExclusiveEventHintOrCaptureContext } from '../utils/prepareEvent';
9-
import { parseEventHintOrCaptureContext } from '../utils/prepareEvent';
4+
import type { Client } from '@sentry/types';
105

116
/** Get the currently active client. */
127
export function getClient<C extends Client>(): C {
@@ -20,35 +15,3 @@ export function getClient<C extends Client>(): C {
2015
// TODO otherwise ensure we use a noop client
2116
return {} as C;
2217
}
23-
24-
/**
25-
* Forks the current scope and sets the provided span as active span in the context of the provided callback.
26-
*
27-
* @param span Spans started in the context of the provided callback will be children of this span.
28-
* @param callback Execution context in which the provided span will be active. Is passed the newly forked scope.
29-
* @returns the value returned from the provided callback function.
30-
*/
31-
export function withActiveSpan<T>(span: Span, callback: (scope: Scope) => T): T {
32-
const newContextWithActiveSpan = trace.setSpan(context.active(), span);
33-
return context.with(newContextWithActiveSpan, () => callback(getCurrentScope()));
34-
}
35-
36-
/** Record an exception and send it to Sentry. */
37-
export function captureException(exception: unknown, hint?: ExclusiveEventHintOrCaptureContext): string {
38-
return getCurrentScope().captureException(exception, parseEventHintOrCaptureContext(hint));
39-
}
40-
41-
/** Record a message and send it to Sentry. */
42-
export function captureMessage(message: string, captureContext?: CaptureContext | SeverityLevel): string {
43-
// This is necessary to provide explicit scopes upgrade, without changing the original
44-
// arity of the `captureMessage(message, level)` method.
45-
const level = typeof captureContext === 'string' ? captureContext : undefined;
46-
const context = typeof captureContext !== 'string' ? { captureContext } : undefined;
47-
48-
return getCurrentScope().captureMessage(message, level, context);
49-
}
50-
51-
/** Capture a generic event and send it to Sentry. */
52-
export function captureEvent(event: Event, hint?: EventHint): string {
53-
return getCurrentScope().captureEvent(event, hint);
54-
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212

1313
import {
1414
addBreadcrumb,
15+
captureEvent,
1516
endSession,
1617
getCurrentScope,
1718
getIsolationScope,
@@ -24,7 +25,7 @@ import {
2425
startSession,
2526
withScope,
2627
} from '@sentry/core';
27-
import { captureEvent, getClient } from './api';
28+
import { getClient } from './api';
2829
import { callExtensionMethod } from './globals';
2930

3031
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
makeNodeTransport,
1414
spotlightIntegration,
1515
} from '@sentry/node';
16+
import { setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
1617
import type { Client, Integration, Options } from '@sentry/types';
1718
import {
1819
consoleSandbox,
@@ -26,7 +27,6 @@ import { DEBUG_BUILD } from '../debug-build';
2627
import { getAutoPerformanceIntegrations } from '../integrations/getAutoPerformanceIntegrations';
2728
import { httpIntegration } from '../integrations/http';
2829
import { nativeNodeFetchIntegration } from '../integrations/node-fetch';
29-
import { setOpenTelemetryContextAsyncContextStrategy } from '../otel/asyncContextStrategy';
3030
import type { NodeExperimentalClientOptions, NodeExperimentalOptions } from '../types';
3131
import { NodeExperimentalClient } from './client';
3232
import { initOtel } from './initOtel';

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import { Resource } from '@opentelemetry/resources';
33
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
44
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
55
import { SDK_VERSION } from '@sentry/core';
6-
import { SentryPropagator, SentrySampler, setupEventContextTrace } from '@sentry/opentelemetry';
6+
import { SentryPropagator, SentrySampler, SentrySpanProcessor, setupEventContextTrace } from '@sentry/opentelemetry';
77
import { logger } from '@sentry/utils';
88

99
import { DEBUG_BUILD } from '../debug-build';
1010
import { SentryContextManager } from '../otel/contextManager';
1111
import type { NodeExperimentalClient } from '../types';
1212
import { getClient } from './api';
13-
import { NodeExperimentalSentrySpanProcessor } from './spanProcessor';
1413

1514
/**
1615
* Initialize OpenTelemetry for Node.
@@ -55,7 +54,7 @@ export function setupOtel(client: NodeExperimentalClient): BasicTracerProvider {
5554
}),
5655
forceFlushTimeoutMillis: 500,
5756
});
58-
provider.addSpanProcessor(new NodeExperimentalSentrySpanProcessor());
57+
provider.addSpanProcessor(new SentrySpanProcessor());
5958

6059
// Initialize the provider
6160
provider.register({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { context } from '@opentelemetry/api';
2+
import { getScopesFromContext } from '@sentry/opentelemetry';
23
import type { Scope } from '@sentry/types';
3-
import { getScopesFromContext } from '../utils/contextData';
44

55
/**
66
* Update the active isolation scope.

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

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/node-experimental/src/utils/contextData.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)