首页  

micrometer使用     所属分类 springboot 浏览量 1309
业务监控埋点

Micrometer 提供 通用api 
与不同监控系统的适配

MeterRegistry  
SimpleMeterRegistry  在内存中维护数据

SimpleMeterRegistry  数据不会发布到其他系统,维护在内存中
CompositeMeterRegistry 多 个MeterRegistry聚合,内部维护了一个MeterRegistry的列表
全局的MeterRegistry  
io.micrometer.core.instrument.Metrics 静态 CompositeMeterRegistry实例 globalRegistry


常用指标类型
Counter Gauge Timer DistributionSummary

metric   name Tag

MeterRegistry registry = new SimpleMeterRegistry();
Counter counter = registry.counter("request.count", "url", "home");

Counter counter2 = Counter
     .builder("request.count")
     .description("count2") // optional
     .tags("url", "home") // optional
     .register(registry);
     
counter.increment();
counter2.increment();
System.out.println(counter.measure()); 


推荐使用
io.micrometer.core.instrument.Metrics
Metrics.counter("invokeCount").increment();


注意要注册下 ,否则数据不会更新
MeterRegistry registry = new SimpleMeterRegistry();
Metrics.addRegistry(registry);
List< Meter> meters = Metrics.globalRegistry.getMeters();
System.out.println(meters);



long start = System.currentTimeMillis();
...
long end = System.currentTimeMillis();
long time = end - start;
Metrics.timer("statusInvokeTime").record(time, TimeUnit.MILLISECONDS);



Metrics.counter("xxx").increment();
long start = System.currentTimeMillis();
// bizcode here
long end = System.currentTimeMillis();
long time = end - start;
Metrics.timer("xxx").record(time, TimeUnit.MILLISECONDS);



# HELP statusInvokeTime_seconds  
# TYPE statusInvokeTime_seconds summary
statusInvokeTime_seconds_count 13.0
statusInvokeTime_seconds_sum 0.416
statusInvokeTime_seconds_max 0.243




自动导出数据给各种监控系统 譬如 prometheus

不同的监控系统 ,命名自动转换 
Prometheus - http_server_requests_duration_seconds 
Atlas - httpServerRequests 
Graphite - http.server.requests 
InfluxDB - http_server_requests 

NamingConvention 5种默认的转换规则 dot snakeCase camelCase upperCamelCase slashes


测试代码
https://gitee.com/dyyx/springboothello/blob/master/src/test/java/dyyx/MicrometerTest.java

上一篇     下一篇
zookeeper在kafka中的应用

zookeeper如何保证数据一致性

JVM GC 过程简介

zookeeper 与 dubbo

LongAdder和LongAccumulator

mysql面试题