0
votes

Is it possible to expose all the Spring metrics in an rest endpoint?

In our environment Micrometer cannot forward directly the metrics to our monitoring system, then my idea was to have all the metrics exposed in a rest endpoint (or on a file, json format but the endpoint is preferable) and then having a script to fetch all the metrics at once, manipulate the payload and forward the metrics to our customized monitoring api.

If micrometer is not the right choice, what is it?

EDIT

I tried to use the actuator/prometheus endpoint. I can see the endpoint but as soon as I hit the endpoint, it hang and does not return anything:

I am using spring boot 2.2.2 and I added this dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.3.1</version>
</dependency>

application.yml

management:
  endpoints:
   web:
     exposure:
     include: health, metrics, prometheus
2
You need to show us more actuator related configuration, it seems like your endpoint is not properly configured.Manuel
@Manuel I've added all the info I haveFabry

2 Answers

0
votes

You could use Spring Boot's built-in prometheus endpoint. It exposes all of the Micrometer-based metrics. It's primarily intended for periodic fetching by a Prometheus server, but you could do similar in a script of your own that transforms the data to the necessary format and forwards it to your custom monitoring API.

0
votes

At the level of idea you did everything right, and Micrometer is indeed the way to go here.

At the level of pom.xml definitions, you don't need to set the version: If the version or micrometer/micrometer-prometheus module that your spring boot comes with clashes with your definitions it might not work properly.

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Other than that, if you work only with core-micrometer (without even prometheus integration) you have metrics endpoint that also shows all the metrics in the "common" format. Prometheus shows the metrics in a format that prometheus can scrape. For your use case it seems like both ways can work but you'll have to be familiar with the formats in either way you chose.

Another thought: you say that the application can't forward to monitoring system, is it because some security constraints or because you use some kind of custom solution or something that micrometer can't integrate with? If its not a security thing, you can consider to write your own micrometer registry that will act as a bridge between micrometer and the monitoring system.

One more thing I would like to answer on: you say that the endpoint doesn't finish.

I can only speculate, because basically there can be many reasons (one of which is clash of versions that I've referred above) - but all are bugs, it should not work like that.

Another reason reason is that you might have defined a Gauge in micrometer that in order to be calculated performs some "expensive" calculations: goes to the database that for some reason takes ages to access, calls some expensive http endpoint, etc.

When the rest endpoint gets invoked, it takes "snapshots" (the current values) of all the metering primitives, for gauges it actually invokes your custom code that can do anything, so I would have checked that.

Yet another reason might be connectivity / security configurations (the request doesn't reach actuator at all) but given the information you've presented I can't tell more than that...