|
| 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 | +} |
0 commit comments