Add dependency

Add the following dependency to the project.

<dependency>
  <groupId>io.avaje.metrics</groupId>
  <artifactId>metrics</artifactId>
  <version>8.3</version>
</dependency>

Maven tile

When using maven add the following maven tile into the build / plugins section. This plugin performs build time enhancement of @Timed methods.

<plugin>
  <groupId>io.repaint.maven</groupId>
  <artifactId>tiles-maven-plugin</artifactId>
  <version>2.16</version>
  <extensions>true</extensions>
  <configuration>
    <tiles>
      <tile>io.avaje.tile:metrics-enhance:8.3</tile>
      <!-- other tiles ... -->
    </tiles>
  </configuration>
</plugin>

Gradle plugin

When using Gradle add the following plugin (TODO).

Register JVM metrics

Via MetricManager.jvmMetrics() specify if we want:

  • Standard JVM metrics (GC, Threads, Memory)
  • CGroup metrics
  • Logback or Log4J counters for errors and warnings

Below is typical configuration code that additionally specifies a reporter to report metrics to a local CSV file every 10 seconds.

@Factory
class Configuration {

  @Bean
  MetricReportManager metrics() {

    MetricManager.jvmMetrics()
      .registerJvmMetrics()
      .registerCGroupMetrics()
      .registerLogbackMetrics();

    // simple csv reporter
    MetricReportConfig config = new MetricReportConfig();
    config.setFreqInSeconds(10);
    return new MetricReportManager(config);
  }
}

Add @Timed

Add @Timed to classes that we want methods to be timed. By default all public methods will have timing added to them.

@Timed
@Singleton
public class IngestQueueConsumer {
  ...
}
  • Add @Timed on additional private methods that we want timing on
  • Add @NotTimed on public methods that we do not want timing on

Note that DInject controllers automatically have timing added.