Skip to content

Commit 0c779fc

Browse files
committed
fix span status in applyOtelSpanData
1 parent 4bb0077 commit 0c779fc

3 files changed

Lines changed: 23 additions & 27 deletions

File tree

dev-packages/deno-integration-tests/suites/orchestrion-aws/test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts';
88
import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts';
99
import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts';
1010
import { resetGlobals, transactionSink, withTimeout } from '../../src/index.ts';
11+
import { SENTRY_OP } from '@sentry/conventions/attributes';
1112

1213
Deno.test('aws-sdk instrumentation: included in default integrations (Deno 2.8.0+)', () => {
1314
resetGlobals();
@@ -37,7 +38,7 @@ Deno.test('aws-sdk instrumentation: orchestrion @smithy/smithy-client:send chann
3738
// child has actually ended and can be captured on the transaction.
3839
const rpcSpanEnded = new Promise<void>(resolve => {
3940
client.on('spanEnd', (span: Span) => {
40-
if (spanToJSON(span).op === 'rpc') {
41+
if (spanToJSON(span).attributes[SENTRY_OP] === 'rpc') {
4142
resolve();
4243
}
4344
});

packages/opentelemetry/src/applyOtelSpanData.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
getClient,
44
hasSpanStreamingEnabled,
55
spanToJSON,
6+
spanToStaticSpanJSON,
67
SPAN_STATUS_ERROR,
78
SPAN_STATUS_OK,
89
isStatusErrorMessageValid,
@@ -21,7 +22,14 @@ export function applyOtelSpanData(span: Span, options: { finalizeStatus?: boolea
2122
if (options.finalizeStatus) {
2223
applyOtelCompatibilityAttributes(span, attributes);
2324
const client = getClient();
24-
applyOtelSpanStatus(span, attributes, spanJSON.status, !!client && hasSpanStreamingEnabled(client));
25+
// The status message (`not_found`, `internal_error`, …) is what we branch on here, so read it from
26+
// the static representation — `spanToJSON` only narrows to `'ok' | 'error'`.
27+
applyOtelSpanStatus(
28+
span,
29+
attributes,
30+
spanToStaticSpanJSON(span).status,
31+
!!client && hasSpanStreamingEnabled(client),
32+
);
2533
}
2634
}
2735

packages/opentelemetry/test/tracerProvider.test.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import {
44
getActiveSpan,
55
getCapturedScopesOnSpan,
66
getRootSpan,
7-
SEMANTIC_ATTRIBUTE_SENTRY_OP,
8-
SEMANTIC_ATTRIBUTE_SENTRY_STATUS_MESSAGE,
97
spanToJSON,
8+
spanToStaticSpanJSON,
109
SPAN_STATUS_ERROR,
1110
SPAN_STATUS_OK,
1211
startSpanManual,
@@ -136,30 +135,34 @@ describe('SentryTracerProvider', () => {
136135
it('finalizes span statuses like the OpenTelemetry exporter', () => {
137136
const okSpan = trace.getTracer('test').startSpan('ok');
138137
applyOtelSpanData(okSpan as Span, { finalizeStatus: true });
138+
expect(spanToStaticSpanJSON(okSpan as Span).status).toBe('ok');
139139
expect(spanToJSON(okSpan as Span).status).toBe('ok');
140+
expect(spanToJSON(okSpan as Span).attributes).not.toHaveProperty('sentry.status.message');
140141

141142
const httpErrorSpan = trace.getTracer('test').startSpan('http-error');
142143
httpErrorSpan.setAttribute('http.response.status_code', 500);
143144
applyOtelSpanData(httpErrorSpan as Span, { finalizeStatus: true });
144-
expect(spanToJSON(httpErrorSpan as Span).attributes[SEMANTIC_ATTRIBUTE_SENTRY_STATUS_MESSAGE]).toBe(
145-
'internal_error',
146-
);
145+
expect(spanToStaticSpanJSON(httpErrorSpan as Span).status).toBe('internal_error');
146+
expect(spanToJSON(httpErrorSpan as Span).status).toBe('error');
147+
expect(spanToJSON(httpErrorSpan as Span).attributes).toMatchObject({
148+
'sentry.status.message': 'internal_error',
149+
});
147150

148151
const legacyHttpErrorSpan = trace.getTracer('test').startSpan('legacy-http-error');
149152
legacyHttpErrorSpan.setAttribute('http.status_code', 500);
150153
applyOtelSpanData(legacyHttpErrorSpan as Span, { finalizeStatus: true });
154+
expect(spanToStaticSpanJSON(legacyHttpErrorSpan as Span).status).toBe('internal_error');
155+
expect(spanToJSON(legacyHttpErrorSpan as Span).status).toBe('error');
151156
expect(spanToJSON(legacyHttpErrorSpan as Span).attributes).toMatchObject({
152-
[SEMANTIC_ATTRIBUTE_SENTRY_STATUS_MESSAGE]: 'internal_error',
153157
'http.response.status_code': 500,
154158
'http.status_code': 500,
159+
'sentry.status.message': 'internal_error',
155160
});
156161

157162
const customErrorSpan = trace.getTracer('test').startSpan('custom-error');
158163
customErrorSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'This is a custom error' });
159164
applyOtelSpanData(customErrorSpan as Span, { finalizeStatus: true });
160-
expect(spanToJSON(customErrorSpan as Span).attributes[SEMANTIC_ATTRIBUTE_SENTRY_STATUS_MESSAGE]).toBe(
161-
'internal_error',
162-
);
165+
expect(spanToStaticSpanJSON(customErrorSpan as Span).status).toBe('internal_error');
163166
});
164167

165168
it('preserves an explicit OK status when finalizing', () => {
@@ -194,20 +197,4 @@ describe('SentryTracerProvider', () => {
194197
expect(streamed.status).toBe('error');
195198
expect(streamed.attributes?.['sentry.status.message']).toBe('Cannot enqueue Query after fatal error.');
196199
});
197-
<<<<<<< HEAD
198-
=======
199-
200-
it('infers op for HTTP server spans', () => {
201-
const span = trace.getTracer('test').startSpan('GET', {
202-
kind: SpanKind.SERVER,
203-
attributes: {
204-
'http.method': 'GET',
205-
'http.route': '/my-path/:id',
206-
},
207-
});
208-
209-
const json = spanToJSON(span as Span);
210-
expect(json.attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBe('http.server');
211-
});
212-
>>>>>>> 598470e78d (feat(core)!: Return `StreamedSpanJSON` from `spanToJSON`)
213200
});

0 commit comments

Comments
 (0)