1.秒内新增
select UNIX_TIMESTAMP(create_time) as time_sec,
count(1) as value,
‘min count rnr reply’ as metric
from t_demo
group by UNIX_TIMESTAMP(create_time)
order by time_sec
2.table数据
select
UNIX_TIMESTAMP(create_time) AS “time”,
INSERT(name, 2, 50, ‘') AS “名字”,
INSERT(order_id, 2, 50, '’) AS “order_id”,
(CASE WHEN status
= 1 then ‘正常’ else ‘暂停’ end) AS ‘状态’
FROM t_demo
3.
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Summary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyPrometheusConfig {
@Autowired
private PrometheusMeterRegistry prometheusMeterRegistry;
/**
* 指标名{tag} 数量
* api_request_count_total{service="/core/demo/list",method="GET",code="200",} 24397.0
*
* @return
*/
@Bean
public Counter requestCounter() {
return Counter.build("api_request_count", "count every request by serivce")
.labelNames("service", "method", "code")
.register(prometheusMeterRegistry.getPrometheusRegistry());
}
/**
* 指标名{tag} 数量
* api_request_latency{service="/core/demo/list",method="GET",code="200",quantile="0.9",} 4.0
* api_request_latency{service="/core/demo/list",method="GET",code="200",quantile="0.95",} 5.0
* api_request_latency{service="/core/demo/list",method="GET",code="200",quantile="0.99",} 6.0
* api_request_latency_count{service="/core/demo/list",method="GET",code="200",} 24397.0
* api_request_latency_sum{service="/core/demo/list",method="GET",code="200",} 69438.0
*
* @return
*/
@Bean
public Summary requestLatencySummery() {
return Summary.build("api_request_latency", "monite request latency")
.quantile(0.9, 0.01)
.quantile(0.95, 0.01)
.quantile(0.99, 0.01)
.labelNames("service", "method", "code")
.register(prometheusMeterRegistry.getPrometheusRegistry());
}
}
import io.prometheus.client.Counter;
import io.prometheus.client.Summary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
@Component
public class PrometheusMetricsInterceptor implements HandlerInterceptor {
@Autowired
private Counter requestCounter;
@Autowired
private Summary requestLatencySummery;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
request.setAttribute("startTime", new Date().getTime());
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
String service = request.getRequestURI();
requestCounter.labels(service, request.getMethod(), response.getStatus() + "").inc();
long duration = new Date().getTime() - (long) request.getAttribute("startTime");
requestLatencySummery.labels(service, request.getMethod(), response.getStatus() + "")
.observe(duration);
}
}
------请求计数
import io.prometheus.client.Counter;
import io.prometheus.client.Summary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
@Component
public class PrometheusMetricsInterceptor implements HandlerInterceptor {
@Autowired
private Counter requestCounter;
@Autowired
private Summary requestLatencySummery;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
request.setAttribute("startTime", new Date().getTime());
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
String service = request.getRequestURI();
requestCounter.labels(service, request.getMethod(), response.getStatus() + "").inc();
long duration = new Date().getTime() - (long) request.getAttribute("startTime");
requestLatencySummery.labels(service, request.getMethod(), response.getStatus() + "")
.observe(duration);
}
}