实践SpringBoot Actuator+Phrometheus实现应用监控

通常大型业务应用平台由多个相对独立的服务组成,监控每个服务的运行状态是一项必要的工作。SpringBoot提供了Actuator,可以提供大量监控数据。但是,很多时候我们需要定义业务级监控指标,而不仅仅是系统级指标。同时,我们也希望能将各种指标以一种标准的、统一的方式进行展示和监控。本文通过一个简单示例,展示如何以Actuator为基础自定义应用监控指标,并输出到Phrometheus进行监控。

概述

在进行示例前,首先需要了解Prometheus的基本概念,特别是Metric,同时要搭建Prometheus+Grafana的监控平台。

上述内容请阅读用Prometheus监控MongoDB慢查询的前两部分。

支持Actuator和Prometheus

Actuator提供了通过REST接口获取应用个方面信息的方法,并且支持将系统监控指标输出到Prometheus。

参考:Spring Boot Actuator

在SpringBoot项目中添加依赖包。

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

打开Endpoint

Actuator提供的每一类监控接口称之为Endpoint,默认情况下只有health是打开的。访问地址:

curl http://localhost:8080/actuator

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true}}}%    

可以看到其中只包括health。为了查看其他Endpoint,需要在配置文件中指定。

修改application.properties文件,添加内容:

management.endpoints.web.exposure.include=* 
management.endpoints.web.exposure.exclude=

management.endpoints.web.exposure.include指定为*,打开所有选项。

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},"configprops-prefix":{"href":"http://localhost:8080/actuator/configprops/{prefix}","templated":true},"env":{"href":"http://localhost:8080/actuator/env","templated":false},"env-toMatch":{"href":"http://localhost:8080/actuator/env/{toMatch}","templated":true},"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","templated":false},"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}%     

再次访问,可以看到内容多了很多,其中包括prometheus,我们访问:

curl http://localhost:8080/actuator/prometheus

... 省略很多内容
# 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="miscellaneous non-heap storage",} -1.0
jvm_memory_max_bytes{area="nonheap",id="class storage",} -1.0
jvm_memory_max_bytes{area="nonheap",id="JIT code cache",} 2.68435456E8
jvm_memory_max_bytes{area="heap",id="tenured-LOA",} 2.14643712E8
jvm_memory_max_bytes{area="nonheap",id="JIT data cache",} 4.02653184E8
jvm_memory_max_bytes{area="heap",id="nursery-survivor",} 2.87163392E8
jvm_memory_max_bytes{area="heap",id="nursery-allocate",} 7.86578432E8
jvm_memory_max_bytes{area="heap",id="tenured-SOA",} 4.078226432E9
# HELP process_start_time_seconds Start time of the process since unix epoch.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.62458330422E9

返回的内容非常多,这些都是基本系统监控指标,可以提供给Pormetheus进行处理。

配置Prometheus

如果已经按照概述的部分搭建好Prometheus监控平台,通过在配置文件中添加抓取目标,就可以对我们的演示应用进行监控了。

修改配置文件prometheus.yml

  - job_name: springboot-metrics
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ['docker.for.mac.host.internal:8080']

特别注意要设置metrics_path参数,Prometheus默认是/metrics,Spring Boot是/actuator/prometheus

显示抓取指标

自定义指标

Spring Boot Actuator不是使用Prometheus的client,而是micrometermicrometer是一项包装了各种监控工具的库,可以灵活实现监控指标。

参考:micrometer

Actuator基础上添加自定义监控指标比较简单,直接上代码:

port io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/webflux")
public class HelloWordController {

  private final String COUNTER_TMS_CUSTOM_REQUESTS = "tms.custom.requests";

  private final String TAG_BUSI = "busi";

  public HelloWordController(MeterRegistry registry) {
    // 自动将点改成下划线,自动加上'_total'的后缀
    // tag对应prometheus中的label
    // 这里必须指定tag,否则后面指定了tag和这里定义对应不上,tag的值不允许是null
    // tag最好设置个默认值,否则会出现1条没有label的数据
    Counter.builder(COUNTER_TMS_CUSTOM_REQUESTS).tags(TAG_BUSI, "abc").description("自定义计数器").register(registry);
  }

  @GetMapping("/helloworld")
  public Mono<String> helloWord(@RequestParam String busi) {
    Metrics.counter(COUNTER_TMS_CUSTOM_REQUESTS, TAG_BUSI, busi).increment();
    return Mono.just("hello WebFlux!");
  }
}

上面的代码就是根据请求的参数busi对请求进行分类计数,这里涉及到Prometheus Metric中Label的使用,可以参照一下代码中的注释。

我们发起写请求看看效果,每个请求都多发几次。

curl "http://localhost:8080/webflux/helloworld?busi=abc"

curl "http://localhost:8080/webflux/helloworld?busi=123"

curl "http://localhost:8080/webflux/helloworld?busi=xyz"

先直接调用接口看看指标收集情况。

curl http://localhost:8080/actuator/prometheus | grep tms

# HELP tms_custom_requests_total 自定义计数器
# TYPE tms_custom_requests_total counter
tms_custom_requests_total{busi="abc",} 3.0
tms_custom_requests_total{busi="123",} 4.0
tms_custom_requests_total{busi="xyz",} 5.0

我们看到系统已经把指标收集好。再进到Promethues中看看。

自定义指标

总结

通过上面的示例可以看到,以Spring Boot Actuator为基础添加自定义监控指标还是比较简答的。设计和实现应用的同时就考虑如何设计监控指标,并加入到代码中,非常有利于提高代码的质量,同时也加强了研发和运维之间的协作。在代码中加入监控指标是一项必要的也非常值得的投入!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容