Spring boot metrics for rest api with PathVariables - spring-boot

In my spring boot project I would like to keep count of how many times the rest api endpoint responded with status 200. The spring boot actuator metrics endpoint came close to solving this issue for me out of the box.
However, the /metrics endpoint names provided the aggregate of responses by the endpoint method rather than each of the dynamic endpoints created through #PathVariable.
For example:
while I can get http://localhost:8084/myproject/actuator/metrics/http.server.requests?tag=status:200,uri:/api/users/{id}/books
I would like to do something like
http://localhost:8084/myproject/actuator/metrics/http.server.requests?tag=status:200,uri:/api/users/1/books
and
http://localhost:8084/myproject/actuator/metrics/http.server.requests?tag=status:200,uri:/api/users/2/books
and so on.
Is there an easy way to do this?

You can roll your own WebMvcTagsProvider. That's the place where you can hook into the tag generation. Have a look at DefaultWebMvcTagsProvider to get insight on how it is done for the default behaviour.
A note: The default tagging is done on purpose the way it is to hinder metrics explosion, because every metric name + tag combination is a new metric. So be aware of that.

Related

high cardinality in promethues for same url

We are calling a 3rd party api /api/{code}/authvalue. Now each time we are calling this api we are getting micrometer is treating each call diffrenet as {code} which is different for each call.
we are using default micrometer of springboot of actuators.
Is there any way we can customize it?
/api/1234/authvalue
/api/4321/authvalue
/api/2324/authvalue
to treated as /api/xxxx/authvalue so that Prometheus can club all these metrics into one.
I supposed you are using a restTemplate to call the 3rd party api and you group on prometheus on the metric http.client.requests on the tag URI
It's mean that if you use
restTemplate.getForObject("/api/{code}/authvalue", ObjectReturn.class, Map.of("code","123456"));
The tag uri on your metric will be always "/api/{code}/authvalue" and you will be able to group it by "/api/{code}/authvalue".
Mostly the same for Webclient.
But anyway if you are using spring boot 3.0.X and you want to customise the metric you can still use ObservationRestTemplateCustomizer and ObservationWebClientCustomizer.
You can have a look at the documentation here

Custom Spring Actuator Endpoint which has subsystem and can be added dynamically

I'm looking for a way to implement custom endpoints for a reactive application using Spring Boot 2.2.
The endpoints have some subsystems and perform specific resource operations on the subsystems. The URL paths look like:
/actuator/system1/subsystem_a
/actuator/system1/subsystem_b
/actuator/system2/subsystem_c
Furthermore, system1 and system2 are not both always deployed, so I'd like to add dynamically the endpoints of the deployed system only.
I know I can use ReactiveHealthContributorRegistry to add custom health check endpoints dynamically. Is there a similar way for a fully custom endpoint?
Thanks in advance.
It seems there is no way to construct such complex endpoints like what I asked in Spring Boot Actuator.
I finally decided to use RouterFunction and HandlerFunction referring to the following websites.
https://www.baeldung.com/spring-5-functional-web
https://spring.io/blog/2016/09/22/new-in-spring-5-functional-web-framework

Spring Boot Actutor web metrics - disable (or group) http_server_requests_seconds_sum

I have a service which handles quite a big amount of REST requests (arount 500k per hour). Most of these calls are GET request with diffrent URLs. To grab metrics from the service I'm using Spring Boot Actuator (services are writen with spring boot 2.0 ) with micrometer and prometheus. So on path: /actuator/prometheus I have all metrics for prometheus.
Now after around 10 min my services stop serving metric, and it could be because there are a lot of different URLs which cause a lot of http_server_requests_seconds_sum metrics.
I want to disable this metric, or maybe group it for all endpoints.
You could disable http metrics
management.metrics.enable.http=false
I found settings to disable this metric: auto-time-requests
But to make it work with reactive endpoint I need also to upgrade Spring Boot to 2.1.0.
There is side effect i don't know any information how many RQ handle my service.
Maybe now i change the question - is there any way to group this metric ?

How to remove default Metrics data provided by Spring Actuator

I am trying to get Production metrics for my application. I am using Actuator on top of Spring Boot. Actuator exposes an endpoint for that "/metrics". I have Autowired CounterService class to get my own metrics data. I will be displaying the metrics data in one of our Application Management UI. The problem here is that I don't want all the default metrics data, as the Response JSON given back by the /metrics endpoint is kind off heavy for me to parse and most of the default metrics data are useless for me and give it to the UI. I have gone through the Spring Docs, I didn't get any help.
Appreciate your help on this!
Oh So I found a way to deal with the scenario. Actually Spring This GitHub Link pretty much solves my problem. Actuator Supports Reqex support to query for the data you need. So my url looks like this now : http://{{hostname}}/metrics/counter.*.count instead of this http://{{hostname}}/metrics/. Hope this helps.
Have you tried wrapping the response class from metrics, with a Custom class?

Spring MVC App Metrics Frameworks

I have a REST webservice written using Spring MVC 4.latest
The service will be deployed on AWS Opsworks.
I want to have graph showing metrics like: tp90, total requests per second/hour/day, detailed by request uri if possible.
Also if possible DB queries response time.
I would like this to be put in something like Cloudwatch or maybe Ganglia (?)
Is there a framework that does this already?
Any other suggestions on how to achieve this?

Resources