Skip to main content
Version: 1.7

Metrics

Pluggable metrics collection interface for observability. Implement the MetricsCollector interface or use the built-in debug implementation.

import "github.com/KARTIKrocks/wshub"
  • Pluggable MetricsCollector interface
  • Built-in DebugMetrics implementation for development
  • Track connections, messages, latency, errors, and room events
  • Official Prometheus subpackage with a drop-in MetricsCollector

Exact signatures live on pkg.go.dev.

Metrics Interface

type MetricsCollector interface {
IncrementConnections()
DecrementConnections()
IncrementMessagesReceived() // 1.5+, was IncrementMessages
IncrementMessagesSent(count int) // 1.5+
IncrementMessagesDropped() // 1.5+
RecordMessageSize(size int)
RecordLatency(duration time.Duration)
RecordBroadcastDuration(duration time.Duration) // 1.5+
IncrementErrors(errorType string)
IncrementRoomJoins()
IncrementRoomLeaves()
IncrementRooms() // 1.5+
DecrementRooms() // 1.5+
}

// Use with hub
hub := wshub.NewHub(
wshub.WithMetrics(myCollector),
)

Debug Metrics

Built-in debug implementation for development and testing:

// Create debug metrics collector
metrics := wshub.NewDebugMetrics()

hub := wshub.NewHub(
wshub.WithMetrics(metrics),
)

// Get a point-in-time stats snapshot
stats := metrics.Stats()
fmt.Printf("Connections: %d\n", stats.Connections)
fmt.Printf("Received: %d\n", stats.TotalMessagesRecv) // renamed from TotalMessages
fmt.Printf("Sent: %d\n", stats.TotalMessagesSent)
fmt.Printf("Dropped: %d\n", stats.TotalDropped)
fmt.Printf("Active rooms: %d\n", stats.ActiveRooms)
fmt.Printf("Avg broadcast: %v\n", stats.AvgBroadcast)

// Pretty-print summary
fmt.Println(metrics)

Prometheus Subpackage

Official drop-in MetricsCollector backed by prometheus/client_golang. Import as a separate module:

go get github.com/KARTIKrocks/wshub/prometheus
OptionDescription
WithRegistry(reg)Use a custom Prometheus registry (default: prometheus.DefaultRegisterer)
WithNamespace(ns)Set the metric name prefix (default: wshub)
WithLatencyBuckets(buckets)Custom histogram buckets for message_latency_seconds
WithBroadcastBuckets(buckets)Custom histogram buckets for broadcast_duration_seconds

Exposed metrics:

MetricDescription
connections_activeGauge of currently connected clients
connections_totalCounter of all connections ever accepted
messages_received_totalCounter of inbound messages
messages_sent_totalCounter of outbound messages
messages_dropped_totalCounter of messages dropped due to full send buffers
message_received_bytes_totalCounter of inbound bytes
message_latency_secondsHistogram of message handler latency
broadcast_duration_secondsHistogram of local fanout duration
rooms_activeGauge of currently active rooms
room_joins_totalCounter of room join events
room_leaves_totalCounter of room leave events
errors_totalCounter of errors, labelled by type
import whprom "github.com/KARTIKrocks/wshub/prometheus"

// Default setup — registers on prometheus.DefaultRegisterer
collector := whprom.NewCollector()

// Custom registry and namespace
collector := whprom.NewCollector(
whprom.WithRegistry(myRegistry),
whprom.WithNamespace("myapp"),
)

hub := wshub.NewHub(
wshub.WithMetrics(collector),
)