Skip to content
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
4 changes: 1 addition & 3 deletions packages/node-experimental/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export {
captureException,
captureEvent,
captureMessage,
addGlobalEventProcessor,
addEventProcessor,
setContext,
setExtra,
Expand All @@ -45,19 +44,18 @@ export {
withIsolationScope,
withActiveSpan,
getCurrentScope,
getGlobalScope,
getIsolationScope,
setIsolationScope,
setCurrentScope,
} from './sdk/api';
export { getCurrentHub, makeMain } from './sdk/hub';
export { Scope } from './sdk/scope';

export {
addBreadcrumb,
makeNodeTransport,
defaultStackParser,
getSentryRelease,
getGlobalScope,
addRequestDataToEvent,
DEFAULT_USER_INCLUDES,
extractRequestData,
Expand Down
1 change: 1 addition & 0 deletions packages/node-experimental/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Span } from '@opentelemetry/api';
import { SpanKind } from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';

import { addBreadcrumb, defineIntegration, hasTracingEnabled, isSentryRequestUrl } from '@sentry/core';
import { _INTERNAL, getClient, getSpanKind, setSpanMetadata } from '@sentry/opentelemetry';
import type { EventProcessor, Hub, Integration, IntegrationFn } from '@sentry/types';
Expand Down
13 changes: 3 additions & 10 deletions packages/node-experimental/src/sdk/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import type {
Extra,
Extras,
Primitive,
Scope,
SeverityLevel,
User,
} from '@sentry/types';
import { getContextFromScope, getScopesFromContext, setScopesOnContext } from '../utils/contextData';

import type { ExclusiveEventHintOrCaptureContext } from '../utils/prepareEvent';
import { parseEventHintOrCaptureContext } from '../utils/prepareEvent';
import type { Scope } from './scope';
import { getClient, getCurrentScope, getGlobalScope, getIsolationScope, isInitialized } from './scope';
import { getClient, getCurrentScope, getIsolationScope, isInitialized } from './scope';

export { getCurrentScope, getGlobalScope, getIsolationScope, getClient, isInitialized };
export { getCurrentScope, getIsolationScope, getClient, isInitialized };
export { setCurrentScope, setIsolationScope } from './scope';

/**
Expand Down Expand Up @@ -113,13 +113,6 @@ export function captureEvent(event: Event, hint?: EventHint): string {
return getCurrentScope().captureEvent(event, hint);
}

/**
* Add a global event processor.
*/
export function addGlobalEventProcessor(eventProcessor: EventProcessor): void {
getGlobalScope().addEventProcessor(eventProcessor);
}

/**
* Add an event processor to the current isolation scope.
*/
Expand Down
29 changes: 0 additions & 29 deletions packages/node-experimental/src/sdk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import type { Tracer } from '@opentelemetry/api';
import { trace } from '@opentelemetry/api';
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import { applySdkMetadata } from '@sentry/core';
import type { CaptureContext, Event, EventHint } from '@sentry/types';
import { Scope } from './scope';

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

return super.flush(timeout);
}

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

// Remove `captureContext` hint and instead clone already here
if (hint && hint.captureContext) {
actualScope = getScopeForEvent(scope, hint.captureContext);
delete hint.captureContext;
}

return super._prepareEvent(event, hint, actualScope, isolationScope);
}
}

function getScopeForEvent(scope: Scope | undefined, captureContext: CaptureContext): Scope | undefined {
const finalScope = scope ? scope.clone() : new Scope();
finalScope.update(captureContext);
return finalScope;
}
2 changes: 1 addition & 1 deletion packages/node-experimental/src/sdk/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
Hub,
Integration,
IntegrationClass,
Scope,
SeverityLevel,
TransactionContext,
} from '@sentry/types';
Expand All @@ -23,7 +24,6 @@ import {
withScope,
} from './api';
import { callExtensionMethod, getGlobalCarrier } from './globals';
import type { Scope } from './scope';
import { getIsolationScope } from './scope';
import type { SentryCarrier } from './types';

