• prometheus


    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);
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
  • 相关阅读:
    基于MIPS32的五级流水及中断控制器设计
    若依(ruoyi)前后端分离版使用教程之用户注册(1)单表操作
    VMware 中 centos7 设置静态IP
    Android中使用AOP进行拦截统一处理功能(如网络判断)
    需求评审时如何平衡团队成员间意见?
    内网渗透(八十二)之 CVE-2019-1040 NTLM MIC 绕过漏洞
    Python从入门到实践:包的使用
    前端面试话术集锦第 14 篇:高频考点(React常考基础知识点)
    STM32CubeMX新建工程并点亮一个LED
    回归 Regression
  • 原文地址:https://blog.csdn.net/u014172271/article/details/126800026