Skip to content

Handle exceptions in Instrumenter.start() and Instrumenter.end() methods#16425

Draft
steverao wants to merge 2 commits intoopen-telemetry:mainfrom
steverao:suppressException
Draft

Handle exceptions in Instrumenter.start() and Instrumenter.end() methods#16425
steverao wants to merge 2 commits intoopen-telemetry:mainfrom
steverao:suppressException

Conversation

@steverao
Copy link
Copy Markdown
Contributor

Resolved #16376

@steverao steverao requested a review from a team as a code owner March 10, 2026 03:08
try {
return doStart(parentContext, request, null);
} catch (Throwable t) {
logStartException(t);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's assume that there was an exception thrown in start and we return the parent context. Now in end we'll end up closing the span from parent context which isn't ideal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, how about using a unique ContextKey to mark the Context that "start failed". If end() detects this mark, skip it directly to avoid erroneously operating on the parent span?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I doubt this would work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm wondering if your concern is about the edge case where the span is created but an exception is thrown afterwards (e.g., in OperationListener.onStart()).
In that case, the created span won't be properly ended. How about a solution that wraps the span creation and listener calls in try-catch to ensure the span is ended if an exception occurs after creation or do you have any suggestion?

Span span = null;
try {
    span = spanBuilder.startSpan();
    // ... listeners ...
    return spanSuppressor.storeInContext(context, spanKind, span);
} catch (Throwable t) {
    if (span != null) span.end();
    throw t;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't follow. If you rethrow the exception from start won't it defeat the purpose of catching the exception in first place?
Instead of trying to wrap the whole method in try catch you could add try catch around individual parts that are more likely to throw e.g. attribute extraction.

@steverao steverao assigned steverao and unassigned steverao Mar 10, 2026
@steverao steverao marked this pull request as draft March 10, 2026 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add try-catch logic for framework native instrumentation

2 participants