
com.taotao.boot.prometheus.configuration.PrometheusAutoConfiguration Maven / Gradle / Ivy
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* 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 com.taotao.boot.prometheus.configuration;
import com.taotao.boot.common.utils.common.PropertyUtils;
import com.taotao.boot.prometheus.collector.PrometheusCollector;
import com.taotao.boot.prometheus.properties.PrometheusProperties;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import java.time.Duration;
@EnableConfigurationProperties({PrometheusProperties.class})
@ConditionalOnProperty(
prefix = PrometheusProperties.PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true)
@AutoConfiguration(after = PrometheusMetricsExportAutoConfiguration.class)
public class PrometheusAutoConfiguration {
@Value("${spring.application.name}")
private String applicationName;
@Bean
public PrometheusCollector prometheusCollector(PrometheusMeterRegistry prometheusMeterRegistry) {
return new PrometheusCollector(prometheusMeterRegistry);
}
// @Bean
// public CollectorRegistry collectorRegistry() {
// return new CollectorRegistry();
// }
//
// @Bean
// public PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig config,
// CollectorRegistry collectorRegistry) {
// return new PrometheusMeterRegistry(config, collectorRegistry, Clock.SYSTEM);
// }
@Bean
MeterRegistryCustomizer appMetricsCommonTags() {
if (applicationName == null) {
applicationName = PropertyUtils.getProperty("spring.application.name");
}
return registry -> registry.config()
.commonTags("application", applicationName)
.meterFilter(new MeterFilter() {
@Override
public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
if (id.getType() == Meter.Type.TIMER && id.getName().matches("^(jvm|http){1}.*")) {
return DistributionStatisticConfig.builder()
.percentilesHistogram(true)
.percentiles(0.5, 0.90, 0.95, 0.99)
.serviceLevelObjectives(
Duration.ofMillis(50).toNanos(),
Duration.ofMillis(100).toNanos(),
Duration.ofMillis(200).toNanos(),
Duration.ofMillis(1).toNanos(),
Duration.ofMillis(5).toNanos())
.minimumExpectedValue((double)Duration.ofMillis(1).toNanos())
.maximumExpectedValue((double)Duration.ofMillis(5).toNanos())
.build()
.merge(config);
} else {
return config;
}
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy