
io.micrometer.spring.autoconfigure.MeterRegistryConfigurer Maven / Gradle / Ivy
/**
* Copyright 2017 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.spring.autoconfigure;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.ObjectProvider;
/**
* Applies {@link MeterRegistryCustomizer customizers}, {@link MeterFilter filters},
* {@link MeterBinder binders} and {@link Metrics#addRegistry
* global registration} to {@link MeterRegistry meter registries}.
*
* @author Jon Schneider
* @author Phillip Webb
*/
class MeterRegistryConfigurer {
private final ObjectProvider>> customizers;
private final ObjectProvider> filters;
private final ObjectProvider> binders;
private final boolean addToGlobalRegistry;
private final boolean hasCompositeMeterRegistry;
MeterRegistryConfigurer(ObjectProvider>> customizers,
ObjectProvider> filters, ObjectProvider> binders,
boolean addToGlobalRegistry, boolean hasCompositeMeterRegistry) {
this.customizers = customizers;
this.filters = filters;
this.binders = binders;
this.addToGlobalRegistry = addToGlobalRegistry;
this.hasCompositeMeterRegistry = hasCompositeMeterRegistry;
}
void configure(MeterRegistry registry) {
// Customizers must be applied before binders, as they may add custom
// tags or alter timer or summary configuration.
customize(registry);
addFilters(registry);
if (!this.hasCompositeMeterRegistry
|| registry instanceof CompositeMeterRegistry) {
addBinders(registry);
}
if (this.addToGlobalRegistry && registry != Metrics.globalRegistry) {
Metrics.addRegistry(registry);
}
}
@SuppressWarnings("unchecked")
private void customize(MeterRegistry registry) {
// Customizers must be applied before binders, as they may add custom tags or alter
// timer or summary configuration.
for (MeterRegistryCustomizer customizer : getOrEmpty(this.customizers)) {
try {
customizer.customize(registry);
} catch (ClassCastException ignored) {
// This is essentially what LambdaSafe.callbacks(..).invoke(..) is doing
// in Spring Boot 2, just trapping ClassCastExceptions since the generic type
// has been erased by this point.
}
}
}
private void addFilters(MeterRegistry registry) {
getOrEmpty(this.filters).forEach(registry.config()::meterFilter);
}
private void addBinders(MeterRegistry registry) {
getOrEmpty(this.binders).forEach((binder) -> binder.bindTo(registry));
}
private List getOrEmpty(ObjectProvider> listProvider) {
List list = listProvider.getIfAvailable();
return list != null ? list : Collections.emptyList();
}
}