Class OtelMetrics

java.lang.Object
com.jsgalactic.axiom.otel.OtelMetrics
All Implemented Interfaces:
Metrics

public final class OtelMetrics extends Object implements Metrics
Maps the dependency-free Metrics SPI to OpenTelemetry API instruments. Counters become dimensionless LongCounters, gauges become LongUpDownCounters and timers become DoubleHistograms in seconds with the explicit bucket boundaries of DEFAULT_BUCKETS, which match the bounds of the in-memory registry in axiom-metrics.

Instruments come from the instrumentation scope "com.jsgalactic.axiom.otel". A lookup resolves and caches one instrument per name and tag set; recording methods are thread-safe and use the attributes captured at lookup. A name always has one kind and one set of tag keys: using it otherwise throws IllegalArgumentException, as the registry does. Tag values remain the caller's responsibility: keep them to route templates, methods and other fixed sets, because neither this adapter nor the OTel API bounds cardinality.

The adapter owns no SDK, reader or exporter. It is safe to share one instance across requests, and several adapters can write to the same MeterProvider.

  • Field Details

    • DEFAULT_BUCKETS

      public static final List<Double> DEFAULT_BUCKETS
      Histogram bounds in seconds: 1 ms, 5 ms, 10 ms, 25 ms, 50 ms, 100 ms, 250 ms, 500 ms, 1 s, 2.5 s, 5 s and 10 s. The last bucket is open-ended.
  • Method Details

    • create

      public static OtelMetrics create(io.opentelemetry.api.OpenTelemetry openTelemetry)
      Creates an adapter using the meter of Tracing.SCOPE_NAME from the supplied instance. The caller owns the SDK and any metric readers or exporters.
      Parameters:
      openTelemetry - meter source, or OpenTelemetry.noop() to discard measurements
      Returns:
      an empty adapter
    • create

      public static OtelMetrics create(io.opentelemetry.api.metrics.MeterProvider meterProvider)
      Creates an adapter using the meter of Tracing.SCOPE_NAME from the supplied provider.
      Parameters:
      meterProvider - provider that creates the instruments
      Returns:
      an empty adapter
    • counter

      public Metrics.Counter counter(String name, String... tags)
      Description copied from interface: Metrics
      Returns the monotonically increasing counter for a series, creating it if needed.
      Specified by:
      counter in interface Metrics
      Parameters:
      name - dotted lowercase metric name
      tags - alternating tag keys and values, possibly none
      Returns:
      a thread-safe counter; never null
    • gauge

      public Metrics.Gauge gauge(String name, String... tags)
      Description copied from interface: Metrics
      Returns the gauge for a series, creating it if needed. A gauge is a value that moves both ways, such as a queue depth. It is changed by deltas, so several components can share one series and the value stays the sum of their contributions.
      Specified by:
      gauge in interface Metrics
      Parameters:
      name - dotted lowercase metric name
      tags - alternating tag keys and values, possibly none
      Returns:
      a thread-safe gauge; never null
    • timer

      public Metrics.Timer timer(String name, String... tags)
      Description copied from interface: Metrics
      Returns the timer for a series, creating it if needed. A timer records durations and keeps their distribution, so latency percentiles can be derived from it.
      Specified by:
      timer in interface Metrics
      Parameters:
      name - dotted lowercase metric name
      tags - alternating tag keys and values, possibly none
      Returns:
      a thread-safe timer; never null