Skip to content

Commit d439eff

Browse files
committed
Add auto-configuration for Micrometer 2.0.0 Observation API
- Adds a ObservationRegistry bean - Add support for ObservationRegistryCustomizers - Enables timer creation for observations if micrometer-core is on the classpath - Registers ObservationPredicate, GlobalTagsProvider and ObservationHandler on the MeterRegistry - Applies grouping to the ObservationHandlers: MeterObservationHandler are added to a FirstMatchingCompositeObservationHandler - If micrometer-tracing is on the classpath, the TracingObservationHandler are added to a FirstMatchingCompositeObservationHandler See gh-29666
1 parent e325115 commit d439eff

17 files changed

+1103
-3
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ dependencies {
4747
optional("com.zaxxer:HikariCP")
4848
optional("io.dropwizard.metrics:metrics-jmx")
4949
optional("io.lettuce:lettuce-core")
50+
optional("io.micrometer:micrometer-observation")
5051
optional("io.micrometer:micrometer-core")
52+
optional("io.micrometer:micrometer-tracing-api")
5153
optional("io.micrometer:micrometer-binders")
5254
optional("io.micrometer:micrometer-registry-appoptics")
5355
optional("io.micrometer:micrometer-registry-atlas") {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.observation;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collection;
21+
import java.util.List;
22+
23+
import io.micrometer.core.instrument.observation.MeterObservationHandler;
24+
import io.micrometer.observation.ObservationHandler;
25+
import io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObservationHandler;
26+
import io.micrometer.observation.ObservationRegistry.ObservationConfig;
27+
28+
/**
29+
* {@link ObservationHandlerGrouping} used by {@link ObservationAutoConfiguration} if
30+
* micrometer-tracing is not on the classpath.
31+
*
32+
* Groups all {@link MeterObservationHandler} into a
33+
* {@link FirstMatchingCompositeObservationHandler}. All other handlers are added to the
34+
* {@link ObservationConfig} directly.
35+
*
36+
* @author Moritz Halbritter
37+
*/
38+
class NoTracingObservationHandlerGrouping implements ObservationHandlerGrouping {
39+
40+
@Override
41+
public void apply(Collection<ObservationHandler<?>> handlers, ObservationConfig config) {
42+
List<ObservationHandler<?>> meterObservationHandlers = new ArrayList<>();
43+
for (ObservationHandler<?> handler : handlers) {
44+
if (handler instanceof MeterObservationHandler<?>) {
45+
meterObservationHandlers.add(handler);
46+
}
47+
else {
48+
config.observationHandler(handler);
49+
}
50+
}
51+
52+
if (!meterObservationHandlers.isEmpty()) {
53+
config.observationHandler(
54+
new FirstMatchingCompositeObservationHandler(castToRawType(meterObservationHandlers)));
55+
}
56+
}
57+
58+
@SuppressWarnings({ "unchecked", "rawtypes" })
59+
private List<ObservationHandler> castToRawType(List<ObservationHandler<?>> handlers) {
60+
// See https://github.com/micrometer-metrics/micrometer/issues/3064
61+
return (List) handlers;
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.observation;
18+
19+
import io.micrometer.core.instrument.MeterRegistry;
20+
import io.micrometer.observation.Observation.GlobalTagsProvider;
21+
import io.micrometer.observation.ObservationHandler;
22+
import io.micrometer.observation.ObservationPredicate;
23+
import io.micrometer.observation.ObservationRegistry;
24+
import io.micrometer.tracing.handler.TracingObservationHandler;
25+
26+
import org.springframework.beans.factory.ObjectProvider;
27+
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.AutoConfiguration;
29+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
33+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
34+
import org.springframework.context.annotation.Bean;
35+
import org.springframework.context.annotation.Configuration;
36+
37+
/**
38+
* {@link EnableAutoConfiguration Auto-configuration} for the Micrometer Observation API.
39+
*
40+
* @author Moritz Halbritter
41+
* @since 3.0.0
42+
*/
43+
@AutoConfiguration(after = CompositeMeterRegistryAutoConfiguration.class)
44+
@ConditionalOnClass(ObservationRegistry.class)
45+
public class ObservationAutoConfiguration {
46+
47+
@Bean
48+
static ObservationRegistryPostProcessor observationRegistryPostProcessor(
49+
ObjectProvider<ObservationRegistryCustomizer<?>> observationRegistryCustomizers,
50+
ObjectProvider<ObservationPredicate> observationPredicates,
51+
ObjectProvider<GlobalTagsProvider<?>> tagProviders,
52+
ObjectProvider<ObservationHandler<?>> observationHandlers,
53+
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping) {
54+
return new ObservationRegistryPostProcessor(observationRegistryCustomizers, observationPredicates, tagProviders,
55+
observationHandlers, observationHandlerGrouping);
56+
}
57+
58+
@Bean
59+
@ConditionalOnMissingBean
60+
ObservationRegistry observationRegistry() {
61+
return ObservationRegistry.create();
62+
}
63+
64+
@Configuration(proxyBeanMethods = false)
65+
@ConditionalOnBean(MeterRegistry.class)
66+
static class MetricsConfiguration {
67+
68+
@Bean
69+
TimerObservationHandlerObservationRegistryCustomizer enableTimerObservationHandler(
70+
MeterRegistry meterRegistry) {
71+
return new TimerObservationHandlerObservationRegistryCustomizer(meterRegistry);
72+
}
73+
74+
}
75+
76+
@Configuration(proxyBeanMethods = false)
77+
@ConditionalOnMissingClass("io.micrometer.tracing.handler.TracingObservationHandler")
78+
static class NoTracingConfiguration {
79+
80+
@Bean
81+
ObservationHandlerGrouping noTracingObservationHandlerGrouping() {
82+
return new NoTracingObservationHandlerGrouping();
83+
}
84+
85+
}
86+
87+
@Configuration(proxyBeanMethods = false)
88+
@ConditionalOnClass(TracingObservationHandler.class)
89+
static class TracingConfiguration {
90+
91+
@Bean
92+
ObservationHandlerGrouping tracingObservationHandlerGrouping() {
93+
return new TracingObservationHandlerGrouping();
94+
}
95+
96+
}
97+
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.observation;
18+
19+
import java.util.Collection;
20+
21+
import io.micrometer.observation.ObservationHandler;
22+
import io.micrometer.observation.ObservationRegistry.ObservationConfig;
23+
24+
/**
25+
* Strategy to apply {@link ObservationHandler ObservationHandlers} to an
26+
* {@link ObservationConfig}.
27+
*
28+
* @author Moritz Halbritter
29+
*/
30+
interface ObservationHandlerGrouping {
31+
32+
/**
33+
* Applies the given list of {@code handlers} to the given {@code config}.
34+
* @param handlers the list of observation handlers
35+
* @param config the config to apply the handlers to
36+
*/
37+
void apply(Collection<ObservationHandler<?>> handlers, ObservationConfig config);
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.observation;
18+
19+
import java.util.List;
20+
21+
import io.micrometer.observation.Observation.GlobalTagsProvider;
22+
import io.micrometer.observation.ObservationHandler;
23+
import io.micrometer.observation.ObservationPredicate;
24+
import io.micrometer.observation.ObservationRegistry;
25+
26+
import org.springframework.beans.factory.ObjectProvider;
27+
import org.springframework.boot.util.LambdaSafe;
28+
29+
/**
30+
* Configurer to apply {@link ObservationRegistryCustomizer customizers} to
31+
* {@link ObservationRegistry observation registries}. Installs
32+
* {@link ObservationPredicate observation predicates} and {@link GlobalTagsProvider
33+
* global tag providers} into the {@link ObservationRegistry}. Also uses a
34+
* {@link ObservationHandlerGrouping} to group handlers, which are then added to the
35+
* {@link ObservationRegistry}.
36+
*
37+
* @author Moritz Halbritter
38+
*/
39+
class ObservationRegistryConfigurer {
40+
41+
private final ObjectProvider<ObservationRegistryCustomizer<?>> customizers;
42+
43+
private final ObjectProvider<ObservationPredicate> observationPredicates;
44+
45+
private final ObjectProvider<GlobalTagsProvider<?>> tagProviders;
46+
47+
private final ObjectProvider<ObservationHandler<?>> observationHandlers;
48+
49+
private final ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping;
50+
51+
ObservationRegistryConfigurer(ObjectProvider<ObservationRegistryCustomizer<?>> customizers,
52+
ObjectProvider<ObservationPredicate> observationPredicates,
53+
ObjectProvider<GlobalTagsProvider<?>> tagProviders,
54+
ObjectProvider<ObservationHandler<?>> observationHandlers,
55+
ObjectProvider<ObservationHandlerGrouping> observationHandlerGrouping) {
56+
this.customizers = customizers;
57+
this.observationPredicates = observationPredicates;
58+
this.tagProviders = tagProviders;
59+
this.observationHandlers = observationHandlers;
60+
this.observationHandlerGrouping = observationHandlerGrouping;
61+
}
62+
63+
void configure(ObservationRegistry registry) {
64+
registerObservationPredicates(registry);
65+
registerGlobalTagsProvider(registry);
66+
registerHandlers(registry);
67+
customize(registry);
68+
}
69+
70+
private void registerHandlers(ObservationRegistry registry) {
71+
this.observationHandlerGrouping.getObject().apply(this.observationHandlers.orderedStream().toList(),
72+
registry.observationConfig());
73+
}
74+
75+
private void registerObservationPredicates(ObservationRegistry registry) {
76+
this.observationPredicates.orderedStream().forEach(
77+
(observationPredicate) -> registry.observationConfig().observationPredicate(observationPredicate));
78+
}
79+
80+
private void registerGlobalTagsProvider(ObservationRegistry registry) {
81+
this.tagProviders.orderedStream()
82+
.forEach((tagProvider) -> registry.observationConfig().tagsProvider(tagProvider));
83+
}
84+
85+
@SuppressWarnings("unchecked")
86+
private void customize(ObservationRegistry registry) {
87+
LambdaSafe.callbacks(ObservationRegistryCustomizer.class, asOrderedList(this.customizers), registry)
88+
.withLogger(ObservationRegistryConfigurer.class).invoke((customizer) -> customizer.customize(registry));
89+
}
90+
91+
private <T> List<T> asOrderedList(ObjectProvider<T> provider) {
92+
return provider.orderedStream().toList();
93+
}
94+
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.observation;
18+
19+
import io.micrometer.observation.ObservationRegistry;
20+
21+
/**
22+
* Callback interface that can be used to customize auto-configured
23+
* {@link ObservationRegistry observation registries}.
24+
*
25+
* @param <T> the registry type to customize
26+
* @author Moritz Halbritter
27+
* @since 3.0.0
28+
*/
29+
@FunctionalInterface
30+
public interface ObservationRegistryCustomizer<T extends ObservationRegistry> {
31+
32+
/**
33+
* Customize the given {@code registry}.
34+
* @param registry the registry to customize
35+
*/
36+
void customize(T registry);
37+
38+
}

0 commit comments

Comments
 (0)