diff --git a/packages/opentelemetry/src/sampler.ts b/packages/opentelemetry/src/sampler.ts index 6da0df78ac7d..446c325f3ac7 100644 --- a/packages/opentelemetry/src/sampler.ts +++ b/packages/opentelemetry/src/sampler.ts @@ -70,11 +70,14 @@ export class SentrySampler implements Sampler { } = inferSpanData(spanName, spanAttributes, spanKind); const mergedAttributes = { - [SEMANTIC_ATTRIBUTE_SENTRY_OP]: op, ...inferredAttributes, ...spanAttributes, }; + if (op) { + mergedAttributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] = op; + } + const mutableSamplingDecision = { decision: true }; this._client.emit( 'beforeSampling', diff --git a/packages/opentelemetry/src/trace.ts b/packages/opentelemetry/src/trace.ts index 6f9fe5dad6d1..54195beb61ee 100644 --- a/packages/opentelemetry/src/trace.ts +++ b/packages/opentelemetry/src/trace.ts @@ -45,8 +45,6 @@ export function startSpan(options: OpenTelemetrySpanContext, callback: (span: const spanOptions = getSpanOptions(options); return tracer.startActiveSpan(name, spanOptions, ctx, span => { - _applySentryAttributesToSpan(span, options); - return handleCallbackErrors( () => callback(span), () => { @@ -90,8 +88,6 @@ export function startSpanManual( const spanOptions = getSpanOptions(options); return tracer.startActiveSpan(name, spanOptions, ctx, span => { - _applySentryAttributesToSpan(span, options); - return handleCallbackErrors( () => callback(span, () => span.end()), () => { @@ -131,8 +127,6 @@ export function startInactiveSpan(options: OpenTelemetrySpanContext): Span { const span = tracer.startSpan(name, spanOptions, ctx); - _applySentryAttributesToSpan(span, options); - return span; }); } @@ -156,22 +150,19 @@ function getTracer(): Tracer { return (client && client.tracer) || trace.getTracer('@sentry/opentelemetry', SDK_VERSION); } -function _applySentryAttributesToSpan(span: Span, options: OpenTelemetrySpanContext): void { - const { op } = options; - - if (op) { - span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, op); - } -} - function getSpanOptions(options: OpenTelemetrySpanContext): SpanOptions { - const { startTime, attributes, kind } = options; + const { startTime, attributes, kind, op } = options; // OTEL expects timestamps in ms, not seconds const fixedStartTime = typeof startTime === 'number' ? ensureTimestampInMilliseconds(startTime) : startTime; return { - attributes, + attributes: op + ? { + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: op, + ...attributes, + } + : attributes, kind, startTime: fixedStartTime, }; diff --git a/packages/opentelemetry/test/trace.test.ts b/packages/opentelemetry/test/trace.test.ts index 2332fd1ced05..5d9329650969 100644 --- a/packages/opentelemetry/test/trace.test.ts +++ b/packages/opentelemetry/test/trace.test.ts @@ -1320,9 +1320,7 @@ describe('trace (sampling)', () => { expect(tracesSampler).toHaveBeenLastCalledWith({ parentSampled: undefined, name: 'outer', - attributes: { - [SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1, - }, + attributes: {}, transactionContext: { name: 'outer', parentSampled: undefined }, }); @@ -1357,16 +1355,25 @@ describe('trace (sampling)', () => { mockSdkInit({ tracesSampler }); - startSpan({ name: 'outer' }, outerSpan => { - expect(outerSpan).toBeDefined(); - }); + startSpan( + { + name: 'outer', + op: 'test.op', + attributes: { attr1: 'yes', attr2: 1 }, + }, + outerSpan => { + expect(outerSpan).toBeDefined(); + }, + ); - expect(tracesSampler).toBeCalledTimes(1); + expect(tracesSampler).toHaveBeenCalledTimes(1); expect(tracesSampler).toHaveBeenLastCalledWith({ parentSampled: undefined, name: 'outer', attributes: { - [SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: 1, + attr1: 'yes', + attr2: 1, + 'sentry.op': 'test.op', }, transactionContext: { name: 'outer', parentSampled: undefined }, });