Skip to content

ObservationFilter beans are not registered automatically #33968

@edwardsre

Description

@edwardsre

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions