[英]Metrics Collection for Spring Boot REST APIs
我正在嘗試為我的 Spring Boot(2.1.0.RELEASE) 應用程序收集指標。 具體我想知道
執行器/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
獲取執行的所有端點及其計數、異常、結果、狀態、總時間等,如下所示。
如果您想查看特定端點的詳細信息,則可以通過調用 request 來完成,如下所示
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
本地主機: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"
]
}
]
}
本地主機: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 Admin 。 為此,我們必須配置客戶端 - 服務器。 為避免該錯誤,請確保客戶端-服務器依賴項的版本相同。 我們可以從下拉列表中添加所需的指標,如圖所示。
pom.xml
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.4</version>
</dependency>
應用程序屬性
spring.boot.admin.api-path=/instances
spring.boot.admin.client.url=http://localhost:6699
management.endpoints.web.exposure.include=*
應用程序屬性
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);
}
}
使用執行器:要將執行器添加到基於 Maven 的項目,請添加以下“啟動器”依賴項:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
對於 Gradle,請使用以下聲明:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
Actuator 端點可讓您監控應用程序並與之交互。 Spring Boot 包含許多內置端點,並允許您添加自己的端點。 例如,健康端點提供基本的應用程序健康信息。 默認情況下,健康端點映射到 /actuator/health。
默認情況下,除關閉之外的所有端點都處於啟用狀態。
由於端點可能包含敏感信息,您應該仔細考慮何時公開它們。 在 application.properties 文件中添加以下內容以啟用健康和信息
management.endpoints.jmx.exposure.include=health,info
或者要啟用除 env 和 bean 之外的所有內容,請執行以下操作
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,bean
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.