6

Hy

I am using springboot 3 with the new micrometer observation. Is there a way to prevent generating a trace_id/span_id for some paths like /actuator/prometheus? Observation add a trace id for each call to /actuator/*.

Thank you

farid
  • 313
  • 1
  • 2
  • 7

2 Answers2

2

I managed to find a half solution to the problem, by defining the ObservationRegistry this way:

@Bean
@ConditionalOnMissingBean
ObservationRegistry observationRegistry() {
    PathMatcher pathMatcher = new AntPathMatcher("/");
    ObservationRegistry observationRegistry = ObservationRegistry.create();
    observationRegistry.observationConfig().observationPredicate((name, context) -> {
        if(context instanceof ServerRequestObservationContext) {
            return !pathMatcher.match("/actuator/**", ((ServerRequestObservationContext) context).getCarrier().getRequestURI());
        } else {
            return true;
        }
    });
    return observationRegistry;
}

This doesn't completely ignore the actuator requests, only the first span. So if you have for example Spring Security on your classpath, those spans are left intact.

EDIT: Looks like you don't need to redefine the entire observation registry, you can use a bean like the one show here: https://docs.spring.io/spring-security/reference/servlet/integrations/observability.html

EDIT2: From what I can tell, you need to include these 2 beans, and no actuator call will be traced (completely disables tracing for spring security too):

    @Bean
    ObservationRegistryCustomizer<ObservationRegistry> skipActuatorEndpointsFromObservation() {
        PathMatcher pathMatcher = new AntPathMatcher("/");
        return (registry) -> registry.observationConfig().observationPredicate((name, context) -> {
            if (context instanceof ServerRequestObservationContext observationContext) {
                return !pathMatcher.match("/actuator/**", observationContext.getCarrier().getRequestURI());
            } else {
                return true;
            }
        });
    }

    @Bean
    ObservationRegistryCustomizer<ObservationRegistry> skipSecuritySpansFromObservation() {
        return (registry) -> registry.observationConfig().observationPredicate((name, context) ->
                !name.startsWith("spring.security"));
    }

Also, you might want to keep an eye out on this issue: https://github.com/spring-projects/spring-framework/issues/29210

László Stahorszki
  • 1,102
  • 7
  • 23
0

You need to give more information about a problem, but i think you have set this line in application.propeties like:

management.endpoints.web.exposure.include=/actuator/*

But exists option like:

management.endpoints.web.exposure.exclude=/actuator/prometheus
dr34mer
  • 1
  • 2
  • hy dr34mer, thank you for the answer. But i need to include the /actuator/prometheus, i use it to monitor the application. I am using the new observability provided by springboot/micrometer. When prometheus call /actuator/prometheus to scrap metrics, this call will generate a trace/span so i want to prevent generating a traceid/spanid for this path. – farid Jan 17 '23 at 13:01
  • You use zipkin/sleuth I guess. Try with `spring.sleuth.web.skip-pattern=/actuator/prometheus` in your properties. I am not sure is that a correct regex. That should help. [https://github.com](https://github.com/spring-cloud/spring-cloud-sleuth/blob/v2.1.4.RELEASE/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/SleuthWebProperties.java#L34) – dr34mer Jan 17 '23 at 14:41
  • I am not using sleuth, it's no more supported in spring-boot 3. Spring-boot 3 use micrometer – farid Jan 18 '23 at 09:04