Issue
Currently, we are experimenting with setting Prometheus up for monitoring for our services, internal as external. The problem is that we cannot configure Prometheus for some of our external services, but we would like these to be still included as a separate job in Prometheus.
I want to have 2 different Prometheus endpoints (e.g. /actuator/prometheus/api
and /actuator/prometheus/thingworx
) that have return different data.
/actuator/prometheus/api
would have the actual data of the API (similar like if you just install the package)./actuator/prometheus/thingworx
would only return some custom metrics that we get at certain intervals from our external service.
This should, ideally, be done on a single Spring server. Can this be done with Spring Actuator and Micrometer or is this impossible?
Solution
After searching, I decided to do it another way. As you can't easily modify the prometheus
endpoint itself to include other paths (Tried with WebEndpointExtension
, but didn't have any success), I created my own custom endpoint that fetches data from a service that contains the main registry that is autowired by Spring Boot and another service that contains a custom registry that is updated in intervals.
@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);
}
}
This way I have full control over the path mapping of my endpoints that live under /actuator
Answered By - BT_Code
Answer Checked By - Mary Flores (JavaFixing Volunteer)