LlmGuard.Telemetry.Metrics (LlmGuard v0.3.1)

View Source

Enhanced telemetry and metrics collection for LlmGuard.

Provides comprehensive metrics tracking including:

  • Detection latency percentiles (P50, P95, P99)
  • Detection outcome rates (safe/detected/error)
  • Detector-specific performance metrics
  • Cache hit rates and efficiency
  • Error categorization and tracking

Metrics Emitted

Counter Metrics

  • llm_guard.requests.total - Total requests processed
  • llm_guard.detections.total - Total detections by category
  • llm_guard.errors.total - Total errors by type
  • llm_guard.cache.hits - Cache hits by type
  • llm_guard.cache.misses - Cache misses by type

Distribution Metrics

  • llm_guard.request.duration - Request duration histogram
  • llm_guard.detector.duration - Per-detector duration
  • llm_guard.detection.confidence - Confidence score distribution

Usage

# Setup telemetry handlers
LlmGuard.Telemetry.Metrics.setup()

# Metrics are automatically emitted during detection

# Get current metrics snapshot
metrics = LlmGuard.Telemetry.Metrics.snapshot()

# Export Prometheus-format metrics
prometheus = LlmGuard.Telemetry.Metrics.prometheus_metrics()

Integration

Telemetry.Metrics

import Telemetry.Metrics

defp metrics do
  [
    # Counter metrics
    counter("llm_guard.requests.total"),

    # Distribution metrics with percentiles
    distribution("llm_guard.request.duration",
      unit: {:native, :millisecond},
      reporter_options: [
        buckets: [10, 50, 100, 500, 1000, 5000]
      ]
    )
  ]
end

Prometheus Integration

# In your supervision tree
children = [
  {TelemetryMetricsPrometheus, metrics: LlmGuard.Telemetry.Metrics.metrics()}
]

Summary

Functions

Returns Telemetry.Metrics definitions for integration.

Exports metrics in Prometheus text format.

Sets up telemetry event handlers for LlmGuard metrics.

Returns a snapshot of current metrics.

Types

snapshot()

@type snapshot() :: %{
  requests: %{
    total: non_neg_integer(),
    safe: non_neg_integer(),
    detected: non_neg_integer(),
    error: non_neg_integer()
  },
  detections: %{by_category: map(), total: non_neg_integer()},
  errors: %{total: non_neg_integer()},
  cache: %{
    hits: non_neg_integer(),
    misses: non_neg_integer(),
    hit_rate: float()
  },
  latency: %{p50: number(), p95: number(), p99: number(), mean: float()}
}

Functions

metrics()

@spec metrics() :: [Telemetry.Metrics.t()]

Returns Telemetry.Metrics definitions for integration.

Returns

List of Telemetry.Metrics metric definitions.

prometheus_metrics()

@spec prometheus_metrics() :: String.t()

Exports metrics in Prometheus text format.

Returns

String containing Prometheus-format metrics suitable for scraping.

setup()

@spec setup() :: :ok

Sets up telemetry event handlers for LlmGuard metrics.

Attaches handlers to all LlmGuard telemetry events and initializes the metrics storage table.

Returns

:ok

snapshot()

@spec snapshot() :: snapshot()

Returns a snapshot of current metrics.

Returns

Map containing all current metrics:

  • :requests - Request metrics
  • :detections - Detection metrics
  • :errors - Error metrics
  • :cache - Cache metrics
  • :latency - Latency percentiles