Class Metrics

java.lang.Object
com.tnsai.quality.observability.Metrics

public final class Metrics extends Object
Simple metrics collection for agent operations.

Provides counters, gauges, and histograms for tracking:

  • Action execution counts and durations
  • LLM call counts, tokens, and latencies
  • Message throughput
  • Error rates

Example usage:

 Metrics metrics = Metrics.getInstance();

 // Count events
 metrics.increment("agent.actions.total");
 metrics.increment("agent.actions.success");

 // Record values
 metrics.record("agent.action.duration_ms", 150);
 metrics.record("llm.tokens.prompt", 500);

 // Get current values
 long totalActions = metrics.getCount("agent.actions.total");
 double avgDuration = metrics.getAverage("agent.action.duration_ms");
 

Integration with monitoring systems:

 // Export all metrics
 Map<String, Number> snapshot = metrics.snapshot();

 // Clear metrics (e.g., after export)
 metrics.reset();
 
See Also:
  • Field Details

  • Method Details

    • getInstance

      public static Metrics getInstance()
      Gets the singleton metrics instance.
      Returns:
      the metrics instance
    • increment

      public void increment(String name)
      Increments a counter by 1.
      Parameters:
      name - the counter name
    • increment

      public void increment(String name, long delta)
      Increments a counter by a specified amount.
      Parameters:
      name - the counter name
      delta - the amount to add
    • getCount

      public long getCount(String name)
      Gets the current count for a counter.
      Parameters:
      name - the counter name
      Returns:
      the current count
    • gauge

      public void gauge(String name, long value)
      Sets a gauge to a specific value.
      Parameters:
      name - the gauge name
      value - the value
    • getGauge

      public long getGauge(String name)
      Gets the current gauge value.
      Parameters:
      name - the gauge name
      Returns:
      the current value
    • record

      public void record(String name, long value)
      Records a value in a histogram.
      Parameters:
      name - the histogram name
      value - the value to record
    • getAverage

      public double getAverage(String name)
      Gets the average value for a histogram.
      Parameters:
      name - the histogram name
      Returns:
      the average, or 0 if no values
    • getMin

      public long getMin(String name)
      Gets the minimum value for a histogram.
      Parameters:
      name - the histogram name
      Returns:
      the minimum, or 0 if no values
    • getMax

      public long getMax(String name)
      Gets the maximum value for a histogram.
      Parameters:
      name - the histogram name
      Returns:
      the maximum, or 0 if no values
    • getHistogramCount

      public long getHistogramCount(String name)
      Gets the count of values recorded in a histogram.
      Parameters:
      name - the histogram name
      Returns:
      the count
    • snapshot

      public Map<String,Number> snapshot()
      Creates a snapshot of all metrics.
      Returns:
      map of metric names to values
    • reset

      public void reset()
      Resets all metrics to zero.