聊聊springboot2的micrometer

本文主要研究下springboot2的micrometer

micrometer

springboot2在spring-boot-actuator中引入了micrometer,对1.x的metrics进行了重构,另外支持对接的监控系统也更加丰富(Atlas、Datadog、Ganglia、Graphite、Influx、JMX、NewRelic、Prometheus、SignalFx、StatsD、Wavefront)。1.x的metrics都有点对齐dropwizard-metrics的味道,而micrometer除了一些基本metrics与dropwizard-metrics相类似外,重点支持了tag。这是一个很重要的信号,标志着老一代的statsd、graphite逐步让步于支持tag的influx以及prometheus。

etsy原版的statsd是不支持tag的,不过datadog以及influx都有对statsd进行改良以支持tag。而influxdb以及prometheus则是天生支持tag的。

对比

  • dropwizard-metrics guage final Gauge<Long> sizeGauge = new Gauge<Long>() { @Override public Long getValue() { return bufferPoolMXBean.getCount(); } };
  • micrometer Gauge gauge = Gauge .builder("gauge", myObj, myObj::gaugeValue) .description("a description of what this gauge does") // optional .tags("region", "test") // optional .register(registry); 可以看到micrometer支持tag

metrics tag/label

关于metrics是否支持tag/label,则代表其metrics是否能够有多维度的支持。像statsd不支持tag,如果要区分多host的同一个jvm指标,则通常是通过添加prefix来解决,不过这个给查询统计以及后续扩展带了诸多的不变。

支持tag的好处就是可以进行多维度的统计和查询,以同一微服务但是不同实例的jvm指标来说,可以通过tag来添加host标识,这样监控系统就可以灵活根据tag查询过滤来查看不同主机粒度的,甚至是不同数据中心的粒度。

Prometheus

springboot2启用/actuator/prometheus端点,供Prometheus来抓取指标。

maven

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

配置

management.endpoints.web.exposure.include=*

/actuator/prometheus

