当前位置:   article > 正文

Spring Boot 监控数据可视化图文教程_management.metrics.tags.application

management.metrics.tags.application

TIPS

本文基于Spring Boot 2.1.4,理论支持Spring Boot 2.x所有版本

众所周知,Spring Boot有个子项目Spring Boot Actuator,它为应用提供了强大的监控能力。从Spring Boot 2.0开始,Actuator将底层改为Micrometer,提供了更强、更灵活的监控能力。Micrometer是一个监控门面,可以类比成监控界的 Slf4j 。

借助Micrometer,应用能够对接各种监控系统,例如:

•AppOptics[1]•Atlas[2]•Datadog[3]•Dynatrace[4]•Elastic[5]•Ganglia[6]•Graphite[7]•Humio[8]•Influx[9]•JMX[10]•KairosDB[11]•New Relic[12]•Prometheus[13]•SignalFx[14]•Simple (in-memory)[15]•StatsD[16]•Wavefront[17]

下面演示如何对接 Prometheus ,并使用 Grafana 实现数据的可视化。

TIPS

童鞋们对Prometheus或Grafana不熟悉也没关系,本文是手把手文章,按步骤操作即可。

编码

编写代码

1 加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>io.micrometer</groupId>
  7. <artifactId>micrometer-registry-prometheus</artifactId>
  8. </dependency>

这里,我们为应用引入了 micrometer-registry-prometheus ,事实上,你想对接上文列表中的哪款监控系统,就写啥。例如想对接 Influx ,则需添加依赖 micrometer-registry-influx 。

2 写配置

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: prometheus-test
  6. management:
  7. endpoints:
  8. web:
  9. exposure:
  10. include: 'prometheus'
  11. metrics:
  12. tags:
  13. application: ${spring.application.name}

如配置所示,指定应用名为 prometheus-test ,并将 Actuator 的 /actuator/prometheus 端点暴露出来;management.metrics.tags.application=prometheus-test 作用是为指标设置一个名为application="prometheus-test" 的Tag,Tag是Prometheus提供的一种能力,从而实现更加灵活的筛选。

测试

1 启动应用

2 访问 http://localhost:8080/actuator/prometheus 可获得类似如下的结果:

  1. # HELP jvm_memory_used_bytes The amount of used memory
  2. # TYPE jvm_memory_used_bytes gauge
  3. jvm_memory_used_bytes{application="prometheus-test",area="heap",id="PS Old Gen",} 2.1193976E7
  4. jvm_memory_used_bytes{application="prometheus-test",area="nonheap",id="Metaspace",} 3.8791688E7
  5. jvm_memory_used_bytes{application="prometheus-test",area="heap",id="PS Survivor Space",} 0.0
  6. jvm_memory_used_bytes{application="prometheus-test",area="nonheap",id="Compressed Class Space",} 5303976.0
  7. jvm_memory_used_bytes{application="prometheus-test",area="heap",id="PS Eden Space",} 8.2574816E7
  8. jvm_memory_used_bytes{application="prometheus-test",area="nonheap",id="Code Cache",} 8693824.0
  9. # HELP tomcat_global_received_bytes_total
  10. # TYPE tomcat_global_received_bytes_total counter
  11. tomcat_global_received_bytes_total{application="prometheus-test",name="http-nio-8080",} 0.0
  12. # HELP jvm_threads_daemon_threads The current number of live daemon threads
  13. # TYPE jvm_threads_daemon_threads gauge
  14. jvm_threads_daemon_threads{application="prometheus-test",} 20.0
  15. # HELP tomcat_sessions_alive_max_seconds
  16. # TYPE tomcat_sessions_alive_max_seconds gauge
  17. tomcat_sessions_alive_max_seconds{application="prometheus-test",} 0.0
  18. # HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool
  19. # TYPE jvm_buffer_memory_used_bytes gauge
  20. jvm_buffer_memory_used_bytes{application="prometheus-test",id="mapped",} 0.0
  21. jvm_buffer_memory_used_bytes{application="prometheus-test",id="direct",} 90112.0
  22. # HELP jvm_threads_states_threads The current number of threads having NEW state
  23. # TYPE jvm_threads_states_threads gauge
  24. jvm_threads_states_threads{application="prometheus-test",state="runnable",} 9.0
  25. jvm_threads_states_threads{application="prometheus-test",state="new",} 0.0
  26. jvm_threads_states_threads{application="prometheus-test",state="terminated",} 0.0
  27. jvm_threads_states_threads{application="prometheus-test",state="blocked",} 0.0
  28. jvm_threads_states_threads{application="prometheus-test",state="waiting",} 12.0
  29. jvm_threads_states_threads{application="prometheus-test",state="timed-waiting",} 3.0
  30. # HELP process_cpu_usage The "recent cpu usage" for the Java Virtual Machine process
  31. # TYPE process_cpu_usage gauge
  32. process_cpu_usage{application="prometheus-test",} 0.0030590633504868434
  33. # HELP logback_events_total Number of error level events that made it to the logs
  34. # TYPE logback_events_total counter
  35. logback_events_total{application="prometheus-test",level="info",} 7.0
  36. logback_events_total{application="prometheus-test",level="warn",} 0.0
  37. logback_events_total{application="prometheus-test",level="trace",} 0.0
  38. logback_events_total{application="prometheus-test",level="debug",} 0.0
  39. logback_events_total{application="prometheus-test",level="error",} 0.0
  40. # HELP tomcat_global_sent_bytes_total
  41. # TYPE tomcat_global_sent_bytes_total counter
  42. tomcat_global_sent_bytes_total{application="prometheus-test",name="http-nio-8080",} 195356.0
  43. # HELP process_files_max_files The maximum file descriptor count
  44. # TYPE
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/425952?site
推荐阅读
相关标签
  

闽ICP备14008679号