Files
scrabble-game/backend/internal/social/metrics.go
T
Ilia Denisov aaac816dc2
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Successful in 49s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m16s
feat(chat): unread read-receipts with lobby/game dot and history-open ack
Persist per-message read state as a chat_messages.unread_seats bitmask
(migration 00008): a text message seeds every recipient seat's bit, a nudge
only the awaited seat's. A seat's bit clears when the player opens the move
history or chat (POST /games/:id/chat/read, sent only when something is
unread), and a nudge additionally clears when its recipient answers by moving
(a wired game NudgeClearer, dependency-inverted so game keeps off social).

UI shows a per-viewer unread dot in the lobby (next to the opponent) and the
game score bar — the unread_chat game-view flag seeds it from authoritative
REST views, live chat/nudge events raise it. Opening the move history counts
as reading (even without entering chat): the 💬 fade-blinks twice and the
client acks. Admin Messages gains an unread-only filter, a read/unread column,
and a per-message card with the per-seat read breakdown. Observability:
chat_read_duration histogram + chat_unread_messages gauge + social tracing.
2026-06-17 11:12:38 +02:00

80 lines
2.9 KiB
Go

package social
import (
"context"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
)
// meterName scopes the social domain's OpenTelemetry instruments.
const meterName = "scrabble/backend/social"
// socialMetrics holds the social domain's operational instruments. It defaults to
// no-ops (see defaultSocialMetrics); SetMetrics installs the real meter during
// startup wiring.
type socialMetrics struct {
messages metric.Int64Counter
readDuration metric.Float64Histogram
}
// defaultSocialMetrics returns instruments backed by a no-op meter.
func defaultSocialMetrics() *socialMetrics {
return newSocialMetrics(noop.NewMeterProvider().Meter(meterName))
}
// newSocialMetrics builds the instruments on meter, falling back to a no-op
// counter on the (rare) construction error.
func newSocialMetrics(meter metric.Meter) *socialMetrics {
c, err := meter.Int64Counter("chat_messages_total",
metric.WithDescription("Per-game chat entries posted, labelled by kind (message/nudge)."))
if err != nil {
c, _ = noop.NewMeterProvider().Meter(meterName).Int64Counter("chat_messages_total")
}
h, err := meter.Float64Histogram("chat_read_duration",
metric.WithDescription("Time from a chat entry being posted to a recipient reading it, in seconds, labelled by kind (message/nudge)."),
metric.WithUnit("s"))
if err != nil {
h, _ = noop.NewMeterProvider().Meter(meterName).Float64Histogram("chat_read_duration")
}
return &socialMetrics{messages: c, readDuration: h}
}
// SetMetrics installs the meter the social domain records to. It must be called
// during startup wiring; the default is a no-op meter.
func (svc *Service) SetMetrics(meter metric.Meter) {
if meter == nil {
return
}
svc.metrics = newSocialMetrics(meter)
// chat_unread_messages observes the current backlog of unread chat entries at scrape
// time (the partial index keeps the count cheap). A registration error is ignored —
// the gauge is best-effort observability.
_, _ = meter.Int64ObservableGauge("chat_unread_messages",
metric.WithDescription("Chat entries with at least one recipient seat that has not read them yet."),
metric.WithInt64Callback(func(ctx context.Context, o metric.Int64Observer) error {
n, err := svc.store.countUnread(ctx)
if err != nil {
return err
}
o.Observe(n)
return nil
}))
}
// recordChat counts one posted chat entry of the given kind (message or nudge).
func (m *socialMetrics) recordChat(ctx context.Context, kind string) {
m.messages.Add(ctx, 1, metric.WithAttributes(attribute.String("kind", kind)))
}
// recordRead records one chat entry's publish-to-read latency, labelled by kind. A
// non-positive duration (clock skew) is dropped.
func (m *socialMetrics) recordRead(ctx context.Context, kind string, d time.Duration) {
if d <= 0 {
return
}
m.readDuration.Record(ctx, d.Seconds(), metric.WithAttributes(attribute.String("kind", kind)))
}