spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Automatically register ObservationFilters

Open edwardsre opened this issue 2 years ago • 4 comments

The Spring Boot 3.0 Migration Guide indicates the following, but this does not appear to be the case.

You can contribute ObservationFilter beans to your application and Spring Boot will auto-configure them with the ObservationRegistry.

Steps to duplicate

I am using Spring Boot 3.0.2

Create an application with Spring Initializer with Spring Web, Lombok, and Actuator dependencies

Modify the main application class as follows

@SpringBootApplication
@RestController
@Slf4j
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

  @GetMapping("test")
  public String testEndpoint() {
    return "hello world";
  }

  @Bean
  public ObservationFilter customMethodFilter() {
    return context -> {
      log.info("Executing observation filter");
      if (context instanceof ServerRequestObservationContext observationContext) {
        context.addLowCardinalityKeyValue(KeyValue.of("custom.method", observationContext.getCarrier().getMethod()));
      }
      return context;
    };
  }
}

Run the application and perform an HTTP request to http://localhost:8080/test. Notice the log statement is not printed.

Add the following bean into the main application class. Based on the docs, this step shouldn't be necessary.

  @Bean
  public ObservationRegistryCustomizer<?> myCustomizer(ObservationFilter customMethodFilter) {
    return registry -> registry.observationConfig().observationFilter(customMethodFilter);
  }

Restart the application and perform the same HTTP request to http://localhost:8080/test. Notice the log statement is now printed indicating the filter is being registered and used.

It appears that ObservationRegistryConfigurer created by ObservationRegistryPostProcessor does not get injected with a ObjectProvider<ObservationFilter>, but all other aspects of the registry can be configured this way. Is this an omission, or is the documentation incorrect?

I also can't find any usages of ObservationRegistry.observationConfig().observationFilter(...) that would indicate auto configuration is registering ObservationFilter beans provided by the application as stated in the migration guide.

edwardsre avatar Jan 24 '23 19:01 edwardsre

I found the Spring Boot Docs to be more accurate for the behavior I am seeing. The migration guide should be updated to remove the false statement.

It would be great if ObservationFilter beans could be auto registered as well.

edwardsre avatar Jan 24 '23 21:01 edwardsre

Thanks for letting us know! I've removed the ObservationFilter line from the migration guide. I think it's a good idea to auto-register ObservationFilters.

mhalbritter avatar Jan 25 '23 08:01 mhalbritter

I wonder if we should consider this to be a bug of omission as we registered MeterFilter beans automatically in 2.x. Also, the statement in the migration guide must have come from somewhere. I wonder if we overlooked something in one of the many observability-related changes we made in 3.0. Flagging for a team meeting so that we can have a chat about what we'd like to do.

wilkinsona avatar Jan 25 '23 14:01 wilkinsona

I'd vote for assuming that this is a bug of omission. We should be auto injecting ObservationFilters to an ObservationRegistry.

marcingrzejszczak avatar Jan 27 '23 15:01 marcingrzejszczak