spring actuator increases the metric counter.status.200.{my endpoint} even if the endpoint returns a 500 - spring

I've developed my first rest service with Spring Boot, Spring MVC Spring Actuator using embedded tomcat 8.
For some reason, when the endpoint fails due to an exception which is not caught, the returned response from the endpoint has the status 500, but the metric of counter.status.200. was increased.
I debug a bit the code and looks like the status of the response in the class ResponseFacade (from tomcat) is set after the metric is increased on the MetricFilterAutoConfiguration.MetricsFilter.
Does someone has an idea how can I get the right status code counter (counter.status.500.{my endpoint}) increased?
Thanks in advance

Related

Webflux: CancelledServerWebExchangeException appears in metrics for seemingly no reason

After upgrading to spring-boot 2.5, CancelledServerWebExchangeException started to appear in prometheus http_server_requests_seconds metrics quite frequently (up to 10% server responses end up with it, according to graphics). It appears in my own API metrics, as well as actuator endpoints metrics (health, info, prometheus).
Example:
http_server_requests_seconds_count{exception="CancelledServerWebExchangeException",method="GET",outcome="UNKNOWN",status="200",uri="/actuator/health"} 137.0
Kind of strange combination of outcome="UNKNOWN" & status="200"
The problem is: all these requests have successful responses.
Questions: what is this exception for and why may it occur so often?
How to reproduce: start application locally and put some load on it (I used 50 threads in jmeter accessing actuator endpoints)

Quarkus Microprofile Rest Client, how to handle errors differently

I have 2 Quarkus services, one acts as an Edge-service and the other one as a Downstream service, the communication is done through Quarkus MicroProfile Rest-API client. Now Downstream service may return some kind of Bad request error (4xx) state with Json body. my problem now is Edge service is throwing 500 error with WebApplicationException because of that, is there a way to populate same response code and body from Downstream Service to Edge Service ?
This exception is thrown by the default rest client ResponseExceptionMapper.
You should be able to disable it by adding the following property to application.properties:
microprofile.rest.client.disable.default.mapper=true
See the Default ResponxeExceptionMapper and ResponseExceptionMapper sections of the MicroProfile Rest Client specification.

Spring Actuator Metrics generate logs

I'm trying to get micrometer metrics data to Splunk. Each metric endpoint gives the current value of a metric, so I would need Splunk to send a http request to my application periodically, or I can write the metric values to a log file periodically.
So how do I get my application to write the metric values to logs?
If you are in spring boot 2.x and Micrometer is of version 1.1.0+ you can create a bean of
periodic (1 minute) special logging registry see (https://github.com/micrometer-metrics/micrometer/issues/605)
#Bean
LoggingMeterRegistry loggingMeterRegistry() {
return new LoggingMeterRegistry();
}
This is by far the easiest way to log everything via logging system.
Another alternative is creating a scheduled job that will run some method on a bean with injected metering registry that will iterate over all the metrics (with possibly filtering out the metrics that you won't need) and preparing the log of your format.
If you think about this, this is exactly what the metrics endpoint of spring boot actuator does, except returning the data via http instead of writing to log.
Here is an up-to-date implementation of the relevant endpoint from the spring boot actuator source

SpringBoot application giving RestClientException (Unknown status code [494] null) 2 days after deploying to EC2 instance

Our application is a spring batch application running on EC2.
We have a RestTemplate call to an external application and it works fine.
But after 2 days, it started to fail.
we checked with the external application and logs shows the rest service response was successful from their end.
But our application log shows the below exception.
We tried redeploying our application and this behaviour still persists(works for 2 days and fails after that).
We are using spring boot 1.5.9 version.
2019-07-04 19:50:38.078 [10.174.81.249:SimpleAsyncTaskExecutor-2380]
Exception when getting data : org.springframework.web.client.UnknownHttpStatusCodeException: Unknown status code [494] null
at org.springframework.web.client.DefaultResponseErrorHandler.getHttpStatusCode(DefaultResponseErrorHandler.java:88)
at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:48)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:688)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
I found status code 494 Request header too large is used by nginx and spring has not covered this status as it is not from the standard.
Please look enum org.springframework.http.HttpStatus
Also org.springframework.web.client.DefaultResponseErrorHandler
Solution: You have to check your configuration to verify which header is too large.

Check MongoDB Status with Spring Boot

How do I check, if the connection to the MongoDB is active (without using the actuator project) if just a MongoRepository is used, which hides the connection?
If mongodb connection is inactive then spring boot application will throw errors which can be checked in logs.
Ideally actuator project is best way to check the status.
You can also check the status by creating your own controller method lets say ping and in implementation write some operation like MongoRepository.findAll() if it returns positive value it is in active state.

Resources