micronautopentracing

Using @SpanTag to add a custom attribute in traces


We are using micronaut tracing for distributed tracing. Which end up in new relic.

We want to add a new attribute/property which to a span, which appears in new relic within a trace. Suppose this property is in Object. I figured from the micronaut guide that adding a @SpanTag could be solution to it. But, I really do not have any other use of this property in the method, so it makes it an unused parameter. Is there another way?

Existing code:

@NewSpan("foo")
public void foo(Object obj) {
    otherService.bar(obj);
}

// to be
@NewSpan("foo")
public void foo(Object obj, @SpanTag("foo.property") String property) {
    // property argument is not used
    otherService.bar(obj);
}

Solution

  • You can inject a Tracer bean into your class.

    import io.opentracing.Tracer;
    
    public Foo {
       private final Tracer tracer;
       public Foo(Tracer tracer) { ... }
    
       @NewSpan("foo")
       public void foo(Object obj) {
          tracer.activeSpan().setTag("foo.property", property); // <1>
          ...
       }
    
    1. Might not 100% correct, but should be close.