spring-bootspring-cloud-sleuthzipkinjaeger

How to add method name to sleuth spans?


im using

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

and jaeger to collect and show spans enter image description here

on screenshot only info myapp-app: get be4eed3 but it makes hard to investigate and find some particular method.

How to customize it to ${method name in controller} and add some additional info ?


Solution

  • You can Inject a SpanHandler bean and change the span name to the controller method name:

            @Bean
            SpanHandler spanHandler() {
                return new SpanHandler() {
                    @Override
                    public boolean end(TraceContext traceContext, MutableSpan span, Cause cause) {
                        String operation = span.tags().getOrDefault("mvc.controller.method", null);
                        if(operation != null) {
                            span.name(operation);
                        }
        
                       
                        return true;
              }
          }