
com.taotao.boot.prometheus.collector.PrometheusCollector 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.collector;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.distribution.TimeWindowPercentileHistogram;
import io.micrometer.prometheusmetrics.PrometheusDistributionSummary;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PrometheusCollector implements InitializingBean {
private final PrometheusMeterRegistry prometheusMeterRegistry;
@Value("${spring.application.name}")
private String applicationName;
/**
* requestCounter
*/
public Counter requestCounter;
/**
* inprogressRequests
*/
public Gauge inprogressRequests;
/**
* requestLatencyHistogram
*/
public TimeWindowPercentileHistogram requestLatencyHistogram;
/**
* requestLatency
*/
public PrometheusDistributionSummary requestLatency;
public PrometheusCollector(PrometheusMeterRegistry prometheusMeterRegistry) {
this.prometheusMeterRegistry = prometheusMeterRegistry;
}
/**
* 服务启动时创建自定义指标
*/
private void init() {
/**
* 用于统计请求总数 计数器可以用于记录只会增加不会减少的指标类型,比如记录应用请求的总量(http_requests_total),
* cpu使用时间(process_cpu_seconds_total)等。 一般而言,Counter类型的metrics指标在命名中 我们使用_total结束。
*/
this.requestCounter = Counter.builder(getName() + "_requests_total")
.description("请求总数数据")
.tags("service", "method", "code", "dd")
.register(prometheusMeterRegistry);
/**
* 使用Gauge可以反映应用的当前状态,例如在监控主机时,主机当前空闲的内容大小(node_memory_MemFree), 37 *
* 可用内存大小(node_memory_MemAvailable)。或者容器当前的CPU使用率,内存使用率。这里我们使用 Gauge记录当前应用正在处理的Http请求数量。
*/
this.inprogressRequests = Gauge.builder(getName() + "_http_inprogress_requests", ()-> 0.0)
.tags("path", "method")
.description("进行中的请求状态数据")
.register(prometheusMeterRegistry);
/**
* 主要用于在指定分布范围内(Buckets)记录大小(如http request bytes)或者事件发生的次数。
* 以请求响应时间requests_latency_seconds为例
*/
//todo: 需要修改
// this.requestLatencyHistogram = Histogram.build()
// .name(getName() + "_http_requests_latency_seconds_histogram")
// .labelNames("path", "method", "code")
// .help("以秒为单位的请求延迟直方图数据")
// .register(prometheusMeterRegistry.getPrometheusRegistry());
/**
* 和Histogram类似,不同在于Histogram可以通过histogram_quantile函数在服务器端计算分位数,而
* Sumamry的分位数则是直接在客户端进行定义。因此对于分位数的计算。 Summary在通过PromQL进行查询时
* 有更好的性能表现,而Histogram则会消耗更多的资源。相对的对于客户端而言Histogram消耗的资源更少 用于统计TP50,TP90
*/
//todo: 需要修改
// this.requestLatency = Summary.build()
// .name(getName() + "_request_latency")
// .quantile(0.5, 0.05)
// .quantile(0.9, 0.01)
// .labelNames("path", "method", "code")
// .help("以秒为单位的请求延迟分数数据")
// .register(prometheusMeterRegistry.getPrometheusRegistry());
}
private String getName() {
return applicationName.replaceAll("-", "_");
}
@Override
public void afterPropertiesSet() throws Exception {
init();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy