Skip to content

Commit e1063f4

Browse files
authored
ref(node): Align semantic attribute handling (#10827)
Also stop clearing out sentry attributes, we don't actually want that (except for the parent sampled stuff for now).
1 parent ba80f79 commit e1063f4

File tree

7 files changed

+65
-38
lines changed

7 files changed

+65
-38
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ describe('Integration | Transactions', () => {
7070
otel: {
7171
attributes: {
7272
'test.outer': 'test value',
73+
'sentry.op': 'test op',
74+
'sentry.origin': 'auto.test',
75+
'sentry.source': 'task',
7376
},
7477
resource: {
7578
'service.name': 'node-experimental',
@@ -241,6 +244,9 @@ describe('Integration | Transactions', () => {
241244
otel: expect.objectContaining({
242245
attributes: {
243246
'test.outer': 'test value',
247+
'sentry.op': 'test op',
248+
'sentry.origin': 'auto.test',
249+
'sentry.source': 'task',
244250
},
245251
}),
246252
trace: {
@@ -283,6 +289,7 @@ describe('Integration | Transactions', () => {
283289
otel: expect.objectContaining({
284290
attributes: {
285291
'test.outer': 'test value b',
292+
'sentry.op': 'test op b',
286293
},
287294
}),
288295
trace: {
@@ -513,7 +520,11 @@ describe('Integration | Transactions', () => {
513520
expect.objectContaining({
514521
contexts: expect.objectContaining({
515522
otel: expect.objectContaining({
516-
attributes: {},
523+
attributes: {
524+
'sentry.op': 'test op',
525+
'sentry.origin': 'auto.test',
526+
'sentry.source': 'task',
527+
},
517528
}),
518529
trace: {
519530
data: {

packages/opentelemetry/src/semanticAttributes.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,5 @@
33
* No guarantees apply to these attributes, and the may change/disappear at any time.
44
*/
55
export const InternalSentrySemanticAttributes = {
6-
ORIGIN: 'sentry.origin',
7-
OP: 'sentry.op',
8-
SOURCE: 'sentry.source',
96
PARENT_SAMPLED: 'sentry.parentSampled',
10-
BREADCRUMB_TYPE: 'sentry.breadcrumb.type',
11-
BREADCRUMB_LEVEL: 'sentry.breadcrumb.level',
12-
BREADCRUMB_EVENT_ID: 'sentry.breadcrumb.event_id',
13-
BREADCRUMB_CATEGORY: 'sentry.breadcrumb.category',
14-
BREADCRUMB_DATA: 'sentry.breadcrumb.data',
157
} as const;

packages/opentelemetry/src/spanExporter.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import { ExportResultCode } from '@opentelemetry/core';
44
import type { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base';
55
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
66
import type { Transaction } from '@sentry/core';
7-
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
8-
import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, getCurrentHub } from '@sentry/core';
7+
import {
8+
SEMANTIC_ATTRIBUTE_SENTRY_OP,
9+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
10+
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
11+
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
12+
getCurrentHub,
13+
} from '@sentry/core';
914
import type { Scope, Span as SentrySpan, SpanOrigin, TransactionSource } from '@sentry/types';
1015
import { addNonEnumerableProperty, dropUndefinedKeys, logger } from '@sentry/utils';
1116
import { startTransaction } from './custom/transaction';
@@ -132,9 +137,9 @@ function shouldCleanupSpan(span: ReadableSpan, maxStartTimeOffsetSeconds: number
132137
function parseSpan(span: ReadableSpan): { op?: string; origin?: SpanOrigin; source?: TransactionSource } {
133138
const attributes = span.attributes;
134139

135-
const origin = attributes[InternalSentrySemanticAttributes.ORIGIN] as SpanOrigin | undefined;
136-
const op = attributes[InternalSentrySemanticAttributes.OP] as string | undefined;
137-
const source = attributes[InternalSentrySemanticAttributes.SOURCE] as TransactionSource | undefined;
140+
const origin = attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] as SpanOrigin | undefined;
141+
const op = attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] as string | undefined;
142+
const source = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] as TransactionSource | undefined;
138143

139144
return { origin, op, source };
140145
}
@@ -278,11 +283,6 @@ function removeSentryAttributes(data: Record<string, unknown>): Record<string, u
278283

279284
/* eslint-disable @typescript-eslint/no-dynamic-delete */
280285
delete cleanedData[InternalSentrySemanticAttributes.PARENT_SAMPLED];
281-
delete cleanedData[InternalSentrySemanticAttributes.ORIGIN];
282-
delete cleanedData[InternalSentrySemanticAttributes.OP];
283-
delete cleanedData[InternalSentrySemanticAttributes.SOURCE];
284-
// We want to avoid having this on each span (as that is set by the Sampler)
285-
// We only want this on the transaction, where we manually add it to `attributes`
286286
delete cleanedData[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE];
287287
/* eslint-enable @typescript-eslint/no-dynamic-delete */
288288

packages/opentelemetry/src/trace.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ import { TraceFlags } from '@opentelemetry/api';
33
import { context } from '@opentelemetry/api';
44
import { SpanStatusCode, trace } from '@opentelemetry/api';
55
import { TraceState, suppressTracing } from '@opentelemetry/core';
6-
import { SDK_VERSION, getClient, getCurrentScope, handleCallbackErrors } from '@sentry/core';
6+
import {
7+
SDK_VERSION,
8+
SEMANTIC_ATTRIBUTE_SENTRY_OP,
9+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
10+
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
11+
getClient,
12+
getCurrentScope,
13+
handleCallbackErrors,
14+
} from '@sentry/core';
715
import type { Client, Scope } from '@sentry/types';
816
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
917
import { SENTRY_TRACE_STATE_DSC } from './constants';
1018

11-
import { InternalSentrySemanticAttributes } from './semanticAttributes';
1219
import type { OpenTelemetryClient, OpenTelemetrySpanContext } from './types';
1320
import { getContextFromScope } from './utils/contextData';
1421
import { getDynamicSamplingContextFromSpan } from './utils/dynamicSamplingContext';
@@ -137,15 +144,15 @@ function _applySentryAttributesToSpan(span: Span, options: OpenTelemetrySpanCont
137144
const { origin, op, source, metadata } = options;
138145

139146
if (origin) {
140-
span.setAttribute(InternalSentrySemanticAttributes.ORIGIN, origin);
147+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, origin);
141148
}
142149

143150
if (op) {
144-
span.setAttribute(InternalSentrySemanticAttributes.OP, op);
151+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, op);
145152
}
146153

147154
if (source) {
148-
span.setAttribute(InternalSentrySemanticAttributes.SOURCE, source);
155+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
149156
}
150157

151158
if (metadata) {
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { Span } from '@opentelemetry/api';
2+
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
23
import type { SpanOrigin } from '@sentry/types';
34

4-
import { InternalSentrySemanticAttributes } from '../semanticAttributes';
5-
65
/** Adds an origin to an OTEL Span. */
76
export function addOriginToSpan(span: Span, origin: SpanOrigin): void {
8-
span.setAttribute(InternalSentrySemanticAttributes.ORIGIN, origin);
7+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, origin);
98
}

packages/opentelemetry/test/integration/transactions.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ describe('Integration | Transactions', () => {
7777
otel: {
7878
attributes: {
7979
'test.outer': 'test value',
80+
'sentry.op': 'test op',
81+
'sentry.origin': 'auto.test',
82+
'sentry.source': 'task',
8083
},
8184
resource: {
8285
'service.name': 'opentelemetry-test',
@@ -245,6 +248,9 @@ describe('Integration | Transactions', () => {
245248
otel: expect.objectContaining({
246249
attributes: {
247250
'test.outer': 'test value',
251+
'sentry.op': 'test op',
252+
'sentry.origin': 'auto.test',
253+
'sentry.source': 'task',
248254
},
249255
}),
250256
trace: {
@@ -287,6 +293,7 @@ describe('Integration | Transactions', () => {
287293
otel: expect.objectContaining({
288294
attributes: {
289295
'test.outer': 'test value b',
296+
'sentry.op': 'test op b',
290297
},
291298
}),
292299
trace: {
@@ -364,7 +371,11 @@ describe('Integration | Transactions', () => {
364371
expect.objectContaining({
365372
contexts: expect.objectContaining({
366373
otel: expect.objectContaining({
367-
attributes: {},
374+
attributes: {
375+
'sentry.op': 'test op',
376+
'sentry.origin': 'auto.test',
377+
'sentry.source': 'task',
378+
},
368379
}),
369380
trace: {
370381
data: {

packages/opentelemetry/test/trace.test.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ import { SpanKind } from '@opentelemetry/api';
33
import { TraceFlags, context, trace } from '@opentelemetry/api';
44
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
55
import { Span as SpanClass } from '@opentelemetry/sdk-trace-base';
6-
import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, getClient, getCurrentScope, spanToJSON } from '@sentry/core';
6+
import {
7+
SEMANTIC_ATTRIBUTE_SENTRY_OP,
8+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
9+
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
10+
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
11+
getClient,
12+
getCurrentScope,
13+
spanToJSON,
14+
} from '@sentry/core';
715
import type { Event, PropagationContext, Scope } from '@sentry/types';
816

9-
import { InternalSentrySemanticAttributes } from '../src/semanticAttributes';
1017
import { startInactiveSpan, startSpan, startSpanManual } from '../src/trace';
1118
import type { AbstractSpan } from '../src/types';
1219
import { setPropagationContextOnContext } from '../src/utils/contextData';
@@ -220,15 +227,15 @@ describe('trace', () => {
220227
origin: 'auto.test.origin',
221228
metadata: { requestPath: 'test-path' },
222229
attributes: {
223-
[InternalSentrySemanticAttributes.SOURCE]: 'task',
230+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'task',
224231
},
225232
},
226233
span => {
227234
expect(span).toBeDefined();
228235
expect(getSpanAttributes(span)).toEqual({
229-
[InternalSentrySemanticAttributes.SOURCE]: 'task',
230-
[InternalSentrySemanticAttributes.ORIGIN]: 'auto.test.origin',
231-
[InternalSentrySemanticAttributes.OP]: 'my-op',
236+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'task',
237+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.test.origin',
238+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'my-op',
232239
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
233240
});
234241

@@ -481,17 +488,17 @@ describe('trace', () => {
481488
op: 'my-op',
482489
origin: 'auto.test.origin',
483490
attributes: {
484-
[InternalSentrySemanticAttributes.SOURCE]: 'task',
491+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'task',
485492
},
486493
metadata: { requestPath: 'test-path' },
487494
});
488495

489496
expect(span2).toBeDefined();
490497
expect(getSpanAttributes(span2)).toEqual({
491498
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1,
492-
[InternalSentrySemanticAttributes.SOURCE]: 'task',
493-
[InternalSentrySemanticAttributes.ORIGIN]: 'auto.test.origin',
494-
[InternalSentrySemanticAttributes.OP]: 'my-op',
499+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'task',
500+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.test.origin',
501+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'my-op',
495502
});
496503

497504
expect(getSpanMetadata(span2)).toEqual({ requestPath: 'test-path' });

0 commit comments

Comments
 (0)