Spring Boot REST API 的指标收集 [英] Metrics Collection for Spring Boot REST APIs

查看:49
本文介绍了Spring Boot REST API 的指标收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的 Spring Boot(2.1.0.RELEASE) 应用程序收集指标.具体我想知道

I am trying to collect metrics for my Spring Boot(2.1.0.RELEASE) Application. Specifically, I want to know

  1. 没有调用单个 REST 端点.
  2. 每个端点处理请求所用的时间.
  3. 我的请求被处理/出错的平均比率.

执行器 /actuator/metrics 端点提供了很多信息,但我不确定这些信息是否对我的情况有用.另外,有人可以判断 @Timed(或任何其他开箱即用的注释) 可用于实现这些统计数据,或者我必须在每个控制器方法中使用如下所示的内容:

The actuator /actuator/metrics endpoint gives lot of info but I am not sure if any of those are useful for my case. Also, can someone tell if @Timed(or any other out-of-the-box annotation) can be used for achieving those stats or I have to use something like below in every controller method:

  Timer timer = new SimpleMeterRegistry().timer("timer.name");
timer.record(() -> {
    // all logic here
});

我尝试在我的控制器方法上使用 @Timed,但它没有向 /actuator/metrics 端点添加任何新响应.

I tried using @Timed on my controller method but it doesn't adds any new response to the /actuator/metrics endpoint.

推荐答案

您可以使用 Spring Boot /actuator/metrics/http.server.requests 获取所有执行的端点及其计数,异常、结果、状态、总时间等如下.

You can use Spring Boot /actuator/metrics/http.server.requests to get all endPoints which are executed with their count, exception, outcome, status, total time, etc as follow.

如果您想查看特定端点的详细信息,则可以按如下方式调用请求来完成

If you want to see details for particular endPoint then you can do it by calling request as follow

localhost:8889/actuator/metrics/http.server.requests?tag=uri:<endPoint>
localhost:8889/actuator/metrics/http.server.requests?tag=uri:/user/asset/getAllAssets
localhost:8889/actuator/metrics/http.server.requests?tag=uri:/user/asset/getAllAssets&tag=status:200

  • 您将获得 COUNT 作为特定端点的次数叫
  • 您将获得 COUNT 作为特定端点的次数
    特定状态
  • 调用
  • 要获得执行 endPoint 的平均时间,您可以执行以下操作TOTAL_TIME/COUNT 用于特定端点以及整个申请
    • You will get COUNT as how many times particular endPoint has been called
    • You will get COUNT as how many times particular endPoint has been
      called with a particular Status
    • To get the average time to execute endPoint you can do TOTAL_TIME/COUNT for particular endPoint as well as for whole application
    • localhost:8889/actuator/metrics/http.server.requests

      {
          "name": "http.server.requests",
          "description": null,
          "baseUnit": "seconds",
          "measurements": [
              {
                  "statistic": "COUNT",
                  "value": 3
              },
              {
                  "statistic": "TOTAL_TIME",
                  "value": 0.21817219999999998
              },
              {
                  "statistic": "MAX",
                  "value": 0.1379249
              }
          ],
          "availableTags": [
              {
                  "tag": "exception",
                  "values": [
                      "MethodArgumentTypeMismatchException",
                      "None"
                  ]
              },
              {
                  "tag": "method",
                  "values": [
                      "GET"
                  ]
              },
              {
                  "tag": "uri",
                  "values": [
                      "/{id}.*",
                      "/user/asset/getAsset/{assetId}",
                      "/user/asset/getAllAssets"
                  ]
              },
              {
                  "tag": "outcome",
                  "values": [
                      "CLIENT_ERROR",
                      "SUCCESS"
                  ]
              },
              {
                  "tag": "status",
                  "values": [
                      "400",
                      "404",
                      "200"
                  ]
              }
          ]
      }
      

      localhost:8889/actuator/metrics/http.server.requests?tag=uri:/user/asset/getAllAssets

      {
          "name": "http.server.requests",
          "description": null,
          "baseUnit": "seconds",
          "measurements": [
              {
                  "statistic": "COUNT",
                  "value": 1
              },
              {
                  "statistic": "TOTAL_TIME",
                  "value": 0.1379249
              },
              {
                  "statistic": "MAX",
                  "value": 0
              }
          ],
          "availableTags": [
              {
                  "tag": "exception",
                  "values": [
                      "None"
                  ]
              },
              {
                  "tag": "method",
                  "values": [
                      "GET"
                  ]
              },
              {
                  "tag": "outcome",
                  "values": [
                      "SUCCESS"
                  ]
              },
              {
                  "tag": "status",
                  "values": [
                      "200"
                  ]
              }
          ]
      }
      

      这篇关于Spring Boot REST API 的指标收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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