diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfiguration.java index d7c531aa0e5b..216bc4262408 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import io.micrometer.core.instrument.observation.MeterObservationHandler; import io.micrometer.observation.GlobalObservationConvention; import io.micrometer.observation.Observation; +import io.micrometer.observation.ObservationFilter; import io.micrometer.observation.ObservationHandler; import io.micrometer.observation.ObservationPredicate; import io.micrometer.observation.ObservationRegistry; @@ -62,9 +63,10 @@ static ObservationRegistryPostProcessor observationRegistryPostProcessor( ObjectProvider observationPredicates, ObjectProvider> observationConventions, ObjectProvider> observationHandlers, - ObjectProvider observationHandlerGrouping) { + ObjectProvider observationHandlerGrouping, + ObjectProvider observationFilters) { return new ObservationRegistryPostProcessor(observationRegistryCustomizers, observationPredicates, - observationConventions, observationHandlers, observationHandlerGrouping); + observationConventions, observationHandlers, observationHandlerGrouping, observationFilters); } @Bean diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationRegistryConfigurer.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationRegistryConfigurer.java index e99288ed9592..f14ea3461828 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationRegistryConfigurer.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationRegistryConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import java.util.List; import io.micrometer.observation.GlobalObservationConvention; +import io.micrometer.observation.ObservationFilter; import io.micrometer.observation.ObservationHandler; import io.micrometer.observation.ObservationPredicate; import io.micrometer.observation.ObservationRegistry; @@ -48,22 +49,27 @@ class ObservationRegistryConfigurer { private final ObjectProvider observationHandlerGrouping; + private final ObjectProvider observationFilters; + ObservationRegistryConfigurer(ObjectProvider> customizers, ObjectProvider observationPredicates, ObjectProvider> observationConventions, ObjectProvider> observationHandlers, - ObjectProvider observationHandlerGrouping) { + ObjectProvider observationHandlerGrouping, + ObjectProvider observationFilters) { this.customizers = customizers; this.observationPredicates = observationPredicates; this.observationConventions = observationConventions; this.observationHandlers = observationHandlers; this.observationHandlerGrouping = observationHandlerGrouping; + this.observationFilters = observationFilters; } void configure(ObservationRegistry registry) { registerObservationPredicates(registry); registerGlobalObservationConventions(registry); registerHandlers(registry); + registerFilters(registry); customize(registry); } @@ -80,6 +86,10 @@ private void registerGlobalObservationConventions(ObservationRegistry registry) this.observationConventions.orderedStream().forEach(registry.observationConfig()::observationConvention); } + private void registerFilters(ObservationRegistry registry) { + this.observationFilters.orderedStream().forEach(registry.observationConfig()::observationFilter); + } + @SuppressWarnings("unchecked") private void customize(ObservationRegistry registry) { LambdaSafe.callbacks(ObservationRegistryCustomizer.class, asOrderedList(this.customizers), registry) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationRegistryPostProcessor.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationRegistryPostProcessor.java index 77d76d4a7906..9ad7563a4488 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationRegistryPostProcessor.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationRegistryPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.boot.actuate.autoconfigure.observation; import io.micrometer.observation.GlobalObservationConvention; +import io.micrometer.observation.ObservationFilter; import io.micrometer.observation.ObservationHandler; import io.micrometer.observation.ObservationPredicate; import io.micrometer.observation.ObservationRegistry; @@ -44,18 +45,22 @@ class ObservationRegistryPostProcessor implements BeanPostProcessor { private final ObjectProvider observationHandlerGrouping; + private final ObjectProvider observationFilters; + private volatile ObservationRegistryConfigurer configurer; ObservationRegistryPostProcessor(ObjectProvider> observationRegistryCustomizers, ObjectProvider observationPredicates, ObjectProvider> observationConventions, ObjectProvider> observationHandlers, - ObjectProvider observationHandlerGrouping) { + ObjectProvider observationHandlerGrouping, + ObjectProvider observationFilters) { this.observationRegistryCustomizers = observationRegistryCustomizers; this.observationPredicates = observationPredicates; this.observationConventions = observationConventions; this.observationHandlers = observationHandlers; this.observationHandlerGrouping = observationHandlerGrouping; + this.observationFilters = observationFilters; } @Override @@ -70,7 +75,7 @@ private ObservationRegistryConfigurer getConfigurer() { if (this.configurer == null) { this.configurer = new ObservationRegistryConfigurer(this.observationRegistryCustomizers, this.observationPredicates, this.observationConventions, this.observationHandlers, - this.observationHandlerGrouping); + this.observationHandlerGrouping, this.observationFilters); } return this.configurer; } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java index 7aac4a071a2a..e9587a6e2c09 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java @@ -28,6 +28,7 @@ import io.micrometer.observation.GlobalObservationConvention; import io.micrometer.observation.Observation; import io.micrometer.observation.Observation.Context; +import io.micrometer.observation.ObservationFilter; import io.micrometer.observation.ObservationHandler; import io.micrometer.observation.ObservationHandler.AllMatchingCompositeObservationHandler; import io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObservationHandler; @@ -176,6 +177,16 @@ void autoConfiguresObservationPredicates() { }); } + @Test + void autoConfiguresObservationFilters() { + this.contextRunner.withUserConfiguration(ObservationFilters.class).run((context) -> { + ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class); + Observation.start("filtered", observationRegistry).stop(); + MeterRegistry meterRegistry = context.getBean(MeterRegistry.class); + assertThat(meterRegistry.get("filtered").tag("filter", "one").timer().count()).isOne(); + }); + } + @Test void autoConfiguresGlobalObservationConventions() { this.contextRunner.withUserConfiguration(CustomGlobalObservationConvention.class).run((context) -> { @@ -273,6 +284,23 @@ ObservationPredicate customPredicate() { } + @Configuration(proxyBeanMethods = false) + static class ObservationFilters { + + @Bean + @Order(1) + ObservationFilter observationFilterOne() { + return (context) -> context.addLowCardinalityKeyValue(KeyValue.of("filter", "one")); + } + + @Bean + @Order(0) + ObservationFilter observationFilterTwo() { + return (context) -> context.addLowCardinalityKeyValue(KeyValue.of("filter", "two")); + } + + } + @Configuration(proxyBeanMethods = false) static class CustomGlobalObservationConvention {