opentracing-java
opentracing-java copied to clipboard
Make GlobalTracer easier to use with java 8 (depends on #115)
In #115 GlobalTracer gets a new method:
* @return the {@link ActiveSpan active span}, or null if none could be found.
*/
ActiveSpan activeSpan();
One of the common uses of this method will be adding tags/logs to the active span if present, like this:
if(GlobalTracer.activeSpan() != null) {
GlobalTracer.activeSpan().setTag("tagged", true);
}
It's tempting to declare the return type as Optional and instead be able to do this:
GlobalTracer.activeSpan().ifPresent(span -> {
span.setTag("tagged", true)
});
But I guess we can't use java 8 in this project. So instead I propose one of two options:
- Add a method taking a
Runnable-like interface (something which allows throwing exceptions maybe?) and make this possible:
I'm not sure about the namingGlobalTracer.withActiveSpan(span -> span.setTag("tagged", true)) - Implement simplified
Optional<ActiveSpan>and return that.activeSpan()comes fromActiveSpanSourceand I'm not sure if it should be touched, so it would need to be returned from a new method.
I'd go with option 1.
Optional:
GlobalTracer.activeSpan().ifPresent(span -> {
span.setTag("tagged", true)
});
and
GlobalTracer.withActiveSpan(span -> span.setTag("tagged", true))
are not the same, null check is still required. I don't see any benefits there.
The intention was to make withActiveSpan have the same semantic - it would basically do the null check. Implementation would be something similar to:
public void withActiveSpan(Consumer<Span> consumer) {
if(activeSpan() != null) {
consumer.accept(span)
}
}
(my recent comment from #134 applies here as well: basically, that this API seems like the right way to do what's being proposed, but that I am hesitant to add API surface area for convenience purposes until we have more experience with ActiveSpan)