Skip to content

Commit ac94dc5

Browse files
committed
fixes to scope & use scope.clone()
1 parent 25ec131 commit ac94dc5

File tree

5 files changed

+29
-26
lines changed

5 files changed

+29
-26
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class Http implements Integration {
132132

133133
// Update the isolation scope, isolation this request
134134
if (getSpanKind(span) === SpanKind.SERVER) {
135-
setIsolationScope(Scope.clone(getIsolationScope()));
135+
setIsolationScope(getIsolationScope().clone());
136136
}
137137
},
138138
responseHook: (span, res) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class SentryContextManager extends AsyncLocalStorageContextManager {
3131
const currentScope = previousScopes ? previousScopes.scope : getCurrentScope();
3232
const isolationScope = previousScopes ? previousScopes.isolationScope : getIsolationScope();
3333

34-
const newCurrentScope = Scope.clone(currentScope);
34+
const newCurrentScope = currentScope.clone();
3535
const scopes: CurrentScopes = { scope: newCurrentScope, isolationScope };
3636

3737
// We also need to "mock" the hub on the context, as the original @sentry/opentelemetry uses that...

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export function withIsolationScope<T>(callback: (scope: Scope, isolationScope: S
100100
isolationScope: getIsolationScope(),
101101
};
102102

103-
scopes.isolationScope = Scope.clone(scopes.isolationScope);
103+
scopes.isolationScope = scopes.isolationScope.clone();
104104

105105
return context.with(setScopesOnContext(ctx, scopes), () => {
106106
return callback(getCurrentScope(), getIsolationScope());

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NodeClient, SDK_VERSION } from '@sentry/node';
33
import type { Tracer } from '@opentelemetry/api';
44
import { trace } from '@opentelemetry/api';
55
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
6-
import type { Event, EventHint } from '@sentry/types';
6+
import type { CaptureContext, Event, EventHint } from '@sentry/types';
77
import { Scope } from './scope';
88

99
/** A client for using Sentry with Node & OpenTelemetry. */
@@ -57,17 +57,23 @@ export class NodeExperimentalClient extends NodeClient {
5757

5858
/**
5959
* Extends the base `_prepareEvent` so that we can properly handle `captureContext`.
60-
* This uses `Scope.clone()`, which we need to replace with `NodeExperimentalScope.clone()` for this client.
60+
* This uses `new Scope()`, which we need to replace with our own Scope for this client.
6161
*/
6262
protected _prepareEvent(event: Event, hint: EventHint, scope?: Scope): PromiseLike<Event | null> {
6363
let actualScope = scope;
6464

6565
// Remove `captureContext` hint and instead clone already here
6666
if (hint && hint.captureContext) {
67-
actualScope = Scope.clone(scope);
67+
actualScope = getFinalScope(scope, hint.captureContext);
6868
delete hint.captureContext;
6969
}
7070

7171
return super._prepareEvent(event, hint, actualScope);
7272
}
7373
}
74+
75+
function getFinalScope(scope: Scope | undefined, captureContext: CaptureContext): Scope | undefined {
76+
const finalScope = scope ? scope.clone() : new Scope();
77+
finalScope.update(captureContext);
78+
return finalScope;
79+
}

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

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,24 @@ export class Scope extends OpenTelemetryScope implements ScopeInterface {
1818
/**
1919
* @inheritDoc
2020
*/
21-
public static clone(scope?: Scope): Scope {
21+
public clone(): Scope {
2222
const newScope = new Scope();
23-
if (scope) {
24-
newScope._breadcrumbs = [...scope['_breadcrumbs']];
25-
newScope._tags = { ...scope['_tags'] };
26-
newScope._extra = { ...scope['_extra'] };
27-
newScope._contexts = { ...scope['_contexts'] };
28-
newScope._user = scope['_user'];
29-
newScope._level = scope['_level'];
30-
newScope._span = scope['_span'];
31-
newScope._session = scope['_session'];
32-
newScope._transactionName = scope['_transactionName'];
33-
newScope._fingerprint = scope['_fingerprint'];
34-
newScope._eventProcessors = [...scope['_eventProcessors']];
35-
newScope._requestSession = scope['_requestSession'];
36-
newScope._attachments = [...scope['_attachments']];
37-
newScope._sdkProcessingMetadata = { ...scope['_sdkProcessingMetadata'] };
38-
newScope._propagationContext = { ...scope['_propagationContext'] };
39-
newScope._client = scope['_client'];
40-
newScope._lastEventId = scope['_lastEventId'];
41-
}
23+
newScope._breadcrumbs = [...this['_breadcrumbs']];
24+
newScope._tags = { ...this['_tags'] };
25+
newScope._extra = { ...this['_extra'] };
26+
newScope._contexts = { ...this['_contexts'] };
27+
newScope._user = this['_user'];
28+
newScope._level = this['_level'];
29+
newScope._span = this['_span'];
30+
newScope._session = this['_session'];
31+
newScope._transactionName = this['_transactionName'];
32+
newScope._fingerprint = this['_fingerprint'];
33+
newScope._eventProcessors = [...this['_eventProcessors']];
34+
newScope._requestSession = this['_requestSession'];
35+
newScope._attachments = [...this['_attachments']];
36+
newScope._sdkProcessingMetadata = { ...this['_sdkProcessingMetadata'] };
37+
newScope._propagationContext = { ...this['_propagationContext'] };
38+
4239
return newScope;
4340
}
4441

0 commit comments

Comments
 (0)