The Spring Boot 3.0 Migration Guide indicates the following, but this does not appear to be the case.
You can contribute
ObservationFilterbeans to your application and Spring Boot will auto-configure them with theObservationRegistry.
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.
Comment From: 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.
Comment From: mhalbritter
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.
Comment From: wilkinsona
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.
Comment From: marcingrzejszczak
I'd vote for assuming that this is a bug of omission. We should be auto injecting ObservationFilters to an ObservationRegistry.