Search code examples
springspring-bootmicrometermicrometer-tracingspring-cloud-sleuth-otel

How can I set custom tags through properties when using Micrometer Tracing with Spring Boot 3


When using Spring Boot 2.x with Spring Sleuth based tracing, we can configure the custom tags for Spans using properties in the application.properties file.

Example:

spring.sleuth.otel.resource.attributes.CUSTOM-KEY=CUSTOM-VALUE

Since Spring Boot 3.x uses Micrometer tracing and not supporting Sleuth, Is there anyway to configure the custom tags in the above way or what is the best practice?


Solution

  • I used it and it worked

    @Bean
    public ServerRequestObservationConvention customTagsRequestObservationConvention(@Value("${observability.tracing.custom-value}") String globalCustomValue){
        return new DefaultServerRequestObservationConvention(){
            @Override
            public KeyValues getLowCardinalityKeyValues(ServerRequestObservationContext context) {
                return super.getLowCardinalityKeyValues(context).and(KeyValue.of("custom.key", globalCustomValue));
            }
        };
    }
    

    It will be caught by auto configuration WebMvcObservationAutoConfiguration