I’m working on implementing Micronaut tracing using OpenTelemetry and Zipkin in a project. The basic configuration is working correctly, but now I want to add a global attribute to each span.
My current approach involves using a class annotated with @Filter and implementing HttpServerFilter to intercept all requests coming to the API. The code is like this:
package com.project.bootable.tracing.interceptor;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MutableHttpResponse;
import io.micronaut.http.annotation.Filter;
import io.micronaut.http.filter.HttpServerFilter;
import io.micronaut.http.filter.ServerFilterChain;
import io.opentelemetry.api.trace.Span;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
@Filter("/**")
public class RequestInterceptor implements HttpServerFilter {
private static final Logger LOG = LoggerFactory.getLogger(RequestInterceptor.class);
@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
Span.current().setAttribute("custom.tag", UUID.randomUUID().toString());
return chain.proceed(request);
}
}
However, although the code appears to be configured correctly, upon reviewing the Zipkin interface, I do not see the attribute reflected in the span.
(https://i.sstatic.net/XIYUZlGc.png)
Interestingly, when I add the attribute from a controller, it does work and I can see the attribute in the Zipkin interface.
@Controller("/employee")
@RequiredArgsConstructor
public class EmployeeController {
private static final Logger LOG = LoggerFactory.getLogger(EmployeeController.class);
private final EmployeeService employeeService;
@Get("/searchByAge{?initialAge}{?endAge}")
public List<Employee> findByRangeAge(
@QueryValue Optional<Long> initialAge,
@QueryValue Optional<Long> endAge){
Span.current().setAttribute("custom.tag", UUID.randomUUID().toString());
return employeeService.findByAgeRange(initialAge.get(),endAge.get());
}
}
(https://i.sstatic.net/2Q8uBFM6.png)
What would be the correct implementation to add a global attribute to the span? I appreciate any guidance on this.
pd: I attach the otel configuration of the applicaction.yml file and the micronaut tracing dependency that I use.
# ...
otel:
traces:
exporter: zipkin
# ...
Micronaut tracing dependency
annotationProcessor("io.micronaut.tracing:micronaut-tracing-opentelemetry-annotation")
implementation("io.micronaut.tracing:micronaut-tracing-opentelemetry-http")
cmenchu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.