Skip to content

Latest commit

 

History

History
115 lines (84 loc) · 2.58 KB

File metadata and controls

115 lines (84 loc) · 2.58 KB

avaje-metrics

Core metrics library providing timers, counters, meters, gauges, built-in JVM metrics, and the default MetricRegistry.

Maven dependency

<dependency>
  <groupId>io.avaje</groupId>
  <artifactId>avaje-metrics</artifactId>
  <version>${version}</version>
</dependency>

If the application uses module-info.java, also add:

requires io.avaje.metrics;

Basic usage

import io.avaje.metrics.Metrics;
import io.avaje.metrics.Tags;

var requests = Metrics.counterBuilder("app.http.requests")
  .unit("{event}")
  .build();

var timer = Metrics.timerBuilder("app.service.run")
  .tags(Tags.of("operation:sync"))
  .build();

var bytesSent = Metrics.meterBuilder("app.bytes.sent")
  .unit("By")
  .build();

Metrics.gauge("app.queue.depth")
  .ofLongs(queue::size);

requests.inc();
timer.time(service::run);
bytesSent.addEvent(4_096);

Default vs custom registry

Most applications use the default registry via Metrics:

var registry = Metrics.registry();

Create a separate registry only when you want isolation:

var registry = Metrics.createRegistry();
var timer = registry.timerBuilder("app.db.query").build();

Built-in JVM metrics

Metrics.jvmMetrics()
  .registerJvmCoreMetrics();

Use registerJvmMetrics() for the fuller built-in set.

Method timing

Programmatic timing:

var timer = Metrics.timer("app.service.run");
timer.time(service::run);

Traced timers:

var timer = Metrics.timerBuilder("app.service.run")
  .buildTraced();

Use buildRootTraced() for a top-level boundary that should create a root span when no recording span is current.

@Timed is the declarative path when build-time enhancement is enabled in the application. See Configure metrics enhancement for metrics-maven-plugin and metrics.mf setup. Custom tags use the same key:value format as Tags.of(...); class-level tags apply to each timed method and method-level tags append to them.

@Timed(tags = "component:billing")
class BillingService {

  @Timed(tags = "operation:sync")
  void syncInvoices() {
  }
}

Next steps