@Timed is a Micrometer annotation that records how long a method takes and how often it is called, publishing the data as a timer metric to your monitoring system. In a Spring Boot application it works only after you register a TimedAspect bean and have Spring AOP available, because the annotation alone does nothing.
Timers expose a count, a total time and a maximum, and they can also publish percentiles or histograms, which is what you need for latency dashboards and alerts.
The annotation is io.micrometer.core.annotation.Timed. When the aspect intercepts a call, it starts a Timer.Sample, runs the method, and records the duration in a timer named by the annotation. The timer reports:
count, the number of callssum (total time) and maxclass, method and exception, added automaticallyThe exception tag is none on success and the exception class name on failure, so the same timer lets you chart error rates per method.
Add the AOP starter, the Actuator, and the registry you use. For a quick local check the Actuator alone is enough.
Then register the aspect:
Without the TimedAspect bean, @Timed on a service method is silently ignored. This is the most common reason people see no metric.
Expose the metric in application.properties:
After a few calls, GET /actuator/metrics/reports.generate shows COUNT, TOTAL_TIME and MAX. With the Prometheus registry on the classpath, /actuator/prometheus prints reports_generate_seconds_count, _sum and _max.
| Attribute | Purpose |
|---|---|
value | Metric name. Required for a useful name; use dots, for example reports.generate |
description | Text shown in metric metadata |
extraTags | Extra tag pairs, written as key, value, key, value |
percentiles | Client-side percentiles, for example 0.95 |
histogram | Publishes percentile histogram buckets, useful for server-side percentiles |
longTask | Records a long task timer, which tracks tasks still running |
Prefer histograms over client-side percentiles when you run several instances. Percentiles computed in each JVM cannot be averaged correctly across instances, while histogram buckets can be aggregated by the monitoring backend.
The aspect uses Spring AOP proxies, so the usual proxy rules apply:
private and final methods cannot be intercepted by a proxy.new is not proxied.Mono or a Flux measure only the time to create the pipeline, not the time to complete it. Use the Micrometer Observation API or the metrics() operator for reactive code.For HTTP endpoints, Spring Boot already publishes http.server.requests, so you rarely need @Timed on controllers.
Micrometer 1.10 and later also provide the Observation API, with @Observed and an ObservedAspect. One observation can produce both a timer metric and a tracing span, so it suits applications that use distributed tracing. @Timed remains a simple and fully supported choice for metrics only.
You can also record manually when an annotation does not fit:
Most often the TimedAspect bean is not registered, the AOP starter is missing, or the call is an internal self-invocation. Also check that the metric name is not excluded by a meter filter.
@Timed records duration and count in a timer. @Counted only increments a counter and needs CountedAspect. A timer already contains a count, so use @Counted only when you do not need durations.
The cost per call is small compared with I/O-bound work, but avoid it on tight loops and very hot methods. Keep tag values low-cardinality, because a tag with unbounded values such as user IDs creates a new time series for each value.
Yes. At class level it applies to every public method of the class, and a method-level annotation overrides the class setting.
@Timed turns a method into a Micrometer timer, but only after you register TimedAspect and have AOP in place. Choose a clear metric name, keep tags low-cardinality, use histograms for percentiles across instances, and watch for self-invocation and reactive return types.