Skip to content

Commit

Permalink
Add ObservationFilter beans to auto-configured ObservationRegistry
Browse files Browse the repository at this point in the history
Fixes gh-33968
  • Loading branch information
wilkinsona committed Feb 8, 2023
1 parent b2d239e commit 3551030
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -62,9 +63,10 @@ static ObservationRegistryPostProcessor observationRegistryPostProcessor(
ObjectProvider<ObservationPredicate> observationPredicates,
ObjectProvider<GlobalObservationConvention<?>> observationConventions,
ObjectProvider<ObservationHandler<?>> observationHandlers,
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping) {
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping,
ObjectProvider<ObservationFilter> observationFilters) {
return new ObservationRegistryPostProcessor(observationRegistryCustomizers, observationPredicates,
observationConventions, observationHandlers, observationHandlerGrouping);
observationConventions, observationHandlers, observationHandlerGrouping, observationFilters);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -48,22 +49,27 @@ class ObservationRegistryConfigurer {

private final ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping;

private final ObjectProvider<ObservationFilter> observationFilters;

ObservationRegistryConfigurer(ObjectProvider<ObservationRegistryCustomizer<?>> customizers,
ObjectProvider<ObservationPredicate> observationPredicates,
ObjectProvider<GlobalObservationConvention<?>> observationConventions,
ObjectProvider<ObservationHandler<?>> observationHandlers,
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping) {
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping,
ObjectProvider<ObservationFilter> 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);
}

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -44,18 +45,22 @@ class ObservationRegistryPostProcessor implements BeanPostProcessor {

private final ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping;

private final ObjectProvider<ObservationFilter> observationFilters;

private volatile ObservationRegistryConfigurer configurer;

ObservationRegistryPostProcessor(ObjectProvider<ObservationRegistryCustomizer<?>> observationRegistryCustomizers,
ObjectProvider<ObservationPredicate> observationPredicates,
ObjectProvider<GlobalObservationConvention<?>> observationConventions,
ObjectProvider<ObservationHandler<?>> observationHandlers,
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping) {
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping,
ObjectProvider<ObservationFilter> observationFilters) {
this.observationRegistryCustomizers = observationRegistryCustomizers;
this.observationPredicates = observationPredicates;
this.observationConventions = observationConventions;
this.observationHandlers = observationHandlers;
this.observationHandlerGrouping = observationHandlerGrouping;
this.observationFilters = observationFilters;
}

@Override
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) -> {
Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit 3551030

Please sign in to comment.