Real-Time Monitoring
SLOzy provides WebSocket-based real-time monitoring through internal/websocket/.
Architecture
The WebSocket system uses a Hub pattern:
Client → WebSocket Upgrade → Hub (register/broadcast) → Subscribed Clients- Hub — Central message router (
hub.go) managing client registration, subscriptions, and broadcasts. - Client — Represents a single WebSocket connection with a send channel and subscription map.
- Subscription — Associates a client with an SLO ID and update channels (e.g.,
status,metrics).
Connecting
javascript
const ws = new WebSocket("wss://slozy.example.com/ws");
ws.onopen = () => {
// Subscribe to SLO updates
ws.send(JSON.stringify({
type: "subscribe",
data: { slo_id: 1, channels: ["status", "metrics"] }
}));
};Message Types
| Type | Direction | Description |
|---|---|---|
subscribe | Client → Server | Subscribe to SLO updates for a specific SLO ID |
unsubscribe | Client → Server | Unsubscribe from SLO updates |
ping / pong | Bidirectional | Keep-alive (server pings every 54s, read deadline 60s) |
| Broadcasts | Server → Client | Pushed metrics, status changes, and alerts |
SLOCalculationService
internal/prometheus/calculator.go powers the real-time metrics via GetRealTimeMetrics():
go
calcReq := &models.SLOCalculationRequest{
SLOID: slo.ID,
TimeWindow: "5m",
}This queries Prometheus every evaluation cycle and pushes results to subscribed WebSocket clients through Hub.BroadcastToSLO(sloID, "metric_update", data).
Broadcasting
go
// Broadcast to all subscribers of an SLO
hub.BroadcastToSLO(sloID, "metric_update", map[string]interface{}{
"current_value": 99.92,
"error_budget": 15.3,
"status": "healthy",
})Statistics
The Hub exposes GetStatistics() returning connected clients, total subscriptions, clients per SLO, and message queue length.