Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(opentelemetry): Do not stomp span status when startSpan callback throws #11170

Merged
merged 2 commits into from
Mar 25, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/core/src/utils/spanUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ function ensureTimestampInSeconds(timestamp: number): number {

/**
* Convert a span to a JSON representation.
* Note that all fields returned here are optional and need to be guarded against.
*
* Note: Because of this, we currently have a circular type dependency (which we opted out of in package.json).
* This is not avoidable as we need `spanToJSON` in `spanUtils.ts`, which in turn is needed by `span.ts` for backwards compatibility.
* And `spanToJSON` needs the Span class from `span.ts` to check here.
*/
// Note: Because of this, we currently have a circular type dependency (which we opted out of in package.json).
// This is not avoidable as we need `spanToJSON` in `spanUtils.ts`, which in turn is needed by `span.ts` for backwards compatibility.
// And `spanToJSON` needs the Span class from `span.ts` to check here.
Comment on lines +89 to +91
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stumbled onto this but found that this comment had no place in JSDoc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that all fields returned here are optional and need to be guarded against.

I'd argue this might still be a helpful message but the types should hint that anyway.
Also just to double-check: Putting a line comment between the JSDoc block and the function declaration doesn't prevent the JSDoc from being associated with the function, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep the JSdoc still works!

export function spanToJSON(span: Span): Partial<SpanJSON> {
if (spanIsSentrySpan(span)) {
return span.getSpanJSON();
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry/src/spanExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ function createTransactionForOtelSpan(span: ReadableSpan): TransactionEvent {
data: attributes,
origin,
op,
status: getStatusMessage(status),
status: getStatusMessage(status), // As per protocol, span status is allowed to be undefined
});

const transactionEvent: TransactionEvent = {
Expand Down Expand Up @@ -252,7 +252,7 @@ function createAndFinishSpanForOtelSpan(node: SpanNode, spans: SpanJSON[], remai
start_timestamp: convertOtelTimeToSeconds(startTime),
// This is [0,0] by default in OTEL, in which case we want to interpret this as no end time
timestamp: convertOtelTimeToSeconds(endTime) || undefined,
status: getStatusMessage(status),
status: getStatusMessage(status), // As per protocol, span status is allowed to be undefined
op,
origin,
_metrics_summary: getMetricSummaryJsonForSpan(span as unknown as Span),
Expand Down
11 changes: 9 additions & 2 deletions packages/opentelemetry/src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getDynamicSamplingContextFromClient,
getRootSpan,
handleCallbackErrors,
spanToJSON,
} from '@sentry/core';
import type { Client, Scope } from '@sentry/types';
import { continueTraceAsRemoteSpan, getSamplingDecision, makeTraceState } from './propagator';
Expand Down Expand Up @@ -46,7 +47,10 @@ export function startSpan<T>(options: OpenTelemetrySpanContext, callback: (span:
return handleCallbackErrors(
() => callback(span),
() => {
span.setStatus({ code: SpanStatusCode.ERROR });
// Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
if (spanToJSON(span).status === undefined) {
span.setStatus({ code: SpanStatusCode.ERROR });
}
},
() => span.end(),
);
Expand Down Expand Up @@ -82,7 +86,10 @@ export function startSpanManual<T>(
return handleCallbackErrors(
() => callback(span, () => span.end()),
() => {
span.setStatus({ code: SpanStatusCode.ERROR });
// Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
if (spanToJSON(span).status === undefined) {
span.setStatus({ code: SpanStatusCode.ERROR });
}
},
);
});
Expand Down