Vaultx.Cache.Metrics (Vaultx v0.7.0)

View Source

Cache metrics collection and reporting system.

This module tracks comprehensive cache performance metrics across all cache layers, providing insights into hit ratios, response times, memory usage, and operational efficiency.

Metrics Collected

Hit/Miss Metrics

  • Cache hits per layer (L1, L2, L3)
  • Cache misses per layer
  • Overall hit ratio
  • Hit ratio trends over time

Performance Metrics

  • Average response time per operation
  • P95/P99 response times
  • Throughput (operations per second)
  • Concurrent operation counts

Resource Metrics

  • Memory usage per layer
  • Storage usage (L3)
  • Network usage (L2)
  • CPU usage for cache operations

Operational Metrics

  • Cache warming statistics
  • Eviction counts and reasons
  • Error rates and types
  • Cleanup operation metrics

Usage Examples

# Record cache operations
Vaultx.Cache.Metrics.record_hit(:l1, "secret/myapp/config")
Vaultx.Cache.Metrics.record_miss("secret/myapp/config")

# Get comprehensive statistics
{:ok, stats} = Vaultx.Cache.Metrics.get_stats()

# Get specific layer statistics
{:ok, l1_stats} = Vaultx.Cache.Metrics.get_layer_stats(:l1)

Integration with Telemetry

All metrics are automatically emitted as telemetry events for integration with monitoring systems like Prometheus, StatsD, or custom dashboards.

# Telemetry events emitted:
[:vaultx, :cache, :hit]
[:vaultx, :cache, :miss]
[:vaultx, :cache, :operation]
[:vaultx, :cache, :eviction]
[:vaultx, :cache, :cleanup]

Summary

Functions

Returns a specification to start this module under a supervisor.

Gets statistics for a specific cache layer.

Gets comprehensive cache statistics.

Records a cache eviction event.

Records a cache hit for the specified layer.

Records a cache miss.

Records a cache operation with timing.

Resets all metrics.

Starts the metrics collection system.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_layer_stats(layer)

Gets statistics for a specific cache layer.

get_stats()

Gets comprehensive cache statistics.

record_eviction(layer, key, reason)

Records a cache eviction event.

record_hit(layer, key)

Records a cache hit for the specified layer.

record_miss(key)

Records a cache miss.

record_operation(operation, key, duration)

Records a cache operation with timing.

reset_metrics()

Resets all metrics.

start_link(opts \\ [])

Starts the metrics collection system.