Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
10.3 KB
Raw Blame
package realtime

import (
	"sort"
	"sync"
	"sync/atomic"
)

// Metrics collects lightweight counters describing the realtime subsystem.
//
// Phase 1 (MUL-1138) extends the phase-0 counter set with subscribe / Redis /
// per-scope-room counters. We keep using std-library atomics rather than a
// Prometheus dependency; a future phase can re-export the same numbers.
type Metrics struct {
	ConnectsTotal        atomic.Int64
	DisconnectsTotal     atomic.Int64
	ActiveConnections    atomic.Int64
	SlowEvictionsTotal   atomic.Int64
	MessagesSentTotal    atomic.Int64
	MessagesDroppedTotal atomic.Int64
	// MembershipRemovalDroppedTotal counts target revalidations discarded when
	// the bounded queue is full. Peer invalidation and content authorization
	// still run.
	MembershipRemovalDroppedTotal atomic.Int64

	// InboundTooLargeTotal counts connections closed because a peer sent a
	// message over inboundReadLimit, on either the pre-auth or the
	// post-auth read path.
	InboundTooLargeTotal atomic.Int64

	// Per-event-type send counters keyed by event type string.
	// Value is *atomic.Int64.
	eventSent sync.Map

	// Per-scope subscribe / unsubscribe / deny counters. Keyed by scope
	// type string ("workspace", "user", "task", "chat"). Value is
	// *atomic.Int64. Scope-room gauges follow the same pattern.
	subscribeTotal       sync.Map
	unsubscribeTotal     sync.Map
	subscribeDeniedTotal sync.Map
	scopeRooms           sync.Map

	// Redis relay counters. Zero unless the Redis broadcaster is enabled.
	RedisXAddTotal               atomic.Int64
	RedisXAddErrors              atomic.Int64
	RedisXReadTotal              atomic.Int64
	RedisXReadErrors             atomic.Int64
	RedisAckTotal                atomic.Int64
	RedisLastXAddLagMicros       atomic.Int64
	RedisMirrorPrimaryErrors     atomic.Int64
	RedisMirrorSecondaryErrors   atomic.Int64
	RedisMirrorDivergenceTotal   atomic.Int64
	RedisRelayStreamTrimmedTotal atomic.Int64
	RedisRelayStreamMissingTotal atomic.Int64
	RedisRelayRetentionErrors    atomic.Int64
	RedisRelayStreamsWithoutTTL  atomic.Int64
	RedisUsedMemoryBytes         atomic.Int64
	RedisMaxMemoryBytes          atomic.Int64
	RedisEvictedKeys             atomic.Int64

	redisStreamsMu sync.RWMutex
	redisStreams   map[string]RedisStreamObservation

	redisTTLStatusMu              sync.Mutex
	redisStreamsWithoutTTLByRelay map[string]int64

	// RedisConnected is set by the relay on startup / reconnect.
	RedisConnected atomic.Bool
	// RedisLastError stores the most recent consumer error message.
	redisLastErrMu sync.RWMutex
	redisLastErr   string

	// NodeID is set once at boot by the relay (or empty in single-node mode).
	NodeID atomic.Value // string
}

// RedisStreamObservation is the latest low-frequency retention sample for one
// relay stream. PTTLMillis uses Redis sentinel values: -1 means no expiry and
// -2 means the key does not exist.
type RedisStreamObservation struct {
	Entries    int64 `json:"entries"`
	PTTLMillis int64 `json:"pttl_millis"`
}

// M is the package-level metrics singleton.
var M = &Metrics{}

func loadOrInitCounter(m *sync.Map, key string) *atomic.Int64 {
	if v, ok := m.Load(key); ok {
		return v.(*atomic.Int64)
	}
	c := new(atomic.Int64)
	if existing, loaded := m.LoadOrStore(key, c); loaded {
		return existing.(*atomic.Int64)
	}
	return c
}

// RecordEvent increments the per-event-type send counter.
func (m *Metrics) RecordEvent(eventType string) {
	if eventType == "" {
		return
	}
	loadOrInitCounter(&m.eventSent, eventType).Add(1)
}

// SubscribesTotal returns the per-scope-type counter for successful subscribes.
func (m *Metrics) SubscribesTotal(scopeType string) *atomic.Int64 {
	return loadOrInitCounter(&m.subscribeTotal, scopeType)
}

