Spring Boot REST API的度量标准集合 [英] Metrics Collection for Spring Boot REST APIs

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

问题描述

我正在尝试为我的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端点添加任何新的响应.

解决方案

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

如果您想查看特定端点的详细信息,则可以通过按以下方式调用请求来做到这一点

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用于特定端点以及整个端点 应用程序

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

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

  1. No of times individual REST endpoints were called.
  2. Time taken by each of those endpoints to process the request.
  3. Average rate at which my requests are being processed/errored.

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
});

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

解决方案

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

  • 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天全站免登陆