Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Add a warning about try-with-resource to auto-close spans. #1362

Merged
merged 1 commit into from
Aug 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/src/main/java/io/opencensus/trace/SpanBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ public SpanBuilder setSpanKind(@Nullable Span.Kind spanKind) {
* }
* }</pre>
*
* <p>WARNING: The try-with-resources feature to auto-close spans as described above can sound
* very tempting due to its convenience, but it comes with an important and easy-to-miss
* trade-off: the span will be closed before any {@code catch} or {@code finally} blocks get a
* chance to execute. So if you need to catch any exceptions and log information about them (for
* example), then you do not want to use the try-with-resources shortcut because that logging will
* not be tagged with the span info of the span it logically falls under, and if you try to
* retrieve {@code Tracer.getCurrentSpan()} then you'll either get the parent span if one exists
* or {@code BlankSpan} if there was no parent span. This can be confusing and seem
* counter-intuitive, but it's the way try-with-resources works.
*
* @return an object that defines a scope where the newly created {@code Span} will be set to the
* current Context.
* @since 0.5
Expand Down