// UnsubscribesTotal returns the per-scope-type counter for unsubscribes.
func (m *Metrics) UnsubscribesTotal(scopeType string) *atomic.Int64 {
	return loadOrInitCounter(&m.unsubscribeTotal, scopeType)
}

// SubscribeDeniedTotal returns the per-scope-type counter for denied subscribes.
func (m *Metrics) SubscribeDeniedTotal(scopeType string) *atomic.Int64 {
	return loadOrInitCounter(&m.subscribeDeniedTotal, scopeType)
}

// IncRoom / DecRoom adjust the active-rooms gauge for scopeType.
func (m *Metrics) IncRoom(scopeType string) { loadOrInitCounter(&m.scopeRooms, scopeType).Add(1) }
func (m *Metrics) DecRoom(scopeType string) { loadOrInitCounter(&m.scopeRooms, scopeType).Add(-1) }

// SetRedisLastError stores msg as the most recent Redis consumer error. An
// empty msg clears it.
func (m *Metrics) SetRedisLastError(msg string) {
	m.redisLastErrMu.Lock()
	m.redisLastErr = msg
	m.redisLastErrMu.Unlock()
}

func (m *Metrics) lastRedisErr() string {
	m.redisLastErrMu.RLock()
	defer m.redisLastErrMu.RUnlock()
	return m.redisLastErr
}

// ObserveRedisStream replaces the latest sampled statistics for stream.
func (m *Metrics) ObserveRedisStream(stream string, entries, pttlMillis int64) {
	m.redisStreamsMu.Lock()
	defer m.redisStreamsMu.Unlock()
	if m.redisStreams == nil {
		m.redisStreams = make(map[string]RedisStreamObservation)
	}
	m.redisStreams[stream] = RedisStreamObservation{
		Entries:    entries,
		PTTLMillis: pttlMillis,
	}
}

// RedisStreamObservations returns a copy safe for metrics collection.
func (m *Metrics) RedisStreamObservations() map[string]RedisStreamObservation {
	m.redisStreamsMu.RLock()
	defer m.redisStreamsMu.RUnlock()
	out := make(map[string]RedisStreamObservation, len(m.redisStreams))
	for stream, observation := range m.redisStreams {
		out[stream] = observation
	}
	return out
}

// SetRedisStreamsWithoutTTL updates one relay mode's latest count and exposes
// the process-wide sum. Keeping per-mode state prevents dual mode collectors
// from overwriting each other with whichever maintenance loop ran last.
func (m *Metrics) SetRedisStreamsWithoutTTL(relay string, count int64) {
	m.redisTTLStatusMu.Lock()
	defer m.redisTTLStatusMu.Unlock()
	if m.redisStreamsWithoutTTLByRelay == nil {
		m.redisStreamsWithoutTTLByRelay = make(map[string]int64)
	}
	m.redisStreamsWithoutTTLByRelay[relay] = count
	var total int64
	for _, relayCount := range m.redisStreamsWithoutTTLByRelay {
		total += relayCount
	}
	m.RedisRelayStreamsWithoutTTL.Store(total)
}