# HELP process_cpu_usage The "recent cpu usage" for the Java Virtual Machine process
# TYPE process_cpu_usage gauge
process_cpu_usage 0.0
# HELP tomcat_global_request_seconds  
# TYPE tomcat_global_request_seconds summary
tomcat_global_request_seconds_count{name="http-nio-8080",} 2.0
tomcat_global_request_seconds_sum{name="http-nio-8080",} 0.111
# HELP tomcat_global_received_bytes_total  
# TYPE tomcat_global_received_bytes_total counter
tomcat_global_received_bytes_total{name="http-nio-8080",} 0.0
# HELP jvm_buffer_count An estimate of the number of buffers in the pool
# TYPE jvm_buffer_count gauge
jvm_buffer_count{id="mapped",} 0.0
jvm_buffer_count{id="direct",} 4.0
# HELP jvm_threads_live The current number of live threads including both daemon and non-daemon threads
# TYPE jvm_threads_live gauge
jvm_threads_live 43.0
# HELP process_files_open The open file descriptor count
# TYPE process_files_open gauge
process_files_open 84.0
# HELP tomcat_threads_current  
# TYPE tomcat_threads_current gauge
tomcat_threads_current{name="http-nio-8080",} 10.0
# HELP tomcat_servlet_error_total  
# TYPE tomcat_servlet_error_total counter
tomcat_servlet_error_total{name="default",} 0.0
# HELP process_start_time_seconds The start time of the Java virtual machine
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.521297826521E9
# HELP tomcat_sessions_active_max  
# TYPE tomcat_sessions_active_max gauge
tomcat_sessions_active_max 0.0
# HELP tomcat_servlet_request_max_seconds  
# TYPE tomcat_servlet_request_max_seconds gauge
tomcat_servlet_request_max_seconds{name="default",} 0.0
# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{area="nonheap",id="CodeHeap 'non-nmethods'",} 3073408.0
jvm_memory_used_bytes{area="nonheap",id="Metaspace",} 4.2002072E7
jvm_memory_used_bytes{area="nonheap",id="Compressed Class Space",} 5467256.0
jvm_memory_used_bytes{area="heap",id="G1 Eden Space",} 1.05906176E8
jvm_memory_used_bytes{area="heap",id="G1 Old Gen",} 1.8914304E7
jvm_memory_used_bytes{area="heap",id="G1 Survivor Space",} 1.048576E7
jvm_memory_used_bytes{area="nonheap",id="CodeHeap 'non-profiled nmethods'",} 9643520.0
# HELP tomcat_sessions_rejected_total  
# TYPE tomcat_sessions_rejected_total counter
tomcat_sessions_rejected_total 0.0
# HELP system_load_average_1m The sum of the number of runnable entities queued to available processors and the number of runnable entities running on the available processors averaged over a period of time
# TYPE system_load_average_1m gauge
system_load_average_1m 3.796875
# HELP tomcat_threads_config_max  
# TYPE tomcat_threads_config_max gauge
tomcat_threads_config_max{name="http-nio-8080",} 200.0
# HELP tomcat_cache_hit_total  
# TYPE tomcat_cache_hit_total counter
tomcat_cache_hit_total 0.0
# HELP tomcat_sessions_active_current  
# TYPE tomcat_sessions_active_current gauge
tomcat_sessions_active_current 0.0
# HELP jvm_gc_pause_seconds Time spent in GC pause
# TYPE jvm_gc_pause_seconds summary
jvm_gc_pause_seconds_count{action="end of minor GC",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",cause="Metadata GC Threshold",} 0.017
jvm_gc_pause_seconds_max{action="end of minor GC",cause="Metadata GC Threshold",} 0.017
jvm_gc_pause_seconds_count{action="end of minor GC",cause="G1 Evacuation Pause",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",cause="G1 Evacuation Pause",} 0.019
jvm_gc_pause_seconds_max{action="end of minor GC",cause="G1 Evacuation Pause",} 0.019
# HELP logback_events_total Number of error level events that made it to the logs
# TYPE logback_events_total counter
logback_events_total{level="error",} 0.0
logback_events_total{level="warn",} 0.0
logback_events_total{level="info",} 39.0
logback_events_total{level="debug",} 0.0
logback_events_total{level="trace",} 0.0
# HELP jvm_gc_memory_promoted_bytes_total Count of positive increases in the size of the old generation memory pool before GC to after GC
# TYPE jvm_gc_memory_promoted_bytes_total counter
jvm_gc_memory_promoted_bytes_total 0.0
# HELP jvm_buffer_total_capacity_bytes An estimate of the total capacity of the buffers in this pool
# TYPE jvm_buffer_total_capacity_bytes gauge
jvm_buffer_total_capacity_bytes{id="mapped",} 0.0
jvm_buffer_total_capacity_bytes{id="direct",} 32768.0
# HELP jvm_classes_loaded The number of classes that are currently loaded in the Java virtual machine
# TYPE jvm_classes_loaded gauge
jvm_classes_loaded 8219.0
# HELP jvm_classes_unloaded_total The total number of classes unloaded since the Java virtual machine has started execution
# TYPE jvm_classes_unloaded_total counter
jvm_classes_unloaded_total 0.0
# HELP tomcat_sessions_alive_max_seconds  
# TYPE tomcat_sessions_alive_max_seconds gauge
tomcat_sessions_alive_max_seconds 0.0
# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes -1.0
# HELP tomcat_sessions_created_total  
# TYPE tomcat_sessions_created_total counter
tomcat_sessions_created_total 0.0
# HELP system_cpu_usage The "recent cpu usage" for the whole system
# TYPE system_cpu_usage gauge
system_cpu_usage 0.0
# HELP jvm_threads_daemon The current number of live daemon threads
# TYPE jvm_threads_daemon gauge
jvm_threads_daemon 41.0
# HELP system_cpu_count The number of processors available to the Java virtual machine
# TYPE system_cpu_count gauge
system_cpu_count 8.0
# HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management
# TYPE jvm_memory_max_bytes gauge
jvm_memory_max_bytes{area="nonheap",id="CodeHeap 'non-nmethods'",} 7553024.0
jvm_memory_max_bytes{area="nonheap",id="Metaspace",} -1.0
jvm_memory_max_bytes{area="nonheap",id="Compressed Class Space",} 1.073741824E9
jvm_memory_max_bytes{area="heap",id="G1 Eden Space",} -1.0
jvm_memory_max_bytes{area="heap",id="G1 Old Gen",} 4.294967296E9
jvm_memory_max_bytes{area="heap",id="G1 Survivor Space",} -1.0
jvm_memory_max_bytes{area="nonheap",id="CodeHeap 'non-profiled nmethods'",} 2.44105216E8
# HELP tomcat_sessions_expired_total  
# TYPE tomcat_sessions_expired_total counter
tomcat_sessions_expired_total 0.0
# HELP tomcat_global_sent_bytes_total  
# TYPE tomcat_global_sent_bytes_total counter
tomcat_global_sent_bytes_total{name="http-nio-8080",} 2524.0
# HELP tomcat_global_error_total  
# TYPE tomcat_global_error_total counter
tomcat_global_error_total{name="http-nio-8080",} 0.0
# HELP process_files_max The maximum file descriptor count
# TYPE process_files_max gauge
process_files_max 10240.0
# HELP jvm_gc_live_data_size_bytes Size of old generation memory pool after a full GC
# TYPE jvm_gc_live_data_size_bytes gauge
jvm_gc_live_data_size_bytes 0.0
# HELP jvm_gc_memory_allocated_bytes_total Incremented for an increase in the size of the young generation memory pool after one GC to before the next
# TYPE jvm_gc_memory_allocated_bytes_total counter
jvm_gc_memory_allocated_bytes_total 4977768.0
# HELP tomcat_threads_busy  
# TYPE tomcat_threads_busy gauge
tomcat_threads_busy{name="http-nio-8080",} 1.0
# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for  the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{area="nonheap",id="CodeHeap 'non-nmethods'",} 3080192.0
jvm_memory_committed_bytes{area="nonheap",id="Metaspace",} 4.5088768E7
jvm_memory_committed_bytes{area="nonheap",id="Compressed Class Space",} 6029312.0
jvm_memory_committed_bytes{area="heap",id="G1 Eden Space",} 1.92937984E8
jvm_memory_committed_bytes{area="heap",id="G1 Old Gen",} 1.19537664E8
jvm_memory_committed_bytes{area="heap",id="G1 Survivor Space",} 1.048576E7
jvm_memory_committed_bytes{area="nonheap",id="CodeHeap 'non-profiled nmethods'",} 9699328.0
# HELP jvm_threads_peak The peak live thread count since the Java virtual machine started or peak was reset
# TYPE jvm_threads_peak gauge
jvm_threads_peak 49.0
# HELP http_server_requests_seconds  
# TYPE http_server_requests_seconds summary
http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/actuator",} 1.0
http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/actuator",} 0.046773049
http_server_requests_seconds_max{exception="None",method="GET",status="200",uri="/actuator",} 0.046773049
http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/**/favicon.ico",} 1.0
http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/**/favicon.ico",} 0.020588395
http_server_requests_seconds_max{exception="None",method="GET",status="200",uri="/**/favicon.ico",} 0.020588395
# HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool
# TYPE jvm_buffer_memory_used_bytes gauge
jvm_buffer_memory_used_bytes{id="mapped",} 0.0
jvm_buffer_memory_used_bytes{id="direct",} 32768.0
# HELP tomcat_global_request_max_seconds  
# TYPE tomcat_global_request_max_seconds gauge
tomcat_global_request_max_seconds{name="http-nio-8080",} 0.087
# HELP tomcat_cache_access_total  
# TYPE tomcat_cache_access_total counter
tomcat_cache_access_total 0.0
# HELP tomcat_servlet_request_seconds  
# TYPE tomcat_servlet_request_seconds summary
tomcat_servlet_request_seconds_count{name="default",} 0.0
tomcat_servlet_request_seconds_sum{name="default",} 0.0
# HELP process_uptime_seconds The uptime of the Java virtual machine
# TYPE process_uptime_seconds gauge
process_uptime_seconds 14.232

比如jvm_memory_used_bytes

jvm_memory_used_bytes{area="nonheap",id="CodeHeap 'non-nmethods'",} 3073408.0
jvm_memory_used_bytes{area="nonheap",id="Metaspace",} 4.2002072E7
jvm_memory_used_bytes{area="nonheap",id="Compressed Class Space",} 5467256.0
jvm_memory_used_bytes{area="heap",id="G1 Eden Space",} 1.05906176E8
jvm_memory_used_bytes{area="heap",id="G1 Old Gen",} 1.8914304E7
jvm_memory_used_bytes{area="heap",id="G1 Survivor Space",} 1.048576E7
jvm_memory_used_bytes{area="nonheap",id="CodeHeap 'non-profiled nmethods'",} 9643520.0

括号里头的就tag/label,查询的时候就可以使用jvm_memory_used_bytes或者jvm_memory_used_bytes{area=”nonheap”}或者jvm_memory_used_bytes{area=”nonheap”,id=”Metaspace”}等配合tag/label进行查询。

小结

springboot2的micrometer支持了tag/label,配合支持tag/label的监控系统,使得我们可以更加方便地对metrics进行多维度的统计查询及监控。

doc

  • metrics.dropwizard
  • Support for Statsd Tags/Dimensions #1536
  • Getting Started with Sending StatsD Metrics to Telegraf & InfluxDB
  • How can I send multiple tags in statsd #567
  • Using Prometheus in Grafana
  • prometheus QUERY EXAMPLES
  • Using Micrometer With Spring Boot 2
  • Micrometer: Spring Boot 2’s new application metrics collector

原文发布于微信公众号 - 码匠的流水账(geek_luandun)

原文发表时间:2018-03-17

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏乐沙弥的世界

ERROR 1160 (08S01) : Got an error writing communication packets

    应用程序端碰到错误提示,MySQLNonTransientConnectionException:Got an error writing commun...

1720
来自专栏杨建荣的学习笔记

关于生产系统锁问题的排查(r3笔记第79天)

今天生产系统中的一个daemon出现了严重的数据处理延迟,客户需要我们立即给出处理的方案。在综合评估之后,为了不保证在线业务延迟,开发部门给出了临时的解决意见。...

2995
来自专栏陈仁松博客

暴力遍历还没注册的双拼域名

最近突然发现双拼域名越来越少,价格也在不断上涨。想注册一个有趣的双拼域名玩玩,于是动手写了一个暴力查询双拼域名的工具。 思路比较简单,首先找到域名查询的接口,这...

5976
来自专栏乐沙弥的世界

delete archivelog all 无法彻底删除归档日志?

    最近在因归档日志暴增,使用delete archivelog all貌似无法清除所有的归档日志,到底是什么原因呢?

921
来自专栏Java学习123

IBM WebSphere MQ 系列(四) 使用MQ命令

6996
来自专栏阿杜的世界

Mybatis 3学习笔记(一)主要内容:

MyBatis是一个开源持久化框架,用于简化持久层的实现。Mybatis可以减少很多JDBC相关的模板样式代码,还提供了方便使用的数据库API。

823
来自专栏古时的风筝

Spring集成MyBatis

如果觉得Hibernate不够灵活,可以尝试用Mybatis。相比于Hibernate,Mybatis则显得更加轻量、更加灵活,对于我这种不喜欢被束缚的人,使用...

2477
来自专栏蓝天

mysql_init调用卡住原因分析

看似简单的代码,但非常不幸,程序运行时,卡在了mysql_init处。语法上看不出任何破绽,原因会是什么了?

1092
来自专栏杨建荣的学习笔记

配置dg broker的问题分析及修复(r6笔记第84天)

最近从同事那儿接手了一套新环境,备库因为服务器问题已经下架,重新配了一台服务器,所以需要搭一套备库,主库已经配置好了,而且同事已经把在主库把dg broker配...

3787
来自专栏zhisheng

Elasticsearch自定义分词器

下载地址:https://github.com/medcl/elasticsearch-analysis-mmseg/releases 根据对应的版本进行下载

9143

扫码关注云+社区

领取腾讯云代金券