【问题标题】:Spring Actuator: Configure multiple Prometheus endpoint with different dataSpring Actuator:使用不同的数据配置多个 Prometheus 端点
【发布时间】:2022-09-23 18:04:30
【问题描述】:
目前,我们正在尝试设置 Prometheus 来监控我们的服务,内部和外部。问题是我们无法为我们的一些外部服务配置 Prometheus,但我们希望这些仍然作为 Prometheus 中的单独作业包含在内。
我想要 2 个不同的 Prometheus 端点(例如 /actuator/prometheus/api 和 /actuator/prometheus/thingworx)返回不同的数据。
-
/actuator/prometheus/api 将拥有 API 的实际数据(类似于如果您只是安装包)。
-
/actuator/prometheus/thingworx 只会返回我们在特定时间间隔从外部服务获得的一些自定义指标。
理想情况下,这应该在单个 Spring 服务器上完成。这可以用弹簧执行器和千分尺完成还是不可能?
标签:
java
spring-boot
prometheus
spring-actuator
【解决方案1】:
搜索后,我决定用另一种方式来做。因为您无法轻松修改 prometheus 端点本身以包含其他路径(尝试WebEndpointExtension,但没有成功),我创建了自己的自定义端点,该端点从包含由 Spring Boot 自动装配的主注册表的服务和另一个包含定期更新的自定义注册表的服务中获取数据。
@RestController
@RestControllerEndpoint(id = "multiPrometheus")
public class PrometheusController {
private final APIPrometheusService apiService;
private final ThingworxPrometheusService thingworxService;
public PrometheusController( APIPrometheusService apiService, ThingworxPrometheusService thingworxService) {
this.apiService = apiService;
this.thingworxService = thingworxService;
}
@GetMapping( value = "/api", produces = TEXT_PLAIN_VALUE)
public String getPrometheusApiStream(){
return apiService.scrape();
}
@GetMapping(value = "/thingworx", produces = TEXT_PLAIN_VALUE)
public String getPrometheusThingworxStream(){
if(thingworxService.isConnected()){
return thingworxService.scrape();
}
throw new ResponseStatusException(SERVICE_UNAVAILABLE);
}
}
这样我就可以完全控制位于/actuator 下的端点的路径映射