title: Codahale Metrics: using @Timed metrics annotation in plain Java
body: <p>I am trying to add metrics to a plain Java application using codahale metrics. I'd like to use the @Timed annotation, but it is unclear to me which MetricRegistry it uses, or how to tell it which MetricRegistry to use. The application is a plain Java 8 application, built with Maven 3, no Spring, no Hibernate.</p>
  
  <p>I can not find any documentation on how to implement @Timed in the dropwizard documentation: <a href="https://dropwizard.github.io/metrics/3.1.0/manual/">https://dropwizard.github.io/metrics/3.1.0/manual/</a></p>
  
  <p>I've added these dependencies:</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;io.dropwizard.metrics&lt;/groupId&gt;
    &lt;artifactId&gt;metrics-core&lt;/artifactId&gt;
    &lt;version&gt;3.1.0&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;com.codahale.metrics&lt;/groupId&gt;
    &lt;artifactId&gt;metrics-annotation&lt;/artifactId&gt;
    &lt;version&gt;3.0.2&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>When I use a programatic call to Timer, I can get reports because I know which MetricsRegistry is used:</p>
  
  <pre><code>static final MetricRegistry metrics = new MetricRegistry();
  private void update() throws SQLException {
    Timer.Context time = metrics.timer("domainobject.update").time();
    try {
      [...]
    } finally {
      time.stop();
    }
  }
  </code></pre>
  
  <p>But when I use the much more elegant @Timed annotation, I have no idea which registry is used, and therefore I can not create a reporter, which means I can not get the metrics reported (I'm not even sure if this actually does anything):</p>
  
  <pre><code>@Timed(name = "domainobject.update")
  private void update() throws SQLException {
      [...]
  }
  </code></pre>
  
  <p>Please advise on how to make the @Timed and other Metrics annotations work in a regular Java application.</p>
  
  <p><strong>Additional info:</strong> The reason I am finding this strange is that I have added the Lombok framework and the @Slf4j annotations do work. I added Lombok as a dependency in the maven pom.xml:</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
    &lt;artifactId&gt;lombok&lt;/artifactId&gt;
    &lt;version&gt;1.14.8&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>And I can use the @Sl4fj class annotation to add a logger to the class without cluttering up the member variables:</p>
  
  <pre><code>@Slf4j
  public class App {
    public void logsome(){
      log.info("Hello there");
    }
  }
  </code></pre>
  
  <p>So if that's possible by just adding a dependency, I reckon I am just missing a dependency or configuration to get the codahale @Timed annotation work, as described above.</p>
  
  <p>(by the way, check out Lombok, it will make your life easier: <a href="http://projectlombok.org/">http://projectlombok.org/</a> )</p>