Timed annotation
Spring metrics
Performance monitoring
Application metrics
Spring framework

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.

java
1import io.micrometer.core.annotation.Timed;
2import org.springframework.stereotype.Service;
3
4@Service
5public class OrderService {
6
7    @Timed(value = "orders.process", description = "Time spent processing orders")
8    public void processOrder(String orderId) throws InterruptedException {
9        Thread.sleep(150);
10    }
11}

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.

java
1import io.micrometer.core.aop.TimedAspect;
2import io.micrometer.core.instrument.MeterRegistry;
3import org.springframework.context.annotation.Bean;
4import org.springframework.context.annotation.Configuration;
5
6@Configuration
7public class MetricsConfig {
8
9    @Bean
10    public TimedAspect timedAspect(MeterRegistry meterRegistry) {
11        return new TimedAspect(meterRegistry);
12    }
13}

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.

java
1import io.micrometer.core.annotation.Timed;
2import org.springframework.stereotype.Service;
3
4@Service
5public class ReportService {
6
7    @Timed(value = "reports.generate", longTask = true)
8    public void generateLargeReport() throws InterruptedException {
9        Thread.sleep(5_000);
10    }
11}

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

  • '@Timed records method duration as a Micrometer timer metric.'
  • Use a TimedAspect or another supported integration so Spring can intercept annotated methods.
  • Give timers stable names and avoid high-cardinality metric dimensions.
  • Use longTask = true for 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.

Course illustration
Course illustration

All Rights Reserved.