Raxol. Core. Metrics. MetricsCollector
(Raxol v2.9.2)
View Source
ETS-backed metrics collection for high-throughput metric recording.
Uses ETS tables for concurrent write access, avoiding GenServer mailbox serialization that bottlenecks write-heavy metric workloads.
Design
- Writes: Direct ETS inserts from any process (no serialization)
- Reads: Direct ETS lookups (no blocking)
- GenServer: Only for lifecycle management and periodic tasks
Usage
# Record metrics (can be called from any process)
MetricsCollector.record_metric(:request_time, :performance, 45.2)
MetricsCollector.record_performance(:parse_time, 3.3)
MetricsCollector.record_resource(:memory_mb, 128.5)
# Read metrics
MetricsCollector.get_metric(:request_time, :performance)
MetricsCollector.get_all_metrics()ETS Tables
raxol_metrics- Main metrics storage (ordered_set for time-ordered queries)raxol_metrics_meta- Metadata and aggregates
Both are owned by one unlinked process per node, started the first time any process records or reads, and kept for the life of the node. An ETS table is deleted when its owner exits, so neither the caller that happens to touch them first nor this GenServer owns them: a metric outlives the process that recorded it and a restart of the collector.
Summary
Functions
Returns a specification to start this module under a supervisor.
Clears all metrics.
Clears all metrics (with optional parameter for compatibility).
Gets all metrics grouped by type.
Gets metric entries for a name and type.
Gets metric statistics (count, latest value, etc.)
Alias for get_all_metrics/0.
Gets metrics for a specific metric name and tags.
Gets metrics by type.
Normalizes recorded tags to a map: keyword lists become maps and plain tag
lists such as [:api] become %{api: true}.
Records a custom metric.
Records a metric value. Can be called from any process.
Records an operation metric.
Records a performance metric.
Records a resource metric.
Stops the metrics collector.
Looks up a tag in a map from normalize_tags/1 by a string or atom key,
trying the other key form when the given one is absent. A string key is
converted with String.to_existing_atom/1, so a key no tag uses never
creates an atom. Returns nil when neither form is present.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec clear_metrics() :: :ok
Clears all metrics.
Clears all metrics (with optional parameter for compatibility).
@spec get_all_metrics() :: map()
Gets all metrics grouped by type.
Examples
MetricsCollector.get_all_metrics()
# => %{
# performance: %{request_time: [...], parse_time: [...]},
# resource: %{memory_mb: [...]}
# }
Gets metric entries for a name and type.
Examples
MetricsCollector.get_metric(:request_time, :performance)
# => [%{value: 45.2, timestamp: ~U[...], tags: []}, ...]
@spec get_metric_stats(atom(), atom()) :: %{ count: non_neg_integer(), latest: term(), min: number() | nil, max: number() | nil, avg: number() | nil }
Gets metric statistics (count, latest value, etc.)
Alias for get_all_metrics/0.
Gets metrics for a specific metric name and tags.
Gets metrics by type.
Normalizes recorded tags to a map: keyword lists become maps and plain tag
lists such as [:api] become %{api: true}.
Records a custom metric.
Records a metric value. Can be called from any process.
Parameters
name- Metric name (atom)type- Metric type (:performance, :resource, :operation, :custom)value- Metric value (number or map)opts- Options including:tags
Examples
MetricsCollector.record_metric(:request_time, :performance, 45.2)
MetricsCollector.record_metric(:cache_hits, :operation, 1, tags: [:api])
Records an operation metric.
Records a performance metric.
Records a resource metric.
Stops the metrics collector.
Looks up a tag in a map from normalize_tags/1 by a string or atom key,
trying the other key form when the given one is absent. A string key is
converted with String.to_existing_atom/1, so a key no tag uses never
creates an atom. Returns nil when neither form is present.