Skip to content

Commit a86aa17

Browse files
committed
ref(node-experimental): Remove custom breadcrumb handling
Instead, just rely on the default behavior of having these on the isolation scope.
1 parent bdd66b6 commit a86aa17

File tree

11 files changed

+56
-377
lines changed

11 files changed

+56
-377
lines changed

packages/node-experimental/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export { startSpan, startSpanManual, startInactiveSpan, getActiveSpan } from '@s
3030
export {
3131
getClient,
3232
isInitialized,
33-
addBreadcrumb,
3433
captureException,
3534
captureEvent,
3635
captureMessage,
@@ -55,6 +54,7 @@ export { getCurrentHub, makeMain } from './sdk/hub';
5554
export { Scope } from './sdk/scope';
5655

5756
export {
57+
addBreadcrumb,
5858
makeNodeTransport,
5959
defaultStackParser,
6060
getSentryRelease,

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import type { Span } from '@opentelemetry/api';
44
import { context, trace } from '@opentelemetry/api';
55
import type {
6-
Breadcrumb,
7-
BreadcrumbHint,
86
CaptureContext,
97
Event,
108
EventHint,
@@ -15,7 +13,6 @@ import type {
1513
SeverityLevel,
1614
User,
1715
} from '@sentry/types';
18-
import { consoleSandbox, dateTimestampInSeconds } from '@sentry/utils';
1916
import { getContextFromScope, getScopesFromContext, setScopesOnContext } from '../utils/contextData';
2017

2118
import type { ExclusiveEventHintOrCaptureContext } from '../utils/prepareEvent';
@@ -116,29 +113,6 @@ export function captureEvent(event: Event, hint?: EventHint): string {
116113
return getCurrentScope().captureEvent(event, hint);
117114
}
118115

119-
/**
120-
* Add a breadcrumb to the current isolation scope.
121-
*/
122-
export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): void {
123-
const client = getClient();
124-
125-
const { beforeBreadcrumb, maxBreadcrumbs } = client.getOptions();
126-
127-
if (maxBreadcrumbs && maxBreadcrumbs <= 0) return;
128-
129-
const timestamp = dateTimestampInSeconds();
130-
const mergedBreadcrumb = { timestamp, ...breadcrumb };
131-
const finalBreadcrumb = beforeBreadcrumb
132-
? (consoleSandbox(() => beforeBreadcrumb(mergedBreadcrumb, hint)) as Breadcrumb | null)
133-
: mergedBreadcrumb;
134-
135-
if (finalBreadcrumb === null) return;
136-
137-
client.emit('beforeAddBreadcrumb', finalBreadcrumb, hint);
138-
139-
getIsolationScope().addBreadcrumb(finalBreadcrumb, maxBreadcrumbs);
140-
}
141-
142116
/**
143117
* Add a global event processor.
144118
*/

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import type {
99
TransactionContext,
1010
} from '@sentry/types';
1111

12-
import { endSession, startSession } from '@sentry/core';
12+
import { addBreadcrumb, endSession, startSession } from '@sentry/core';
1313
import {
14-
addBreadcrumb,
1514
captureEvent,
1615
getClient,
1716
getCurrentScope,

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getGlobalScope as _getGlobalScope, setGlobalScope } from '@sentry/core';
22
import { OpenTelemetryScope } from '@sentry/opentelemetry';
3-
import type { Breadcrumb, Client, Event, EventHint, SeverityLevel } from '@sentry/types';
3+
import type { Client, Event, EventHint, SeverityLevel } from '@sentry/types';
44
import { uuid4 } from '@sentry/utils';
55

66
import { getGlobalCarrier } from './globals';
@@ -161,13 +161,6 @@ export class Scope extends OpenTelemetryScope implements ScopeInterface {
161161
return eventId;
162162
}
163163

164-
/**
165-
* @inheritDoc
166-
*/
167-
public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this {
168-
return this._addBreadcrumb(breadcrumb, maxBreadcrumbs);
169-
}
170-
171164
/** Get scope data for this scope only. */
172165
public getOwnScopeData(): ScopeData {
173166
return super.getScopeData();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { captureException, withScope } from '@sentry/core';
1+
import { addBreadcrumb, captureException, withScope } from '@sentry/core';
22
import { startSpan } from '@sentry/opentelemetry';
3-
import { addBreadcrumb, getClient, withIsolationScope } from '../../src/sdk/api';
3+
import { getClient, withIsolationScope } from '../../src/sdk/api';
44

55
import type { NodeExperimentalClient } from '../../src/types';
66
import { cleanupOtel, mockSdkInit } from '../helpers/mockSdkInit';

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ describe('Integration | Scope', () => {
4141
scope2.setTag('tag3', 'val3');
4242

4343
Sentry.startSpan({ name: 'outer' }, span => {
44-
// TODO: This is "incorrect" until we stop cloning the current scope for setSpanScopes
45-
// Once we change this, the scopes _should_ be the same again
46-
if (enableTracing) {
47-
expect(getSpanScopes(span)?.scope).not.toBe(scope2);
48-
}
44+
expect(getSpanScopes(span)?.scope).toBe(enableTracing ? scope2 : undefined);
4945

5046
spanId = span.spanContext().spanId;
5147
traceId = span.spanContext().traceId;

packages/opentelemetry/src/custom/scope.ts

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@ import type { Span } from '@opentelemetry/api';
22
import type { TimedEvent } from '@opentelemetry/sdk-trace-base';
33
import { Scope } from '@sentry/core';
44
import type { Breadcrumb, ScopeData, SeverityLevel, Span as SentrySpan } from '@sentry/types';
5-
import { dateTimestampInSeconds, dropUndefinedKeys, logger, normalize } from '@sentry/utils';
5+
import { dropUndefinedKeys, logger } from '@sentry/utils';
66

77
import { DEBUG_BUILD } from '../debug-build';
88
import { InternalSentrySemanticAttributes } from '../semanticAttributes';
99
import { convertOtelTimeToSeconds } from '../utils/convertOtelTimeToSeconds';
10-
import { getActiveSpan, getRootSpan } from '../utils/getActiveSpan';
10+
import { getActiveSpan } from '../utils/getActiveSpan';
1111
import { getSpanParent } from '../utils/spanData';
1212
import { spanHasEvents } from '../utils/spanTypes';
1313

1414
/** A fork of the classic scope with some otel specific stuff. */
1515
export class OpenTelemetryScope extends Scope {
16-
/**
17-
* This can be set to ensure the scope uses _this_ span as the active one,
18-
* instead of using getActiveSpan().
19-
*/
20-
public activeSpan: Span | undefined;
21-
2216
/**
2317
* @inheritDoc
2418
*/
@@ -71,26 +65,6 @@ export class OpenTelemetryScope extends Scope {
7165
return this;
7266
}
7367

74-
/**
75-
* @inheritDoc
76-
*/
77-
public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this {
78-
const activeSpan = this.activeSpan || getActiveSpan();
79-
const rootSpan = activeSpan ? getRootSpan(activeSpan) : undefined;
80-
81-
if (rootSpan) {
82-
const mergedBreadcrumb = {
83-
timestamp: dateTimestampInSeconds(),
84-
...breadcrumb,
85-
};
86-
87-
rootSpan.addEvent(...breadcrumbToOtelEvent(mergedBreadcrumb));
88-
return this;
89-
}
90-
91-
return this._addBreadcrumb(breadcrumb, maxBreadcrumbs);
92-
}
93-
9468
/** @inheritDoc */
9569
public getScopeData(): ScopeData {
9670
const data = super.getScopeData();
@@ -100,17 +74,11 @@ export class OpenTelemetryScope extends Scope {
10074
return data;
10175
}
10276

103-
/** Add a breadcrumb to this scope. */
104-
protected _addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this {
105-
return super.addBreadcrumb(breadcrumb, maxBreadcrumbs);
106-
}
107-
10877
/**
10978
* @inheritDoc
11079
*/
11180
protected _getBreadcrumbs(): Breadcrumb[] {
112-
const span = this.activeSpan || getActiveSpan();
113-
81+
const span = getActiveSpan();
11482
const spanBreadcrumbs = span ? getBreadcrumbsForSpan(span) : [];
11583

11684
return spanBreadcrumbs.length > 0 ? this._breadcrumbs.concat(spanBreadcrumbs) : this._breadcrumbs;
@@ -126,39 +94,6 @@ function getBreadcrumbsForSpan(span: Span): Breadcrumb[] {
12694
return events.map(otelEventToBreadcrumb);
12795
}
12896

129-
function breadcrumbToOtelEvent(breadcrumb: Breadcrumb): Parameters<Span['addEvent']> {
130-
const name = breadcrumb.message || '<no message>';
131-
132-
const dataAttrs = serializeBreadcrumbData(breadcrumb.data);
133-
134-
return [
135-
name,
136-
dropUndefinedKeys({
137-
[InternalSentrySemanticAttributes.BREADCRUMB_TYPE]: breadcrumb.type,
138-
[InternalSentrySemanticAttributes.BREADCRUMB_LEVEL]: breadcrumb.level,
139-
[InternalSentrySemanticAttributes.BREADCRUMB_EVENT_ID]: breadcrumb.event_id,
140-
[InternalSentrySemanticAttributes.BREADCRUMB_CATEGORY]: breadcrumb.category,
141-
...dataAttrs,
142-
}),
143-
breadcrumb.timestamp ? new Date(breadcrumb.timestamp * 1000) : undefined,
144-
];
145-
}
146-
147-
function serializeBreadcrumbData(data: Breadcrumb['data']): undefined | Record<string, unknown> {
148-
if (!data || Object.keys(data).length === 0) {
149-
return undefined;
150-
}
151-
152-
try {
153-
const normalizedData = normalize(data);
154-
return {
155-
[InternalSentrySemanticAttributes.BREADCRUMB_DATA]: JSON.stringify(normalizedData),
156-
};
157-
} catch (e) {
158-
return undefined;
159-
}
160-
}
161-
16297
function otelEventToBreadcrumb(event: TimedEvent): Breadcrumb {
16398
const attributes = event.attributes || {};
16499

packages/opentelemetry/src/spanProcessor.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,7 @@ function onSpanStart(span: Span, parentContext: Context, _ScopeClass: typeof Ope
3737
// eslint-disable-next-line deprecation/deprecation
3838
const isolationScope = actualHub.getIsolationScope();
3939
setSpanHub(span, actualHub);
40-
41-
// Use this scope for finishing the span
42-
// TODO: For now we need to clone this, as we need to store the `activeSpan` on it
43-
// Once we can get rid of this (when we move breadcrumbs to the isolation scope),
44-
// we can stop cloning this here
45-
const finishScope = (scope as OpenTelemetryScope).clone();
46-
// this is needed for breadcrumbs, for now, as they are stored on the span currently
47-
finishScope.activeSpan = span;
48-
setSpanScopes(span, { scope: finishScope, isolationScope });
40+
setSpanScopes(span, { scope, isolationScope });
4941
}
5042
}
5143

0 commit comments

Comments
 (0)