Boot 2.x 普罗米修斯数据采集

<!-- boot2.x 兼容-->
<!-- The client -->
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient</artifactId>
    <version>0.6.0</version>
</dependency>
<!-- Hotspot JVM metrics-->
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_hotspot</artifactId>
    <version>0.6.0</version>
</dependency>
<!-- Exposition HTTPServer-->
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_httpserver</artifactId>
    <version>0.6.0</version>
</dependency>
<!-- Pushgateway exposition-->
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_pushgateway</artifactId>
    <version>0.6.0</version>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.1.4</version>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
    <version>1.1.4</version>
</dependency>
import io.micrometer.core.instrument.Metrics;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private PrometheusMetricsUtils() {

}

private static final Logger LOGGER = LoggerFactory.getLogger(PrometheusMetricsUtils.class);


private static final String HTTP_REQUEST_COUNTER_ALL = "http_request_counter_all";
private static final String HTTP_REQUEST_COUNTER_ERROR = "http_request_counter_error";
private static final String HTTP_RESPONSE_RT_MS = "http_response_rt_ms";



private static final String API = "api";
private static final String RC = "rc";


/**
 * 收集api total 埋点数据
 * @param op api名
 */
public static void metricTotalRequest(String op) {
    try {
        Metrics.counter(HTTP_REQUEST_COUNTER_ALL, API, op)
                .increment();
    } catch (Exception e) {
        LOGGER.info("PrometheusMetrics API QPS ERROR ,caused by {}", ExceptionUtils.getFullStackTrace(e));
    }
}

/**
 * 收集api error 埋点数据
 * @param op api名
 */
public static void metricErrorRequest(String op) {
    try {

        Metrics.counter(HTTP_REQUEST_COUNTER_ERROR, API, op)
                .increment();
    } catch (Exception e) {
        LOGGER.info("PrometheusMetrics API ERROR COUNTER ,caused by {}", ExceptionUtils.getFullStackTrace(e));
    }
}

/**
 * 收集接口响应时间
 * @param op 接口标识
 * @param rc 返回码
 * @param rt 返回时间
 */
public static void metricResponseTime(String op, String rc, double rt) {
    Metrics.summary(HTTP_RESPONSE_RT_MS, API, op, RC, rc).record(rt);
}

转载于:https://my.oschina.net/yugj/blog/3053739

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值