我正在尝试在具有以下依赖项的 springboot 项目中启用 Prometheus 端点。
SpringBoot版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.12-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
依赖关系:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
我正在使用restTemplate进行出站调用,但是当我检查Prometheus端点时,与http_client_requests_*相关的数据丢失了
应用程序属性:
management.endpoints.web.exposure.include=health,metrics,prometheus
management.endpoint.health.show-details=always
management.endpoint.health.enabled=true
management.endpoint.info.enabled=true
management.metrics.web.server.request.autotime.percentiles=0.90,0.95
management.metrics.web.client.request.autotime.percentiles=0.90,0.95
management.metrics.web.client.request.autotime.enabled=true
最佳答案
只需确保将 RestTemplate 创建为 Spring 托管 bean,以确保它有机会进行检测(如果不是自动创建)。您可以使用 RestTemplateBuilder 进行自定义,然后像任何其他 Spring bean 一样将其注入(inject)到您需要的位置。
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.additionalCustomizers(myRestTemplateCustomizer())
.build();
}
您可能还需要
<!-- Micrometer core dependency -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
手动创建(在类中使用“new”)是其无法链接到指标的原因之一。
关于spring-boot - Spring Boot Promethus 端点中缺少 http_client_requests_seconds_count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73607334/