Expand Down
7 changes: 3 additions & 4 deletions packages/node-experimental/src/sdk/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { httpIntegration } from '../integrations/http';
import { nativeNodeFetchIntegration } from '../integrations/node-fetch';
import { setOpenTelemetryContextAsyncContextStrategy } from '../otel/asyncContextStrategy';
import type { NodeExperimentalClientOptions, NodeExperimentalOptions } from '../types';
import { getClient, getCurrentScope, getGlobalScope, getIsolationScope } from './api';
import { getClient, getCurrentScope, getIsolationScope } from './api';
import { NodeExperimentalClient } from './client';
import { getGlobalCarrier } from './globals';
import { setLegacyHubOnCarrier } from './hub';
Expand Down Expand Up @@ -70,9 +70,8 @@ export function init(options: NodeExperimentalOptions | undefined = {}): void {
scope.update(options.initialScope);

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

if (isEnabled(client)) {
client.init();
Expand Down
128 changes: 6 additions & 122 deletions packages/node-experimental/src/sdk/scope.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { getGlobalScope as _getGlobalScope, setGlobalScope } from '@sentry/core';
import { OpenTelemetryScope } from '@sentry/opentelemetry';
import type { Client, Event, EventHint, SeverityLevel } from '@sentry/types';
import { uuid4 } from '@sentry/utils';
import { Scope as ScopeClass, getGlobalScope } from '@sentry/core';
import type { Client } from '@sentry/types';
import type { Scope } from '@sentry/types';

import { getGlobalCarrier } from './globals';
import type { CurrentScopes, Scope as ScopeInterface, ScopeData, SentryCarrier } from './types';
import type { CurrentScopes, SentryCarrier } from './types';

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

/**
* Get the global scope.
* We overwrite this from the core implementation to make sure we get the correct Scope class.
*/
export function getGlobalScope(): Scope {
const globalScope = _getGlobalScope();

// If we have a default Scope here by chance, make sure to "upgrade" it to our custom Scope
if (!(globalScope instanceof Scope)) {
const newScope = new Scope();
newScope.update(globalScope);
setGlobalScope(newScope);
return newScope;
}

return globalScope;
}

/** Get the currently active isolation scope. */
export function getIsolationScope(): Scope {
return getScopes().isolationScope as Scope;
Expand Down Expand Up @@ -70,103 +51,6 @@ export function isInitialized(): boolean {
return !!getClient().getDsn();
}

/** A fork of the classic scope with some otel specific stuff. */
export class Scope extends OpenTelemetryScope implements ScopeInterface {
protected _client: Client | undefined;

/**
* @inheritDoc
*/
public clone(): Scope {
const newScope = new Scope();
newScope._breadcrumbs = [...this['_breadcrumbs']];
newScope._tags = { ...this['_tags'] };
newScope._extra = { ...this['_extra'] };
newScope._contexts = { ...this['_contexts'] };
newScope._user = { ...this['_user'] };
newScope._level = this['_level'];
newScope._span = this['_span'];
newScope._session = this['_session'];
newScope._transactionName = this['_transactionName'];
newScope._fingerprint = this['_fingerprint'];
newScope._eventProcessors = [...this['_eventProcessors']];
newScope._requestSession = this['_requestSession'];
newScope._attachments = [...this['_attachments']];
newScope._sdkProcessingMetadata = { ...this['_sdkProcessingMetadata'] };
newScope._propagationContext = { ...this['_propagationContext'] };
newScope._client = this._client;

return newScope;
}

/** Update the client on the scope. */
public setClient(client: Client): void {
this._client = client;
}

/**
* Get the client assigned to this scope.
* Should generally not be used by users - use top-level `Sentry.getClient()` instead!
* @internal
*/
public getClient(): Client | undefined {
return this._client;
}

/** Capture an exception for this scope. */
public captureException(exception: unknown, hint?: EventHint): string {
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
const syntheticException = new Error('Sentry syntheticException');

getClient().captureException(
exception,
{
originalException: exception,
syntheticException,
...hint,
event_id: eventId,
},
this,
);

return eventId;
}

/** Capture a message for this scope. */
public captureMessage(message: string, level?: SeverityLevel, hint?: EventHint): string {
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
const syntheticException = new Error(message);

getClient().captureMessage(
message,
level,
{
originalException: message,
syntheticException,
...hint,
event_id: eventId,
},
this,
);

return eventId;
}

/** Capture a message for this scope. */
public captureEvent(event: Event, hint?: EventHint): string {
const eventId = hint && hint.event_id ? hint.event_id : uuid4();

getClient().captureEvent(event, { ...hint, event_id: eventId }, this);

return eventId;
}

/** Get scope data for this scope only. */
public getOwnScopeData(): ScopeData {
return super.getScopeData();
}
}

function getScopes(): CurrentScopes {
const carrier = getGlobalCarrier();

Expand All @@ -184,8 +68,8 @@ function getScopes(): CurrentScopes {
function getGlobalCurrentScopes(carrier: SentryCarrier): CurrentScopes {
if (!carrier.scopes) {
carrier.scopes = {
scope: new Scope(),
isolationScope: new Scope(),
scope: new ScopeClass(),
isolationScope: new ScopeClass(),
};
}

Expand Down
5 changes: 0 additions & 5 deletions packages/node-experimental/src/sdk/spanProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ import { SentrySpanProcessor, getClient } from '@sentry/opentelemetry';
import type { Http } from '../integrations/http';
import type { NodeFetch } from '../integrations/node-fetch';
import type { NodeExperimentalClient } from '../types';
import { Scope } from './scope';

/**
* Implement custom code to avoid sending spans in certain cases.
*/
export class NodeExperimentalSentrySpanProcessor extends SentrySpanProcessor {
public constructor() {
super({ scopeClass: Scope });
}

/** @inheritDoc */
protected _shouldSendSpanToSentry(span: Span): boolean {
const client = getClient<NodeExperimentalClient>();
Expand Down
8 changes: 1 addition & 7 deletions packages/node-experimental/src/sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
Integration,
Primitive,
PropagationContext,
Scope as BaseScope,
Scope,
SeverityLevel,
User,
} from '@sentry/types';
Expand All @@ -27,12 +27,6 @@ export interface ScopeData {
level?: SeverityLevel;
}

export interface Scope extends BaseScope {
// @ts-expect-error typeof this is what we want here
clone(scope?: Scope): typeof this;
getScopeData(): ScopeData;
}

export interface CurrentScopes {
scope: Scope;
isolationScope: Scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('Integration | breadcrumbs', () => {
addBreadcrumb({ timestamp: 123455, message: 'test3' });

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

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

// eslint-disable-next-line deprecation/deprecation
captureException(error);
});

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

// eslint-disable-next-line deprecation/deprecation
captureException(error);
});
});
Expand Down Expand Up @@ -213,7 +210,6 @@ describe('Integration | breadcrumbs', () => {
addBreadcrumb({ timestamp: 123457, message: 'test2' });
});

// eslint-disable-next-line deprecation/deprecation
captureException(error);
});

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

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

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

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

// eslint-disable-next-line deprecation/deprecation
captureException(error);
});
});
Expand Down
Loading