版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cpongo3/article/details/89327713
添加依赖jar <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <exclusions> <exclusion> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.1.2</version> </dependency>
配置application.conf
server: port: 9988 tomcat: max-connections: 20000 max-threads: 500 accept-count: 500 min-spare-threads: 20 session: tracking-modes: url timeout: 5 management: server: port: 9999 security: # 仅限于 开发环境可对security进行关闭。 enabled: false metrics: export: prometheus: enabled: true step: 1m descriptions: true web: server: auto-time-requests: false endpoints: prometheus: id: springmetrics web: base-path: /ops path-mapping: prometheus: monitor exposure: include: health,info,prometheus # include: health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics # http://localhost:9999/ops/monitor # http://prometheus.xchanger.cn/graph package com.ecarx.sys.util.collect; import com.ecarx.sys.util.BigDecimalUtil; import io.micrometer.core.instrument.Metrics; import io.prometheus.client.Counter; import io.prometheus.client.Histogram; /** * @Author: geyang * @Description: * @Date : Created in 3:24 PM 1/10/2019 * http://confluence.ecarx.com.cn/pages/viewpage.action?pageId=2020839 */ public class MeterUtil { //响应时间分布 private final static Histogram httpRequestDurationMs = Histogram.build().buckets ( .1,.5,1,5,10,30,60 ) .name("http_request_duration_histogram").labelNames ( "server" ) .help ( "http_request_duration_histogram" ) // .exponentialBuckets(25, 2, 7) .register(); private final static Histogram httpRequestdependDurationMs = Histogram.build().buckets ( .1,.5,1,5,10,30,60 ) .name("http_depend_service_reponses_histogram").labelNames ( "server","target" ) .help ( "http_depend_service_reponses_histogram" ) // .exponentialBuckets(25, 2, 7) .register(); //响应code分布 public final static Histogram httpRequestCodeMs = Histogram.build().buckets (200,300,400,500 ) .name("http_request_code_histogram").labelNames ( "server" ) .help ( "http_request_code_histogram" ) .register(); public final static Histogram httpRequestDependCodeMs = Histogram.build().buckets (200,300,400,500) .name("http_depend_service_status_histogram").labelNames ( "server","target" ) .help ( "http_depend_service_status_histogram" ) .register(); public final static Counter httpRequestTotal = Counter.build() .name("http_request_total").labelNames ( "server","method","handler" ) .help ( "http_request_total" ) .register(); public final static Counter bizTotal = Counter.build() .name("rec_si_counter").labelNames ( "server","handler" ) .help ( "rec_si_counter" ) .register(); public final static Counter serviceMessageConsumerTotal = Counter.build() .name("service_message_consumer_total").labelNames ( "server","msgtype","topic" ) .help ( "service_message_consumer_total" ) .register(); /** *接口总访问量(次数) * @param server 服务 * @param method * @param handler uri */ public static void httpRequestTotal(String server,String method,String handler){ httpRequestTotal.labels (server,method,handler).inc (); // Metrics.counter ( "http_request_total","server",server,"method",method,"handler",handler ).increment (); } public static void bizTotal(String server,String handler){ bizTotal.labels (server,handler).inc (); } // public static void httpRequestDurationSeconds(String server,String handler,long duration){ // Timer timer= Metrics.timer("http_request_duration", Tags.of ( "handler",handler, "server",server )); // timer.record ( duration, TimeUnit.MILLISECONDS ); // } public static void serviceMessageConsumerTotal(String server,String msgtype,String topic){ serviceMessageConsumerTotal.labels (server,msgtype,topic).inc (); // Metrics.counter ( "service_message_consumer_total","server",server,"msgtype",msgtype,"topic",topic ).increment (); } // public static void httpRequestDurationSeconds(String server,String handler,long duration){ // Metrics.gauge ("http_request_duration_seconds", Tags.of ( "handler",handler, "server",server ),duration); // } // public static void httpDependServiceReponseSeconds(String server,String target,long duration){ // Metrics.gauge ("http_depend_service_reponses_seconds", Tags.of ( "target",target, "server",server ),duration); // } public static void httpDependServiceStatusHistogram(String server,String target,long le){ httpRequestdependDurationMs.labels(server,target).observe(BigDecimalUtil.div ( le,1000,3 )); // Metrics.timer ( "http_depend_service_reponses_histogram","server",server,"target",target ).record ( le, TimeUnit.MILLISECONDS ); } public static void httpRequestDurationHistogram(String server,long le){ httpRequestDurationMs.labels(server).observe( BigDecimalUtil.div ( le,1000,3 )); // Metrics.timer ( "http_request_duration_histogram","server",server ).record ( le, TimeUnit.MILLISECONDS ); } public static void httpRequestCodeMs(String server,int le){ httpRequestCodeMs.labels(server).observe(le); // Metrics.summary ( "http_request_code_histogram","server",server ).record ( le ); } public static void httpRequestDependCodeMs(String server,String target,int le){ if (le>=500) le=500; else if (le>=400) le=400; else if (le>=300) le=300; else if (le>=200) le=200; httpRequestDependCodeMs.labels(server,target).observe(le); // Metrics.summary ( "http_depend_service_status_histogram","server",server,"target",target ).record ( le ); } public static void timer(String name,String ... tag){ Metrics.timer(name,tag); } public static void main(String[] args) { System.out.println ( 1 ); } }
package com.ecarx.sys.util.collect; import io.prometheus.client.CollectorRegistry; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Author: geyang * @Description: * @Date : Created in 9:50 AM 1/31/2019 */ @Configuration @ConditionalOnClass(CollectorRegistry.class) public class PrometheusConfiguration { @Bean @ConditionalOnMissingBean CollectorRegistry metricRegistry() { return CollectorRegistry.defaultRegistry; } // @Bean // ServletRegistrationBean registerPrometheusExporterServlet(CollectorRegistry metricRegistry) { // return new ServletRegistrationBean(new MetricsServlet(metricRegistry), "/prometheus"); // } }