IO::MetricsGuidesGetting Started

Getting Started

This guide explains how to use io-metrics to capture listener queue statistics from the host operating system.

Installation

Add the gem to your project:

$ bundle add io-metrics

Capturing Listener Statistics

To capture socket listener statistics, use IO::Metrics::Listener.capture. On unsupported platforms, supported? returns false, so it's good practice to check first:

require "io/metrics"

if IO::Metrics::Listener.supported?
	# Capture stats for all listening sockets
	listeners = IO::Metrics::Listener.capture
	
	listeners.each do |address, listener|
		puts "#{address}: queued=#{listener.queued_count}, active=#{listener.active_count}, close_wait=#{listener.close_wait_count}"
	end
end

Filtering by Address

You can limit captures to specific TCP addresses:

require "io/metrics"

listeners = IO::Metrics::Listener.capture(addresses: ["0.0.0.0:80", "127.0.0.1:8080"])

Capturing Unix Domain Sockets

Unix domain socket paths can be captured alongside or instead of TCP addresses:

require "io/metrics"

# Only Unix sockets
listeners = IO::Metrics::Listener.capture(paths: ["/tmp/socket.sock"])

# Both TCP and Unix sockets together
listeners = IO::Metrics::Listener.capture(addresses: ["0.0.0.0:80"], paths: ["/tmp/socket.sock"])

Metrics

Each IO::Metrics::Listener value provides:

  • queued_count: Number of connections currently waiting to be accepted (in the accept queue).
  • active_count: Number of accepted connections in ESTABLISHED state.
  • close_wait_count: Number of accepted connections in CLOSE_WAIT state (peer has closed; application still processing).