func snapshotCounters(s *sync.Map) map[string]int64 {
	out := map[string]int64{}
	s.Range(func(k, v any) bool {
		out[k.(string)] = v.(*atomic.Int64).Load()
		return true
	})
	keys := make([]string, 0, len(out))
	for k := range out {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	ordered := make(map[string]int64, len(out))
	for _, k := range keys {
		ordered[k] = out[k]
	}
	return ordered
}

// Snapshot returns a JSON-friendly copy of the current counter values.
func (m *Metrics) Snapshot() map[string]any {
	nodeID := ""
	if v := m.NodeID.Load(); v != nil {
		nodeID, _ = v.(string)
	}
	return map[string]any{
		"connects_total":                    m.ConnectsTotal.Load(),
		"disconnects_total":                 m.DisconnectsTotal.Load(),
		"active_connections":                m.ActiveConnections.Load(),
		"slow_evictions_total":              m.SlowEvictionsTotal.Load(),
		"messages_sent_total":               m.MessagesSentTotal.Load(),
		"messages_dropped_total":            m.MessagesDroppedTotal.Load(),
		"membership_removals_dropped_total": m.MembershipRemovalDroppedTotal.Load(),
		"inbound_too_large_total":           m.InboundTooLargeTotal.Load(),
		"events_sent_by_type":               snapshotCounters(&m.eventSent),
		"subscribes_total":                  snapshotCounters(&m.subscribeTotal),
		"unsubscribes_total":                snapshotCounters(&m.unsubscribeTotal),
		"subscribe_denied_total":            snapshotCounters(&m.subscribeDeniedTotal),
		"active_scope_rooms":                snapshotCounters(&m.scopeRooms),
		"redis": map[string]any{
			"connected":               m.RedisConnected.Load(),
			"node_id":                 nodeID,
			"xadd_total":              m.RedisXAddTotal.Load(),
			"xadd_errors":             m.RedisXAddErrors.Load(),
			"xread_total":             m.RedisXReadTotal.Load(),
			"xread_errors":            m.RedisXReadErrors.Load(),
			"ack_total":               m.RedisAckTotal.Load(),
			"last_xadd_lag_micros":    m.RedisLastXAddLagMicros.Load(),
			"mirror_primary_errors":   m.RedisMirrorPrimaryErrors.Load(),
			"mirror_secondary_errors": m.RedisMirrorSecondaryErrors.Load(),
			"mirror_divergence_total": m.RedisMirrorDivergenceTotal.Load(),
			"stream_trimmed_total":    m.RedisRelayStreamTrimmedTotal.Load(),
			"stream_missing_total":    m.RedisRelayStreamMissingTotal.Load(),
			"retention_errors":        m.RedisRelayRetentionErrors.Load(),
			"streams_without_ttl":     m.RedisRelayStreamsWithoutTTL.Load(),
			"used_memory_bytes":       m.RedisUsedMemoryBytes.Load(),
			"max_memory_bytes":        m.RedisMaxMemoryBytes.Load(),
			"evicted_keys":            m.RedisEvictedKeys.Load(),
			"streams":                 m.RedisStreamObservations(),
			"last_error":              m.lastRedisErr(),
		},
	}
}

// Reset zeroes all counters. Tests only.
func (m *Metrics) Reset() {
	m.MembershipRemovalDroppedTotal.Store(0)
	m.ConnectsTotal.Store(0)
	m.DisconnectsTotal.Store(0)
	m.ActiveConnections.Store(0)
	m.SlowEvictionsTotal.Store(0)
	m.MessagesSentTotal.Store(0)
	m.MessagesDroppedTotal.Store(0)
	m.InboundTooLargeTotal.Store(0)
	m.eventSent.Range(func(k, _ any) bool { m.eventSent.Delete(k); return true })
	m.subscribeTotal.Range(func(k, _ any) bool { m.subscribeTotal.Delete(k); return true })
	m.unsubscribeTotal.Range(func(k, _ any) bool { m.unsubscribeTotal.Delete(k); return true })
	m.subscribeDeniedTotal.Range(func(k, _ any) bool { m.subscribeDeniedTotal.Delete(k); return true })
	m.scopeRooms.Range(func(k, _ any) bool { m.scopeRooms.Delete(k); return true })
	m.RedisXAddTotal.Store(0)
	m.RedisXAddErrors.Store(0)
	m.RedisXReadTotal.Store(0)
	m.RedisXReadErrors.Store(0)
	m.RedisAckTotal.Store(0)
	m.RedisLastXAddLagMicros.Store(0)
	m.RedisMirrorPrimaryErrors.Store(0)
	m.RedisMirrorSecondaryErrors.Store(0)
	m.RedisMirrorDivergenceTotal.Store(0)
	m.RedisRelayStreamTrimmedTotal.Store(0)
	m.RedisRelayStreamMissingTotal.Store(0)
	m.RedisRelayRetentionErrors.Store(0)
	m.RedisRelayStreamsWithoutTTL.Store(0)
	m.redisTTLStatusMu.Lock()
	m.redisStreamsWithoutTTLByRelay = nil
	m.redisTTLStatusMu.Unlock()
	m.RedisUsedMemoryBytes.Store(0)
	m.RedisMaxMemoryBytes.Store(0)
	m.RedisEvictedKeys.Store(0)
	m.redisStreamsMu.Lock()
	m.redisStreams = nil
	m.redisStreamsMu.Unlock()
	m.RedisConnected.Store(false)
	m.SetRedisLastError("")
}