【问题标题】:Metrics Collection for Spring Boot REST APIsSpring Boot REST API 的指标集合
【发布时间】:2019-11-21 01:47:47
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: java spring-boot spring-boot-actuator spring-boot-admin spring-micrometer


    【解决方案1】:

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

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

    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 作为特定端点的次数
      特定状态调用
    • 要获得执行端点的平均时间,您可以这样做 TOTAL_TIME/COUNT 用于特定端点以及整个 应用

    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"
                ]
            }
        ]
    }
    

    【讨论】:

    • 但是如果我们重新启动应用程序会丢失现有数据,有没有办法防止这种情况发生?另外,如果我想将类似的指标添加到与外部服务器通信的类中,这在执行器级别上是否可行?例如,我想监控将外部请求发送到另一个应用程序所花费的时间。
    • 而且你不会马上看到端点,如果你是从本地运行,点击 api 一次然后它会显示在 http.server.requests 下
    【解决方案2】:

    另一种方法是使用 Spring Boot Admin。为此,我们必须配置客户端-服务器。为避免该错误,请确保客户端-服务器依赖项的版本相同。如图所示,我们可以从下拉列表中添加所需的指标。

    客户端:

    pom.xml

    <dependency>
         <groupId>de.codecentric</groupId>
         <artifactId>spring-boot-admin-starter-client</artifactId>
         <version>2.1.4</version>
    </dependency>
    

    application.properties

    spring.boot.admin.api-path=/instances
    spring.boot.admin.client.url=http://localhost:6699
    management.endpoints.web.exposure.include=*
    

    服务器端:

    application.properties

    server.port = 6699
    spring.boot.admin.server.url=http://localhost:8889
    

    pom.xml

     <dependency>
             <groupId>de.codecentric</groupId>
             <artifactId>spring-boot-admin-starter-server</artifactId>
             <version>2.1.4</version>
        </dependency>
    

    添加@EnableAdminServer

    import de.codecentric.boot.admin.server.config.EnableAdminServer;
    
    @SpringBootApplication
    @EnableAdminServer
    public class AdminApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AdminApplication.class, args);
        }
    
    }
    

    GUI http://localhost:6699/#/applications

    主页

    指标

    健康

    图表

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2021-06-14
      • 2018-06-22
      • 2021-08-28
      • 2019-03-11
      • 2020-11-13
      相关资源
      最近更新 更多