-
Notifications
You must be signed in to change notification settings - Fork 344
Why doesn't Span support try-with-resources? #361
Comments
Having said that, since |
As far as I recall it was an intentional decision to remove (Auto-) Span span = tracer.buildSpan("someWork").start();
try (Scope scope = tracer.scopeManager().activate(span)) {
// Do things.
} catch(Exception ex) {
Tags.ERROR.set(span, true);
span.log(Map.of(Fields.EVENT, "error", Fields.ERROR_OBJECT, ex, Fields.MESSAGE, ex.getMessage()));
} finally {
span.finish();
} Using the |
@sjoerdtalsma that sounds like a fair point. I've only been experimenting with OpenTracing so far, and I haven't gone into detail concerning error logging, so I haven't had that issue yet. Either way, the docs (https://opentracing.io/docs/getting-started/), which still use try-with-resources, need to be adapted to reflect the latest version. When I opened the issue, I couldn't find that problem right away, so I left it out at first. I'll edit it in now. |
One could always define their own custom wrapper that combines the try (final AutoCloseableSpan span = autoclosing(tracer.buildSpan("test").start())) {
// TODO use
} Sample implementation (using lombok because I'm lazy): @AllArgsConstructor
final class SimpleAutoCloseableSpan implements Span, AutoCloseable {
@Delegate
private final Span span;
@Override
public void close() {
finish();
}
} |
In an old issue (#102), it was stated that the Span interface does not extend AutoCloseable for java 1.6 compatibility.
This change seems to have been requested in yet another (obviously older) issue (#75).
So far no problem - if one is not using Java 6,
Closeable
extendsAutoCloseable
, so there should be no problem here.However, as of the most current version (master > Span.java), the
Span
interface extends neither of those interfaces. This is quite unfortunate, as it means that the following is not possible:Instead, something like the following is required to do the same thing:
By combing through the history of the
Span
interface, it turns out that this commit, created by @bhs, removed theCloseable
interface from theSpan
. Simultaneously, theActiveSpan
"gained" theCloseable
interface.Now, I'm not sure if that change was intentional or not (or what the
ActiveSpan
interface was even meant to do), but there does not seem to be anActiveSpan
interface at all in the current version. I can't find something else that extends or implementsCloseable
either.Now, as I'm merely a beginner in simply using OpenTracing (let alone developing it!), I may be missing something. Either way, I fail to see why the the
Closeable
interface is no longer implemented bySpan
. Can anyone elaborate on this?(Note: I am aware that OpenTracing and OpenCensus are merging into OpenTelemetry. If this problem is solved with OpenTelemetry, that's fine with me.)
//edit: The docs still use try-with-resources-based code. If this functionality won't be re-implemented, the docs should be changed to reflect the latest version.
The text was updated successfully, but these errors were encountered: