Timed annotation in spring metrics
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The @Timed annotation is a convenient way to record method duration as a Micrometer Timer in a Spring application. It is useful when you want lightweight latency metrics on service methods, repositories, or other targeted code paths without manually starting and stopping timers in every method body.
Understand What @Timed Records
A timer records how often a method runs and how long those executions take. In Micrometer terms, that usually means count plus total time, and your monitoring backend can often derive additional views such as averages, rates, or percentile-like summaries depending on configuration.
In Spring applications, the annotation commonly comes from io.micrometer.core.annotation.Timed.
This example records timing data every time processOrder runs.
Wire @Timed into a Spring Application
If you want annotation-based timing on regular Spring beans, register Micrometer's TimedAspect and ensure a MeterRegistry is available.
With Spring Boot, this usually goes together with Actuator and a metrics backend such as Prometheus. Once the registry and aspect are present, Spring can proxy the method and measure execution time around it.
Use Long Task Timing When the Method Really Is Long
Normal timers are good for typical request or service latency. If a method may run for a long time and you want to observe currently running operations, use longTask = true.
Long task timers are tracked separately because they answer a different operational question: not just how long calls took after completion, but how many long-running calls are active right now and how long they have been running.
Choose Metric Names Deliberately
Timer names should be stable, descriptive, and low-cardinality. Good names describe the operation, not the runtime data flowing through it. For example, orders.process is reasonable, while embedding user IDs or order IDs in metric names would be a mistake.
If you need extra context, prefer tags configured in a controlled way rather than dynamically generating large numbers of unique metric series.
Common Pitfalls
The most common mistake is assuming @Timed works by magic on every method without any supporting configuration. On regular Spring beans, you usually need AOP-based interception such as TimedAspect, or another framework integration that explicitly supports the annotation.
Another problem is putting @Timed on private methods or self-invoked methods and expecting Spring proxies to intercept them. Proxy-based AOP typically only wraps calls that go through the Spring-managed proxy, not internal method calls within the same instance.
Developers also sometimes annotate far too many methods. Metrics are useful when they answer a real operational question. Blanket timing across every tiny helper method adds noise and makes dashboards harder to interpret.
Finally, use longTask = true only when the semantics fit. A long task timer is not a generic upgrade over a regular timer; it is a different kind of measurement for different operational behavior.
Summary
- '
@Timedrecords method duration as a Micrometer timer metric.' - Use a
TimedAspector another supported integration so Spring can intercept annotated methods. - Give timers stable names and avoid high-cardinality metric dimensions.
- Use
longTask = truefor genuinely long-running work that you want to observe while it is still active. - Be aware of proxy limitations such as private methods and self-invocation.

