Register all ObservationHandler, regardless which context they use
Closes #31272
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
package org.springframework.boot.actuate.autoconfigure.observation;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.observation.Observation.Context;
|
||||
import io.micrometer.observation.Observation.GlobalKeyValuesProvider;
|
||||
import io.micrometer.observation.ObservationHandler;
|
||||
import io.micrometer.observation.ObservationPredicate;
|
||||
@@ -50,7 +49,7 @@ public class ObservationAutoConfiguration {
|
||||
ObjectProvider<ObservationRegistryCustomizer<?>> observationRegistryCustomizers,
|
||||
ObjectProvider<ObservationPredicate> observationPredicates,
|
||||
ObjectProvider<GlobalKeyValuesProvider<?>> keyValuesProviders,
|
||||
ObjectProvider<ObservationHandler<Context>> observationHandlers,
|
||||
ObjectProvider<ObservationHandler<?>> observationHandlers,
|
||||
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping) {
|
||||
return new ObservationRegistryPostProcessor(observationRegistryCustomizers, observationPredicates,
|
||||
keyValuesProviders, observationHandlers, observationHandlerGrouping);
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.boot.actuate.autoconfigure.observation;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import io.micrometer.observation.Observation.Context;
|
||||
import io.micrometer.observation.ObservationHandler;
|
||||
import io.micrometer.observation.ObservationRegistry.ObservationConfig;
|
||||
|
||||
@@ -35,6 +34,6 @@ interface ObservationHandlerGrouping {
|
||||
* @param handlers the list of observation handlers
|
||||
* @param config the config to apply the handlers to
|
||||
*/
|
||||
void apply(Collection<ObservationHandler<Context>> handlers, ObservationConfig config);
|
||||
void apply(Collection<ObservationHandler<?>> handlers, ObservationConfig config);
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.boot.actuate.autoconfigure.observation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.micrometer.observation.Observation.Context;
|
||||
import io.micrometer.observation.Observation.GlobalKeyValuesProvider;
|
||||
import io.micrometer.observation.ObservationHandler;
|
||||
import io.micrometer.observation.ObservationPredicate;
|
||||
@@ -45,14 +44,14 @@ class ObservationRegistryConfigurer {
|
||||
|
||||
private final ObjectProvider<GlobalKeyValuesProvider<?>> keyValuesProviders;
|
||||
|
||||
private final ObjectProvider<ObservationHandler<Context>> observationHandlers;
|
||||
private final ObjectProvider<ObservationHandler<?>> observationHandlers;
|
||||
|
||||
private final ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping;
|
||||
|
||||
ObservationRegistryConfigurer(ObjectProvider<ObservationRegistryCustomizer<?>> customizers,
|
||||
ObjectProvider<ObservationPredicate> observationPredicates,
|
||||
ObjectProvider<GlobalKeyValuesProvider<?>> keyValuesProviders,
|
||||
ObjectProvider<ObservationHandler<Context>> observationHandlers,
|
||||
ObjectProvider<ObservationHandler<?>> observationHandlers,
|
||||
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping) {
|
||||
this.customizers = customizers;
|
||||
this.observationPredicates = observationPredicates;
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.observation;
|
||||
|
||||
import io.micrometer.observation.Observation.Context;
|
||||
import io.micrometer.observation.Observation.GlobalKeyValuesProvider;
|
||||
import io.micrometer.observation.ObservationHandler;
|
||||
import io.micrometer.observation.ObservationPredicate;
|
||||
@@ -41,7 +40,7 @@ class ObservationRegistryPostProcessor implements BeanPostProcessor {
|
||||
|
||||
private final ObjectProvider<GlobalKeyValuesProvider<?>> keyValuesProviders;
|
||||
|
||||
private final ObjectProvider<ObservationHandler<Context>> observationHandlers;
|
||||
private final ObjectProvider<ObservationHandler<?>> observationHandlers;
|
||||
|
||||
private final ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping;
|
||||
|
||||
@@ -50,7 +49,7 @@ class ObservationRegistryPostProcessor implements BeanPostProcessor {
|
||||
ObservationRegistryPostProcessor(ObjectProvider<ObservationRegistryCustomizer<?>> observationRegistryCustomizers,
|
||||
ObjectProvider<ObservationPredicate> observationPredicates,
|
||||
ObjectProvider<GlobalKeyValuesProvider<?>> keyValuesProviders,
|
||||
ObjectProvider<ObservationHandler<Context>> observationHandlers,
|
||||
ObjectProvider<ObservationHandler<?>> observationHandlers,
|
||||
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping) {
|
||||
this.observationRegistryCustomizers = observationRegistryCustomizers;
|
||||
this.observationPredicates = observationPredicates;
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import io.micrometer.core.instrument.observation.MeterObservationHandler;
|
||||
import io.micrometer.observation.Observation.Context;
|
||||
import io.micrometer.observation.ObservationHandler;
|
||||
import io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObservationHandler;
|
||||
import io.micrometer.observation.ObservationRegistry.ObservationConfig;
|
||||
@@ -39,9 +38,10 @@ import io.micrometer.observation.ObservationRegistry.ObservationConfig;
|
||||
class OnlyMetricsObservationHandlerGrouping implements ObservationHandlerGrouping {
|
||||
|
||||
@Override
|
||||
public void apply(Collection<ObservationHandler<Context>> handlers, ObservationConfig config) {
|
||||
List<ObservationHandler<Context>> meterObservationHandlers = new ArrayList<>();
|
||||
for (ObservationHandler<Context> handler : handlers) {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void apply(Collection<ObservationHandler<?>> handlers, ObservationConfig config) {
|
||||
List<ObservationHandler<?>> meterObservationHandlers = new ArrayList<>();
|
||||
for (ObservationHandler<?> handler : handlers) {
|
||||
if (handler instanceof MeterObservationHandler<?>) {
|
||||
meterObservationHandlers.add(handler);
|
||||
}
|
||||
@@ -50,8 +50,10 @@ class OnlyMetricsObservationHandlerGrouping implements ObservationHandlerGroupin
|
||||
}
|
||||
}
|
||||
|
||||
// The ugly raw casts can be removed once
|
||||
// https://github.com/micrometer-metrics/tracing/issues/27 is resolved
|
||||
if (!meterObservationHandlers.isEmpty()) {
|
||||
config.observationHandler(new FirstMatchingCompositeObservationHandler(meterObservationHandlers));
|
||||
config.observationHandler(new FirstMatchingCompositeObservationHandler((List) meterObservationHandlers));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import io.micrometer.core.instrument.observation.MeterObservationHandler;
|
||||
import io.micrometer.observation.Observation.Context;
|
||||
import io.micrometer.observation.ObservationHandler;
|
||||
import io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObservationHandler;
|
||||
import io.micrometer.observation.ObservationRegistry.ObservationConfig;
|
||||
@@ -42,10 +41,11 @@ import io.micrometer.tracing.handler.TracingObservationHandler;
|
||||
class TracingObservationHandlerGrouping implements ObservationHandlerGrouping {
|
||||
|
||||
@Override
|
||||
public void apply(Collection<ObservationHandler<Context>> handlers, ObservationConfig config) {
|
||||
List<ObservationHandler<Context>> meterObservationHandlers = new ArrayList<>();
|
||||
List<ObservationHandler<Context>> tracingObservationHandlers = new ArrayList<>();
|
||||
for (ObservationHandler<Context> handler : handlers) {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void apply(Collection<ObservationHandler<?>> handlers, ObservationConfig config) {
|
||||
List<ObservationHandler<?>> meterObservationHandlers = new ArrayList<>();
|
||||
List<ObservationHandler<?>> tracingObservationHandlers = new ArrayList<>();
|
||||
for (ObservationHandler<?> handler : handlers) {
|
||||
if (handler instanceof MeterObservationHandler<?>) {
|
||||
meterObservationHandlers.add(handler);
|
||||
}
|
||||
@@ -57,11 +57,13 @@ class TracingObservationHandlerGrouping implements ObservationHandlerGrouping {
|
||||
}
|
||||
}
|
||||
|
||||
// The ugly raw casts can be removed once
|
||||
// https://github.com/micrometer-metrics/tracing/issues/27 is resolved
|
||||
if (!meterObservationHandlers.isEmpty()) {
|
||||
config.observationHandler(new FirstMatchingCompositeObservationHandler(meterObservationHandlers));
|
||||
config.observationHandler(new FirstMatchingCompositeObservationHandler((List) meterObservationHandlers));
|
||||
}
|
||||
if (!tracingObservationHandlers.isEmpty()) {
|
||||
config.observationHandler(new FirstMatchingCompositeObservationHandler(tracingObservationHandlers));
|
||||
config.observationHandler(new FirstMatchingCompositeObservationHandler((List) tracingObservationHandlers));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -126,6 +126,19 @@ class ObservationAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoConfiguresObservationHandlerWithCustomContext() {
|
||||
this.contextRunner.withUserConfiguration(ObservationHandlerWithCustomContextConfiguration.class)
|
||||
.run((context) -> {
|
||||
ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class);
|
||||
List<ObservationHandler<?>> handlers = context.getBean(CalledHandlers.class).getCalledHandlers();
|
||||
CustomContext customContext = new CustomContext();
|
||||
Observation.start("test-observation", customContext, observationRegistry);
|
||||
assertThat(handlers).hasSize(1);
|
||||
assertThat(handlers.get(0)).isInstanceOf(ObservationHandlerWithCustomContext.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoConfiguresObservationHandlerWhenTracingIsActive() {
|
||||
this.tracingContextRunner.withUserConfiguration(ObservationHandlersTracing.class).run((context) -> {
|
||||
@@ -216,6 +229,17 @@ class ObservationAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(CalledHandlersConfiguration.class)
|
||||
static class ObservationHandlerWithCustomContextConfiguration {
|
||||
|
||||
@Bean
|
||||
ObservationHandlerWithCustomContext observationHandlerWithCustomContext(CalledHandlers calledHandlers) {
|
||||
return new ObservationHandlerWithCustomContext(calledHandlers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(CalledHandlersConfiguration.class)
|
||||
static class ObservationHandlersTracing {
|
||||
@@ -298,6 +322,30 @@ class ObservationAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
private static class ObservationHandlerWithCustomContext implements ObservationHandler<CustomContext> {
|
||||
|
||||
private final CalledHandlers calledHandlers;
|
||||
|
||||
ObservationHandlerWithCustomContext(CalledHandlers calledHandlers) {
|
||||
this.calledHandlers = calledHandlers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(CustomContext context) {
|
||||
this.calledHandlers.onCalled(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsContext(Context context) {
|
||||
return context instanceof CustomContext;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class CustomContext extends Context {
|
||||
|
||||
}
|
||||
|
||||
private static class CalledHandlers {
|
||||
|
||||
private final List<ObservationHandler<?>> calledHandlers = new ArrayList<>();
|
||||
|
||||
Reference in New Issue
Block a user