Search documentation of snmpkit

# SnmpKit.SnmpMgr.Metrics (snmpkit v1.2.0)

Comprehensive metrics collection and monitoring for SNMP operations.

This module provides real-time metrics collection, aggregation, and reporting for all SNMP operations including request latency, success rates, throughput, and resource utilization across engines, pools, and circuit breakers.

# Summary

## Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

counter(metrics, metric_name, value \\ 1, tags \\ %{})

Records a counter metric.

gauge(metrics, metric_name, value, tags \\ %{})

Records a gauge metric (current value).

get_metrics(metrics)

Gets current metrics snapshot.

get_summary(metrics)

Gets aggregated metrics summary.

get_window_metrics(metrics, window_start, window_end)

Gets metrics for a specific time window.

histogram(metrics, metric_name, value, tags \\ %{})

Records a histogram metric (for latency/duration measurements).

reset(metrics)

Resets all metrics.

start_link(opts \\ [])

Starts the metrics collector.

subscribe(metrics, subscriber_pid)

Subscribes to metrics updates.

time(metrics, metric_name, fun, tags \\ %{})

Records timing for a function execution.

unsubscribe(metrics, subscriber_pid)

Unsubscribes from metrics updates.

# Functions

# child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# counter(metrics, metric_name, value \\ 1, tags \\ %{})

Records a counter metric.

## Parameters

- `metrics` - Metrics PID or name
- `metric_name` - Name of the metric
- `value` - Value to add (default: 1)
- `tags` - Optional tags for the metric

## Examples

    SnmpKit.SnmpMgr.Metrics.counter(metrics, :requests_total, 1, %{target: "device1"})

# gauge(metrics, metric_name, value, tags \\ %{})

Records a gauge metric (current value).

## Parameters

- `metrics` - Metrics PID or name
- `metric_name` - Name of the metric
- `value` - Current value
- `tags` - Optional tags for the metric

## Examples

    SnmpKit.SnmpMgr.Metrics.gauge(metrics, :active_connections, 15, %{pool: "main"})

# get_metrics(metrics)

Gets current metrics snapshot.

# get_summary(metrics)

Gets aggregated metrics summary.

# get_window_metrics(metrics, window_start, window_end)

Gets metrics for a specific time window.

# histogram(metrics, metric_name, value, tags \\ %{})

Records a histogram metric (for latency/duration measurements).

## Parameters

- `metrics` - Metrics PID or name
- `metric_name` - Name of the metric
- `value` - Value to record
- `tags` - Optional tags for the metric

## Examples

    SnmpKit.SnmpMgr.Metrics.histogram(metrics, :request_duration_ms, 150, %{operation: "get"})

# reset(metrics)

Resets all metrics.

# start_link(opts \\ [])

Starts the metrics collector.

## Options

- `:window_size` - Metrics window size in seconds (default: 60)
- `:retention_period` - How long to keep metrics in seconds (default: 3600)
- `:collection_interval` - Collection frequency in ms (default: 1000)

## Examples

    {:ok, metrics} = SnmpKit.SnmpMgr.Metrics.start_link(
      window_size: 120,
      retention_period: 7200
    )

# subscribe(metrics, subscriber_pid)

Subscribes to metrics updates.

# time(metrics, metric_name, fun, tags \\ %{})

Records timing for a function execution.

## Parameters

- `metrics` - Metrics PID or name
- `metric_name` - Name of the metric
- `fun` - Function to time
- `tags` - Optional tags for the metric

## Examples

    result = SnmpKit.SnmpMgr.Metrics.time(metrics, :snmp_get_duration, fn ->
      SnmpKit.SnmpMgr.get("device1", "sysDescr.0")
    end, %{device: "device1"})

# unsubscribe(metrics, subscriber_pid)

Unsubscribes from metrics updates.