1. 程式人生 > >Boot 2.x 普羅米修斯資料採集

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).re