【问题标题】:getting the metrics of an endpoint获取端点的指标
【发布时间】:2019-07-12 15:52:20
【问题描述】:

我的 API 有一个端点,现在我想跟踪每当向我的端点发出请求时,有多少返回 200,有多少返回 400 或其他 HTTP 状态代码,请告知我如何实现在 spring boot 项目中也是如此,我使用的是 spring boot actuators 2.

假设我的终点是:

https://localhost:9090/users

所以我想要的是:

{  
    "404": 1,
    "200": 6,
    "409": 1
}

【问题讨论】:

  • 欢迎来到 StackOverflow!请阅读如何提出一个好问题here。我建议提供有关您的设置、问题和最小的、可重现的示例的更多信息。
  • 您好,请在下面查看我的答案并让我知道结果

标签: java rest spring-boot spring-boot-actuator


【解决方案1】:

您可以使用/actuator/metrics/ 获取所有执行/调用的端点及其计数、异常、结果、状态、总时间等。对于这种情况计数、状态 很有用

为此,您必须在 pom.xml 中添加一个依赖项或在等级中添加类似项

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

Get Dependency


获取执行/调用的all endPoint 的详细信息(查看结果所有端点)

localhost:8889/actuator/metrics/http.server.requests

获取particular endPoint 的详细信息(请参阅结果详情 端点)

localhost:8889/actuator/metrics/http.server.requests?tag=uri:<endPoint>
localhost:8889/actuator/metrics/http.server.requests?tag=uri:/users

使用状态码获取特定端点的计数(见结果 带有状态码的特定端点)

localhost:8889/actuator/metrics/http.server.requests?tag=uri:/users&tag=status:200

结果所有端点

{
    "name": "http.server.requests",
    "description": null,
    "baseUnit": "seconds",
    "measurements": [
        {
            "statistic": "COUNT",
            "value": 13
        },
        {
            "statistic": "TOTAL_TIME",
            "value": 0.42338
        },
        {
            "statistic": "MAX",
            "value": 0
        }
    ],
    "availableTags": [
        {
            "tag": "exception",
            "values": [
                "None"
            ]
        },
        {
            "tag": "method",
            "values": [
                "GET"
            ]
        },
        {
            "tag": "uri",
            "values": [
                "/actuator/metrics/{requiredMetricName}",
                "/getCountByStatus"
            ]
        },
        {
            "tag": "outcome",
            "values": [
                "CLIENT_ERROR",
                "SUCCESS"
            ]
        },
        {
            "tag": "status",
            "values": [
                "404",
                "200"
            ]
        }
    ]
}

查看结果特定端点

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

带有状态码的结​​果特定端点

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

【讨论】:

    【解决方案2】:

    在您的 pom.xml 文件中配置 Spring boot 执行器依赖项。

    此端点localhost:8080/actuato/metrics可以访问您需要的指标

    它将包含线程、堆内存、剩余内存等统计信息。

    对于您的情况,该 JSON 响应中的这两个键将很有用。

    量规和计数器

    Guage - 将给出每个 API 的响应时间。

    计数器 - 这将完全满足您的要求,例如 { 200:4, 400:1 }

    说明: 您的 API 4 次返回 200 状态码,1 次返回 400 状态码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      • 2018-11-16
      • 2016-08-29
      • 2018-09-08
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多