我有一个 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>
<小时/>
获取执行/调用的所有 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/