LlmGuard.Telemetry.Metrics (LlmGuard v0.3.1)
View SourceEnhanced 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 processedllm_guard.detections.total- Total detections by categoryllm_guard.errors.total- Total errors by typellm_guard.cache.hits- Cache hits by typellm_guard.cache.misses- Cache misses by type
Distribution Metrics
llm_guard.request.duration- Request duration histogramllm_guard.detector.duration- Per-detector durationllm_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]
]
)
]
endPrometheus 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
@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
@spec metrics() :: [Telemetry.Metrics.t()]
Returns Telemetry.Metrics definitions for integration.
Returns
List of Telemetry.Metrics metric definitions.
@spec prometheus_metrics() :: String.t()
Exports metrics in Prometheus text format.
Returns
String containing Prometheus-format metrics suitable for scraping.
@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
@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