Xirsys.XTurn.Plugin.Metrics (xturn_plugins v0.1.1)

Copy Markdown View Source

Passive per-allocation QoS and metering exporter for TURN relay frames.

What problem this solves

Operators need usage and quality signals (bytes, packets, loss, jitter) per TURN allocation without rewriting media. This plugin counts every frame, tracks RTP SSRCs when payloads look like RTP, and emits Telemetry samples on a timer plus a final snapshot at allocation close.

Implements Xirsys.XTurn.Plugin in :passive mode (dedicated process; safe for Process.send_after/3).

Telemetry events:

  • [:xturn, :metrics, :allocation, :sample] - periodic flush
  • [:xturn, :metrics, :allocation, :stop] - final snapshot from handle_close/2

Options

  • :flush_ms - sample interval in milliseconds (default 5000)
  • :clock_rates - payload type to clock rate map (default %{0 => 8000, 8 => 8000})
  • :usage_log - optional path; one JSON line appended per allocation on stop
  • :match - allocation filter passed to attach?/2

RFCs

  • RFC 5766 (TURN data path)
  • RFC 3550 (RTP sequence tracking A.1/A.3, interarrival jitter pt.6.4.1)

Examples

iex> Xirsys.XTurn.Plugin.Metrics.mode()
:passive

iex> Xirsys.XTurn.Plugin.Metrics.hooks()
[:egress, :ingress]

iex> alloc = %Xirsys.XTurn.Plugin.Allocation{username: "alice"}
iex> Xirsys.XTurn.Plugin.Metrics.attach?(alloc, match: [username: "alice"])
true

Summary

Functions

Attach when :match filters are empty or all listed allocation fields match.

Emit the final Telemetry stop event and optional usage-log line.

Update byte/packet/SSRC stats for this frame; return new state.

Handle the periodic sample timer (:xturn_metrics_sample).

Meters both directions.

Start counters and schedule the first sample flush.

Always :passive (per-allocation instance process).

Functions

attach?(allocation, opts)

Attach when :match filters are empty or all listed allocation fields match.

handle_close(reason, state)

Emit the final Telemetry stop event and optional usage-log line.

handle_frame(payload, frame, state)

Update byte/packet/SSRC stats for this frame; return new state.

handle_info(atom, state)

Handle the periodic sample timer (:xturn_metrics_sample).

Emits [:xturn, :metrics, :allocation, :sample] Telemetry and reschedules the next flush after flush_ms.

hooks()

Meters both directions.

init(allocation, opts)

Start counters and schedule the first sample flush.

mode()

Always :passive (per-allocation instance process).