Raxol.Core.Metrics.MetricsCollector (Raxol v2.6.0)

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

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.

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.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear_metrics()

@spec clear_metrics() :: :ok

Clears all metrics.

clear_metrics(collector)

Clears all metrics (with optional parameter for compatibility).

get_all_metrics()

@spec get_all_metrics() :: map()

Gets all metrics grouped by type.

Examples

MetricsCollector.get_all_metrics()
# => %{
#   performance: %{request_time: [...], parse_time: [...]},
#   resource: %{memory_mb: [...]}
# }

get_metric(name, type, opts \\ [])

@spec get_metric(atom(), atom(), keyword()) :: [map()]

Gets metric entries for a name and type.

Examples

MetricsCollector.get_metric(:request_time, :performance)
# => [%{value: 45.2, timestamp: ~U[...], tags: []}, ...]

get_metric_stats(name, type)

@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.)

get_metrics()

Alias for get_all_metrics/0.

get_metrics(metric_name, tags)

@spec get_metrics(String.t() | atom(), map()) :: {:ok, [map()]}

Gets metrics for a specific metric name and tags.

get_metrics_by_type(type)

@spec get_metrics_by_type(atom()) :: map()

Gets metrics by type.

record_custom(name, value)

@spec record_custom(String.t() | atom(), number()) :: :ok

Records a custom metric.

record_metric(name, type, value, opts \\ [])

@spec record_metric(atom(), atom(), number() | map(), keyword()) :: :ok

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])

record_operation(name, value)

@spec record_operation(atom(), number()) :: :ok

Records an operation metric.

record_performance(name, value)

@spec record_performance(atom(), number()) :: :ok

Records a performance metric.

record_performance(name, value, opts)

@spec record_performance(atom(), number(), keyword()) :: :ok

record_resource(name, value)

@spec record_resource(atom(), number() | map()) :: :ok

Records a resource metric.

record_resource(name, value, opts)

@spec record_resource(atom(), number() | map(), keyword()) :: :ok

start_link(opts \\ [])

stop(pid \\ __MODULE__)

Stops the metrics collector.