question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception when using @Timed annotation on a @Scheduled method with PrometheusMeterRegistry and TimedAspect

See original GitHub issue

There is a TimedAspect bean defined:

@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
	return new TimedAspect(registry);
}

And the method being timed looks like:

@Scheduled(cron = "0 0 0/1 * * *")
@Timed(value = "timer.doSomething", percentiles = { 0.5, 0.75, 0.95, 0,98, 0.99 })
public void doSomething() {
       // do something 
}

When the doSomething method finished execution, the exception is:

ERROR o.s.s.s.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
java.lang.IllegalArgumentException: Prometheus requires that all meters with the same name have the same set of tag keys. There is already an existing meter containing tag keys [app]. The meter you are attempting to register has keys [app, class, method].
        at io.micrometer.prometheus.PrometheusMeterRegistry.lambda$collectorByName$9(PrometheusMeterRegistry.java:361)
        at java.util.concurrent.ConcurrentHashMap.compute(ConcurrentHashMap.java:1877)
        at io.micrometer.prometheus.PrometheusMeterRegistry.collectorByName(PrometheusMeterRegistry.java:348)
        at io.micrometer.prometheus.PrometheusMeterRegistry.newTimer(PrometheusMeterRegistry.java:160)
        at io.micrometer.core.instrument.MeterRegistry.lambda$timer$2(MeterRegistry.java:257)
        at io.micrometer.core.instrument.MeterRegistry.getOrCreateMeter(MeterRegistry.java:566)
        at io.micrometer.core.instrument.MeterRegistry.registerMeterIfNecessary(MeterRegistry.java:528)
        at io.micrometer.core.instrument.MeterRegistry.timer(MeterRegistry.java:255)
        at io.micrometer.core.instrument.Timer$Builder.register(Timer.java:447)
        at io.micrometer.core.aop.TimedAspect.timedMethod(TimedAspect.java:78)

app is the name of the default tag that is setup for all metrics. The exception seems to be because the ScheduledMethodMetrics aspect and TimedAspect are both trying to time the same method. ScheduledMethodMetrics creates timer timer.doSomething with only the app tag and then TimedAspect tries to create a timer with the same name but with additional class and method tags.

The timer created by ScheduledMethodMetrics was present in the /prometheus endpoint

timer_doSomething_seconds{app="sample-app",quantile="0.5",} 6.979321856
timer_doSomething_seconds{app="sample-app",quantile="0.75",} 6.979321856
timer_doSomething_seconds{app="sample-app",quantile="0.95",} 6.979321856
timer_doSomething_seconds{app="sample-app",quantile="0.98",} 6.979321856
timer_doSomething_seconds{app="sample-app",quantile="0.99",} 6.979321856
timer_doSomething_seconds_count{app="sample-app",} 2.0
timer_doSomething_seconds_sum{app="sample-app",} 15.689029194

Tested with versions 1.0.5 and 1.0.6 and spring boot version 1.5.15.RELEASE

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jkschneidercommented, Jan 18, 2019

@tooms4444 We can talk about this in more real time in Slack if you’d like, but I still think there is no problem in the Micrometer code itself. It appears that the method is being timed in two different ways with different sets of tags and Prometheus does not allow this. The second set of tags suggests that the method being timed is a rest endpoint (because status and uri are tags). This is instrumented by the framework. If you also add a @Timed annotation with different tags, Prometheus will fail.

0reactions
tooms4444commented, Jan 23, 2019

Thank you @jkschneider and everyone : I didn’t get that the issue was coming from intricated timers, as the message seemed to be related to a particular duplicate timer name

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timed not working despite registering TimedAspect explicitly ...
I use spring boot 2.2.6.RELEASE and this MetricConfig works for me @Configuration public class MetricConfig { @Bean ...
Read more >
Spring Metrics
In a Spring application, it is common for such long running processes to be implemented with @Scheduled . spring-metrics provides a special @Timed...
Read more >
io.micrometer.core.instrument.MeterRegistry Java Examples
The following examples show how to use io.micrometer.core.instrument. ... addAspect(new TimedAspect(failingRegistry)); TimedService service = pf.
Read more >
TimedAspect - micrometer-core 1.7.1 javadoc
AspectJ aspect for intercepting types or methods annotated with @Timed . The aspect supports programmatic customizations through constructor-injectable ...
Read more >
Defining custom metrics in a Spring Boot application using ...
Now, it is time to have a closer look at Micrometer and its' integration ... 's @Timed annotation after configuring the TimedAspect Aspect ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found