java - 获取端点的指标

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

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

假设我的端点是:

https://localhost:9090/users

所以我想要的是:

{  
    "404": 1,
    "200": 6,
    "409": 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

<小时/>

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

localhost:8889/actuator/metrics/http.server.requests
<小时/>

要获取特定终点的详细信息(请参阅特定结果 端点)

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

关于java - 获取端点的指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57010306/

相关文章:

java - 竖线 ('|' ) 在 Android 中意味着什么?

java - 在Libgdx上获取鼠标和 Sprite 的坐标

c++ - 链中的最后一个任务应该返回什么 - C++ Rest sdk?

java - 是否可以在不执行 Spring Controller 方法的情况下检查权限?

java - Spring Boot 中 WebClient 中的 POST 请求并以 JSONObject 形式接收响应

java - 配置嵌入式 MongoDB 的最简单方法

java - 某些东西(Tomcat?)取代了我的 Rest 响应

java - 使用java Netbeans中的插入查询在ms数据库中插入数据

java - Axis2 Web 服务客户端生成 - 无需修改客户端的类型

java - 如何使用 Rest API 和 Java 在 Egnyte 中上传 pdf 文件