SpringBoot中Rest Api的计数器 [英] Counter Metrics for Rest Api's in SpringBoot

查看:119
本文介绍了SpringBoot中Rest Api的计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好的方法是计算一次在春季启动中API的成功/失败响应被触发的次数.

What would be the best approach to count how many times an API is being triggered with its successful/failure response in spring boot.

我的想法是,在应用程序启动和调用api时,使用post构造启动一个新线程,然后对每个新的唯一请求使用计数器服务,以计算由触发的api数量该特定请求以及其中有多少成功或失败.

What I have in my mind is using a post construct to start a new thread when application comes up and when an api is being called and then using a counter service for each new unique request to count how many api's is being triggered by that particular request and how many of them are successful or failed.

如果有的话,推荐一些新方法.

Recommend some new approaches if you guys have any.

推荐答案

如果从头开始,则无需创建,只需使用内置了此功能的Spring Boot Actuator.只需将以下依赖项添加到您的项目中即可:

You don't have to create if from scratch, you can simply use Spring Boot Actuator that has this feature built-in. Just add following dependency to your project:

pom.xml :

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

执行器暴露例如 /metrics 端点,您可以在其中找到所有响应的计数器-每个计数器均以 counter.status.{{status_code}}.{{endpoint}} 开头.

例如,我有一个带有/hello 端点的简单演示应用程序.每当我将此端点指标称为 counter.status.200.hello 都会增加.当我访问/metrics 端点时,我会看到类似的内容:

For example, I have simple demo application with /hello endpoint. Any time I call this endpoint metric counter.status.200.hello will be increased. When I access /metrics endpoint I can see something like:

{
  "mem": 586013,
  "mem.free": 324496,
  "processors": 8,
  "instance.uptime": 144496,
  "uptime": 149950,
  "systemload.average": 0.52,
  "heap.committed": 522240,
  "heap.init": 251904,
  "heap.used": 197743,
  "heap": 3580928,
  "nonheap.committed": 64960,
  "nonheap.init": 2496,
  "nonheap.used": 63773,
  "nonheap": 0,
  "threads.peak": 42,
  "threads.daemon": 25,
  "threads.totalStarted": 54,
  "threads": 27,
  "classes": 8938,
  "classes.loaded": 8938,
  "classes.unloaded": 0,
  "gc.ps_scavenge.count": 8,
  "gc.ps_scavenge.time": 54,
  "gc.ps_marksweep.count": 2,
  "gc.ps_marksweep.time": 65,
  "gauge.response.star-star.favicon.ico": 1,
  "counter.status.200.star-star": 2,
  "counter.status.304.star-star": 2,
  "counter.status.200.metrics.name": 2,
  "counter.status.200.star-star.favicon.ico": 5,
  "gauge.response.star-star": 3,
  "gauge.response.hello": 5,
  "gauge.response.metrics.name": 3,
  "counter.status.200.hello": 2,
  "counter.status.404.metrics.name": 1,
  "httpsessions.max": -1,
  "httpsessions.active": 0,
  "datasource.primary.active": 0,
  "datasource.primary.usage": 0
}

我还可以访问单个指标,例如/metrics/counter.status.200.hello 仅返回:

I can also access single metric, e.g. /metrics/counter.status.200.hello which returns only:

{
  "counter.status.200.hello": 2
}

默认情况下,/metrics 端点是安全的.对于您的本地开发人员,您可以添加以下内容:

By default /metrics endpoint is secured. For your local dev you can turn it of by adding:

management:
  security:
    enabled: false

到您的 application.yml

management.security.enabled=false

application.properties (如果使用的话).

这篇关于SpringBoot中Rest Api的计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