Prometheus云原生监控:运维与开发实战
上QQ阅读APP看书,第一时间看更新

3.2.5 效果展示

通过Spring Boot应用程序访问默认的8080端口http://localhost:8080/actuator/可以获取Spring Boot提供的各项指标,如下所示。


{"_links":{"self":{"href":"http:// localhost:8080/actuator","templated":false},
"auditevents":{"href":"http:// localhost:8080/actuator/auditevents","templated":false},
"beans":{"href":"http:// localhost:8080/actuator/beans","templated":false},
"caches-cache":{"href":"http:// localhost:8080/actuator/caches/{cache}","templated":
  true},"caches":{"href":"http:// localhost:8080/actuator/caches","templated":
  false},
"health-component":{"href":"http:// localhost:8080/actuator/health/{component}",
  "templated":true},
"health":{"href":"http:// localhost:8080/actuator/health","templated":false},
"health-component-instance":{"href":"http:// localhost:8080/actuator/health/
  {component}/{instance}","templated":true},
"conditions":{"href":"http:// localhost:8080/actuator/conditions","templated":
  false},
"shutdown":{"href":"http:// localhost:8080/actuator/shutdown","templated":
  false},
"configprops":{"href":"http:// localhost:8080/actuator/configprops","templated":
  false},
"env":{"href":"http:// localhost:8080/actuator/env","templated":false},
"env-toMatch":{"href":"http:// localhost:8080/actuator/env/{toMatch}","templated":
  true},
"info":{"href":"http:// localhost:8080/actuator/info","templated":false},
"loggers":{"href":"http:// localhost:8080/actuator/loggers","templated":false},
"loggers-name":{"href":"http:// localhost:8080/actuator/loggers/{name}",
  "templated":true},"heapdump":{"href":"http:// localhost:8080/actuator/heapdump",
  "templated":false},
"threaddump":{"href":"http:// localhost:8080/actuator/threaddump","templated":
  false},
"prometheus":{"href":"http:// localhost:8080/actuator/prometheus","templated":
  false},
"metrics":{"href":"http:// localhost:8080/actuator/metrics","templated":false},
"metrics-requiredMetricName":{"href":"http:// localhost:8080/actuator/metrics/
  {requiredMetricName}","templated":true},
"scheduledtasks":{"href":"http:// localhost:8080/actuator/scheduledtasks","temp
  lated":false},
"httptrace":{"href":"http:// localhost:8080/actuator/httptrace","templated":
  false},
"mappings":{"href":"http:// localhost:8080/actuator/mappings","templated":
  false}}}

Spring Boot的指标其实是层级结构,我们可以针对上述任意一项指标进行单独查询,比如:


"metrics":{"href":"http:// localhost:8080/actuator/metrics","templated":false}

通过访问http://localhost:8080/actuator/metrics就可以得到这个Spring Boot案例程序提供的监控指标,如下所示。


{"names":["logback.events","tomcat.global.sent","process.files.open","jvm.buffer.
  memory.used","jvm.memory.committed","http.server.requests","tomcat.sessions.
  active.max","jvm.threads.states","tomcat.global.request.max","jvm.gc.pause",
  "tomcat.global.request","jvm.memory.max","process.start.time","tomcat.global.
  received","process.files.max","jvm.gc.memory.promoted","jvm.memory.used",
  "prometheus.demo.gauge","system.load.average.1m","jvm.gc.max.data.size","tomcat.
  threads.config.max","system.cpu.count","tomcat.global.error","tomcat.sessions.
  created","jvm.threads.daemon","system.cpu.usage","tomcat.threads.current","jvm.
  gc.memory.allocated","prometheus.demo.counter","jvm.buffer.count","tomcat.
  sessions.expired","jvm.buffer.total.capacity","jvm.threads.live","jvm.threads.
  peak","tomcat.threads.busy","process.uptime","tomcat.sessions.rejected",
  "process.cpu.usage","jvm.classes.loaded","jvm.classes.unloaded","tomcat.sessions.
  active.current","tomcat.sessions.alive.max","jvm.gc.live.data.size"]}

上述信息中与JVM相关的就是我们的Maven依赖中micrometer-jvm-extras包引入的,这些信息都是列表类型的,比如jvm.buffer.memory.used和jvm.gc.pause可以分别通过访问http://localhost:8080/actuator/metrics/jvm.buffer.memory.usedhttp://localhost:8080/actuator/metrics/jvm.gc.pause获取,获取的数据分别如下所示。

1)jvm.buffer.memory.used数据:


{"name":"jvm.buffer.memory.used","description":"An estimate of the memory that 
  the Java virtual machine is using for this buffer pool","baseUnit":"bytes",
  "measurements":[{"statistic":"VALUE","value":90112.0}],"availableTags":[{"tag":
  "application","values":["Demo"]},{"tag":"id","values":["direct","mapped"]}]}

2)jvm.gc.pause数据:


{"name":"jvm.gc.pause","description":"Time spent in GC pause","baseUnit":"seconds",
  "measurements":[{"statistic":"COUNT","value":3.0},{"statistic":"TOTAL_TIME",
  "value":0.074},{"statistic":"MAX","value":0.0}],"availableTags":[{"tag":"app
  lication","values":["Demo"]},{"tag":"cause","values":["Metadata GC Threshold",
  "Allocation Failure"]},{"tag":"action","values":["end of minor GC","end of major 
  GC"]}]}

在Spring Boot 2.x项目中的DemoMetrics.java文件里,我们定义的两个指标prometheus.demo.counter和prometheus.demo.gauge也出现在http://localhost:8080/actuator/metrics中,可以分别通过请求http://localhost:8080/actuator/metrics/prometheus.demo.counterhttp://localhost:8080/actuator/metrics/prometheus.demo.gauge来获取相关数据。

1)prometheus.demo.counter数据:


{"name":"prometheus.demo.counter","description":"demo counter","baseUnit":null,
  "measurements":[{"statistic":"COUNT","value":1264.0}],"availableTags":[{"tag":
  "application","values":["Demo"]},{"tag":"name","values":["counter1"]}]}

2)prometheus.demo.gauge数据:


{"name":"prometheus.demo.gauge","description":"This is Gauge","baseUnit":null,
  "measurements":[{"statistic":"VALUE","value":1271.0}],"availableTags":[{"tag":
  "application","values":["Demo"]},{"tag":"name","values":["gauge1"]}]}

因为Spring Boot 2.x中模拟访问请求的计时器每秒执行一次,所以上述这两个数据此时已经分别按照先后顺序更新到1264.0和1271.0。细心的读者可以看到,数据中有tag,比如{"tag":"name","values":["counter1"]},这是一个tag数组,如果想看到具体tag的内容,访问http://localhost:8080/actuator/metrics/prometheus.demo.counter?tag=name:counter1就可以进入下一级,获取的返回数据就是更为细化的数据。


{"name":"prometheus.demo.counter","description":"demo counter","baseUnit":null,
  "measurements":[{"statistic":"COUNT","value":1539.0}],"availableTags":[{"tag":
  "application","values":["Demo"]}]}

同理,设置的Gauge指标也可以用类似的方法进行获取。

以上就是Prometheus通过Micrometer集成Spring Boot的基本原理和方法。