We're upgrading our app to Spring boot 3 and the new Observability API. In the old way we would just create a timedAspect bean and that would allow us to use #Timed annotations to automatically create metrics for those methods/classes.
The new spring ObservabilityAPI does not seem to provide an aspect oriented way of adding metrics to ObservationRegistry. Does spring boot or micrometer provide any alternative to the #Timed annotation for collecting metrics in this new observability system.
Related
I'm using Spring's WebClient in a non-reactive application to make requests to a third-party service that uses OAuth2 client credentials for its security.
I would like to have http.client.requests related metrics for the OAuth2 requests that are made, however (as best I can tell) this isn't happening because DefaultClientCredentialsTokenResponseClient creates its own instance of RestTemplate using the constructor, instead of leveraging an injected RestTemplateBuilder, which would handle the instrumentation automatically.
Is there a way to have these metrics surfaced by Micrometer?
Spring Boot provides a utility called actuator which gives several useful maintenance endpoints like
/beans , /metrics , /health , /trace , /info, /dump.
This insight is vital for monitoring. By default Spring Actuator functionality is accessible via Spring MVC.
For projects which are not using Spring, like any JAX RS implementation (ours is RESTEASY), Is it possible to somehow integrate these utilities or is there reference implementation that can be referred?
I'm trying to use the baggage propagation offered by Spring Cloud Sleuth. In the calls that use the Feign client I don't have any kind of problem, I can propagate custom fields, while while using JmsTemplate of Spring JMS I can't propagate the same fields.
My application is a Spring Boot application, version 2.1.5, with Spring Cloud version Greenwich.SR1.
In order to put the custom fields, I use
ExtraFieldPropagation.set("communicationId", "123456");
while, inside my application.properties, I put
spring.sleuth.baggage-keys= communicationId
I expect that the same functionality present for the Feign client, is also present for the producer JMS. Where am I wrong?
I have services built in spring boot 2.0.2. I`m using redis and solrj.
Now if i want to get metrics of redis and solr. It don`t show in
http://localhost:8080/actuator/metrics
Is there any way e.g making custom endpoint to get redis and solr metrics?
Any help would be appreciated..
Yes, it should be possible.
Spring boot 2's default metrics framework is Micrometer
When this framework is integrated into spring boot application (via starter as usual), any metrics reported to micrometer will immediately appear in spring boot actuator.
So the question is whether the redis/solrj integration expose Metrics or not.
If they are not, you'll have to understand what exactly you want to measure and plug in the integration.
Here is the example:
import io.micrometer.core.annotation;
#Timed("my-redis-calls")
public void doSomethingInMyCode() {
//call redis here
}
Of course, there is also a programmatic interface, using annotations in not mandatory
The code shown in Mark Bramnik's answer doesn't specifically give you timing for Redis client calls, it gives you timing for doSomethingInMyCode method, which can be different.
Micrometer support seems to be available in the Lettuce 6.1 release.
MeterRegistry meterRegistry = …;
MicrometerOptions options = MicrometerOptions.create();
ClientResources resources = ClientResources.builder().commandLatencyRecorder(new MicrometerCommandLatencyRecorder(meterRegistry, options)).build();
RedisClient client = RedisClient.create(resources);
https://lettuce.io/core/release/reference/index.html#command.latency.metrics.micrometer
I have followed the indication that adding the coda hale metrics library into the classpath would automatically autoconfigure the metrics.
That works, I get the injected metricRegistry bean.
However, how to I expose these new metrics in the /metrics endpoint?
Thanks!
There's some integration magic accomplished by http://www.ryantenney.com/metrics-spring/ that wires codahale metrics into the actuator /health endpoint.
With this dependency included,
compile 'com.ryantenney.metrics:metrics-spring:3.0.0-RC2'
you can "enableMetrics" in your application configuration
#EnableMetrics
public class ApplicationConfiguration {
...
This allows you to time each request with the #timed annotation:
#Timed
#RequestMapping(method=RequestMethod.GET)
public #ResponseBody
Foo foo() {
...
and to have your other interactions with MetricRegistry aggregate into the actuator /health endpoint.
I've put together an example application which implements this integration:
https://github.com/benschw/consul-cluster-puppet/tree/master/demo
and written a more in depth tutorial here:
http://txt.fliglio.com/2014/10/spring-boot-actuator/
That's not the way it's supposed to work. Codahale has some metric types that do not map to Spring Boot ones, so you are supposed to use the Codahale native APIs for reporting once you start collecting metrics that way. Boot is only providing the service abstraction for Gauge and Counter at that point.
UPDATE: Spring Boot also adds Codahale metrics to the /metrics endpoint since 1.2.0 (see MetricRegistryMetricReader).
I update a metrics histogram using spring-boot like this:
gauge.submit("histogram." + name + ".millis", durationMillis);