<!-- 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);
}