Skip to content

Commit c78789d

Browse files
authored
ref(node-experimental): Remove custom hub & scope (#10616)
This removes almost all the custom hub & scope stuff from node-experimental/opentelemetry! 1. I opted to leave the `setupGlobalHub()` method in as a noop, as that is currently documented for `@sentry/opentelemetry`. We can't deprecate this on v7 as it is required there... but it's OK imho, it's just an empty function we can remove in v9.
1 parent c244978 commit c78789d

File tree

27 files changed

+50
-543
lines changed

27 files changed

+50
-543
lines changed

packages/node-experimental/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export {
3333
captureException,
3434
captureEvent,
3535
captureMessage,
36-
addGlobalEventProcessor,
3736
addEventProcessor,
3837
setContext,
3938
setExtra,
@@ -45,19 +44,18 @@ export {
4544
withIsolationScope,
4645
withActiveSpan,
4746
getCurrentScope,
48-
getGlobalScope,
4947
getIsolationScope,
5048
setIsolationScope,
5149
setCurrentScope,
5250
} from './sdk/api';
5351
export { getCurrentHub, makeMain } from './sdk/hub';
54-
export { Scope } from './sdk/scope';
5552

5653
export {
5754
addBreadcrumb,
5855
makeNodeTransport,
5956
defaultStackParser,
6057
getSentryRelease,
58+
getGlobalScope,
6159
addRequestDataToEvent,
6260
DEFAULT_USER_INCLUDES,
6361
extractRequestData,

packages/node-experimental/src/integrations/http.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Span } from '@opentelemetry/api';
33
import { SpanKind } from '@opentelemetry/api';
44
import { registerInstrumentations } from '@opentelemetry/instrumentation';
55
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
6+
67
import { addBreadcrumb, defineIntegration, hasTracingEnabled, isSentryRequestUrl } from '@sentry/core';
78
import { _INTERNAL, getClient, getSpanKind, setSpanMetadata } from '@sentry/opentelemetry';
89
import type { EventProcessor, Hub, Integration, IntegrationFn } from '@sentry/types';

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import type {
1010
Extra,
1111
Extras,
1212
Primitive,
13+
Scope,
1314
SeverityLevel,
1415
User,
1516
} from '@sentry/types';
1617
import { getContextFromScope, getScopesFromContext, setScopesOnContext } from '../utils/contextData';
1718

1819
import type { ExclusiveEventHintOrCaptureContext } from '../utils/prepareEvent';
1920
import { parseEventHintOrCaptureContext } from '../utils/prepareEvent';
20-
import type { Scope } from './scope';
21-
import { getClient, getCurrentScope, getGlobalScope, getIsolationScope, isInitialized } from './scope';
21+
import { getClient, getCurrentScope, getIsolationScope, isInitialized } from './scope';
2222

23-
export { getCurrentScope, getGlobalScope, getIsolationScope, getClient, isInitialized };
23+
export { getCurrentScope, getIsolationScope, getClient, isInitialized };
2424
export { setCurrentScope, setIsolationScope } from './scope';
2525

2626
/**
@@ -113,13 +113,6 @@ export function captureEvent(event: Event, hint?: EventHint): string {
113113
return getCurrentScope().captureEvent(event, hint);
114114
}
115115

116-
/**
117-
* Add a global event processor.
118-
*/
119-
export function addGlobalEventProcessor(eventProcessor: EventProcessor): void {
120-
getGlobalScope().addEventProcessor(eventProcessor);
121-
}
122-
123116
/**
124117
* Add an event processor to the current isolation scope.
125118
*/

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import type { Tracer } from '@opentelemetry/api';
44
import { trace } from '@opentelemetry/api';
55
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
66
import { applySdkMetadata } from '@sentry/core';
7-
import type { CaptureContext, Event, EventHint } from '@sentry/types';
8-
import { Scope } from './scope';
97

108
/** A client for using Sentry with Node & OpenTelemetry. */
119
export class NodeExperimentalClient extends NodeClient {
@@ -45,31 +43,4 @@ export class NodeExperimentalClient extends NodeClient {
4543

4644
return super.flush(timeout);
4745
}
48-
49-
/**
50-
* Extends the base `_prepareEvent` so that we can properly handle `captureContext`.
51-
* This uses `new Scope()`, which we need to replace with our own Scope for this client.
52-
*/
53-
protected _prepareEvent(
54-
event: Event,
55-
hint: EventHint,
56-
scope?: Scope,
57-
isolationScope?: Scope,
58-
): PromiseLike<Event | null> {
59-
let actualScope = scope;
60-
61-
// Remove `captureContext` hint and instead clone already here
62-
if (hint && hint.captureContext) {
63-
actualScope = getScopeForEvent(scope, hint.captureContext);
64-
delete hint.captureContext;
65-
}
66-
67-
return super._prepareEvent(event, hint, actualScope, isolationScope);
68-
}
69-
}
70-
71-
function getScopeForEvent(scope: Scope | undefined, captureContext: CaptureContext): Scope | undefined {
72-
const finalScope = scope ? scope.clone() : new Scope();
73-
finalScope.update(captureContext);
74-
return finalScope;
7546
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
Hub,
66
Integration,
77
IntegrationClass,
8+
Scope,
89
SeverityLevel,
910
TransactionContext,
1011
} from '@sentry/types';
@@ -23,7 +24,6 @@ import {
2324
withScope,
2425
} from './api';
2526
import { callExtensionMethod, getGlobalCarrier } from './globals';
26-
import type { Scope } from './scope';
2727
import { getIsolationScope } from './scope';
2828
import type { SentryCarrier } from './types';
2929

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { httpIntegration } from '../integrations/http';
2222
import { nativeNodeFetchIntegration } from '../integrations/node-fetch';
2323
import { setOpenTelemetryContextAsyncContextStrategy } from '../otel/asyncContextStrategy';
2424
import type { NodeExperimentalClientOptions, NodeExperimentalOptions } from '../types';
25-
import { getClient, getCurrentScope, getGlobalScope, getIsolationScope } from './api';
25+
import { getClient, getCurrentScope, getIsolationScope } from './api';
2626
import { NodeExperimentalClient } from './client';
2727
import { getGlobalCarrier } from './globals';
2828
import { setLegacyHubOnCarrier } from './hub';
@@ -70,9 +70,8 @@ export function init(options: NodeExperimentalOptions | undefined = {}): void {
7070
scope.update(options.initialScope);
7171

7272
const client = new NodeExperimentalClient(clientOptions);
73-
// The client is on the global scope, from where it generally is inherited
74-
// unless somebody specifically sets a different one on a scope/isolations cope
75-
getGlobalScope().setClient(client);
73+
// The client is on the current scope, from where it generally is inherited
74+
getCurrentScope().setClient(client);
7675

7776
if (isEnabled(client)) {
7877
client.init();

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

Lines changed: 6 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { getGlobalScope as _getGlobalScope, setGlobalScope } from '@sentry/core';
2-
import { OpenTelemetryScope } from '@sentry/opentelemetry';
3-
import type { Client, Event, EventHint, SeverityLevel } from '@sentry/types';
4-
import { uuid4 } from '@sentry/utils';
1+
import { Scope as ScopeClass, getGlobalScope } from '@sentry/core';
2+
import type { Client } from '@sentry/types';
3+
import type { Scope } from '@sentry/types';
54

65
import { getGlobalCarrier } from './globals';
7-
import type { CurrentScopes, Scope as ScopeInterface, ScopeData, SentryCarrier } from './types';
6+
import type { CurrentScopes, SentryCarrier } from './types';
87

98
/** Get the current scope. */
109
export function getCurrentScope(): Scope {
@@ -19,24 +18,6 @@ export function setCurrentScope(scope: Scope): void {
1918
getScopes().scope = scope;
2019
}
2120

22-
/**
23-
* Get the global scope.
24-
* We overwrite this from the core implementation to make sure we get the correct Scope class.
25-
*/
26-
export function getGlobalScope(): Scope {
27-
const globalScope = _getGlobalScope();
28-
29-
// If we have a default Scope here by chance, make sure to "upgrade" it to our custom Scope
30-
if (!(globalScope instanceof Scope)) {
31-
const newScope = new Scope();
32-
newScope.update(globalScope);
33-
setGlobalScope(newScope);
34-
return newScope;
35-
}
36-
37-
return globalScope;
38-
}
39-
4021
/** Get the currently active isolation scope. */
4122
export function getIsolationScope(): Scope {
4223
return getScopes().isolationScope as Scope;
@@ -70,103 +51,6 @@ export function isInitialized(): boolean {
7051
return !!getClient().getDsn();
7152
}
7253

73-
/** A fork of the classic scope with some otel specific stuff. */
74-
export class Scope extends OpenTelemetryScope implements ScopeInterface {
75-
protected _client: Client | undefined;
76-
77-
/**
78-
* @inheritDoc
79-
*/
80-
public clone(): Scope {
81-
const newScope = new Scope();
82-
newScope._breadcrumbs = [...this['_breadcrumbs']];
83-
newScope._tags = { ...this['_tags'] };
84-
newScope._extra = { ...this['_extra'] };
85-
newScope._contexts = { ...this['_contexts'] };
86-
newScope._user = { ...this['_user'] };
87-
newScope._level = this['_level'];
88-
newScope._span = this['_span'];
89-
newScope._session = this['_session'];
90-
newScope._transactionName = this['_transactionName'];
91-
newScope._fingerprint = this['_fingerprint'];
92-
newScope._eventProcessors = [...this['_eventProcessors']];
93-
newScope._requestSession = this['_requestSession'];
94-
newScope._attachments = [...this['_attachments']];
95-
newScope._sdkProcessingMetadata = { ...this['_sdkProcessingMetadata'] };
96-
newScope._propagationContext = { ...this['_propagationContext'] };
97-
newScope._client = this._client;
98-
99-
return newScope;
100-
}
101-
102-
/** Update the client on the scope. */
103-
public setClient(client: Client): void {
104-
this._client = client;
105-
}
106-
107-
/**
108-
* Get the client assigned to this scope.
109-
* Should generally not be used by users - use top-level `Sentry.getClient()` instead!
110-
* @internal
111-
*/
112-
public getClient(): Client | undefined {
113-
return this._client;
114-
}
115-
116-
/** Capture an exception for this scope. */
117-
public captureException(exception: unknown, hint?: EventHint): string {
118-
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
119-
const syntheticException = new Error('Sentry syntheticException');
120-
121-
getClient().captureException(
122-
exception,
123-
{
124-
originalException: exception,
125-
syntheticException,
126-
...hint,
127-
event_id: eventId,
128-
},
129-
this,
130-
);
131-
132-
return eventId;
133-
}
134-
135-
/** Capture a message for this scope. */
136-
public captureMessage(message: string, level?: SeverityLevel, hint?: EventHint): string {
137-
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
138-
const syntheticException = new Error(message);
139-
140-
getClient().captureMessage(
141-
message,
142-
level,
143-
{
144-
originalException: message,
145-
syntheticException,
146-
...hint,
147-
event_id: eventId,
148-
},
149-
this,
150-
);
151-
152-
return eventId;
153-
}
154-
155-
/** Capture a message for this scope. */
156-
public captureEvent(event: Event, hint?: EventHint): string {
157-
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
158-
159-
getClient().captureEvent(event, { ...hint, event_id: eventId }, this);
160-
161-
return eventId;
162-
}
163-
164-
/** Get scope data for this scope only. */
165-
public getOwnScopeData(): ScopeData {
166-
return super.getScopeData();
167-
}
168-
}
169-
17054
function getScopes(): CurrentScopes {
17155
const carrier = getGlobalCarrier();
17256

@@ -184,8 +68,8 @@ function getScopes(): CurrentScopes {
18468
function getGlobalCurrentScopes(carrier: SentryCarrier): CurrentScopes {
18569
if (!carrier.scopes) {
18670
carrier.scopes = {
187-
scope: new Scope(),
188-
isolationScope: new Scope(),
71+
scope: new ScopeClass(),
72+
isolationScope: new ScopeClass(),
18973
};
19074
}
19175

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ import { SentrySpanProcessor, getClient } from '@sentry/opentelemetry';
66
import type { Http } from '../integrations/http';
77
import type { NodeFetch } from '../integrations/node-fetch';
88
import type { NodeExperimentalClient } from '../types';
9-
import { Scope } from './scope';
109

1110
/**
1211
* Implement custom code to avoid sending spans in certain cases.
1312
*/
1413
export class NodeExperimentalSentrySpanProcessor extends SentrySpanProcessor {
15-
public constructor() {
16-
super({ scopeClass: Scope });
17-
}
18-
1914
/** @inheritDoc */
2015
protected _shouldSendSpanToSentry(span: Span): boolean {
2116
const client = getClient<NodeExperimentalClient>();

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
Integration,
99
Primitive,
1010
PropagationContext,
11-
Scope as BaseScope,
11+
Scope,
1212
SeverityLevel,
1313
User,
1414
} from '@sentry/types';
@@ -27,12 +27,6 @@ export interface ScopeData {
2727
level?: SeverityLevel;
2828
}
2929

30-
export interface Scope extends BaseScope {
31-
// @ts-expect-error typeof this is what we want here
32-
clone(scope?: Scope): typeof this;
33-
getScopeData(): ScopeData;
34-
}
35-
3630
export interface CurrentScopes {
3731
scope: Scope;
3832
isolationScope: Scope;

packages/node-experimental/test/integration/breadcrumbs.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ describe('Integration | breadcrumbs', () => {
2626
addBreadcrumb({ timestamp: 123455, message: 'test3' });
2727

2828
const error = new Error('test');
29-
// eslint-disable-next-line deprecation/deprecation
3029
captureException(error);
3130

3231
await client.flush();
@@ -117,7 +116,6 @@ describe('Integration | breadcrumbs', () => {
117116
addBreadcrumb({ timestamp: 123455, message: 'test3' });
118117
});
119118

120-
// eslint-disable-next-line deprecation/deprecation
121119
captureException(error);
122120
});
123121

@@ -170,7 +168,6 @@ describe('Integration | breadcrumbs', () => {
170168
addBreadcrumb({ timestamp: 123457, message: 'test2-b' });
171169
});
172170

173-
// eslint-disable-next-line deprecation/deprecation
174171
captureException(error);
175172
});
176173
});
@@ -213,7 +210,6 @@ describe('Integration | breadcrumbs', () => {
213210
addBreadcrumb({ timestamp: 123457, message: 'test2' });
214211
});
215212

216-
// eslint-disable-next-line deprecation/deprecation
217213
captureException(error);
218214
});
219215

@@ -260,7 +256,6 @@ describe('Integration | breadcrumbs', () => {
260256
startSpan({ name: 'inner3' }, () => {
261257
addBreadcrumb({ timestamp: 123457, message: 'test4' });
262258

263-
// eslint-disable-next-line deprecation/deprecation
264259
captureException(error);
265260

266261
startSpan({ name: 'inner4' }, () => {
@@ -320,7 +315,6 @@ describe('Integration | breadcrumbs', () => {
320315

321316
await new Promise(resolve => setTimeout(resolve, 10));
322317

323-
// eslint-disable-next-line deprecation/deprecation
324318
captureException(error);
325319
});
326320
});

0 commit comments

Comments
 (0)