feat(chat): unread read-receipts with lobby/game dot and history-open ack
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
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
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.
This commit is contained in:
@@ -2,6 +2,7 @@ package social
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
@@ -15,7 +16,8 @@ const meterName = "scrabble/backend/social"
|
||||
// no-ops (see defaultSocialMetrics); SetMetrics installs the real meter during
|
||||
// startup wiring.
|
||||
type socialMetrics struct {
|
||||
messages metric.Int64Counter
|
||||
messages metric.Int64Counter
|
||||
readDuration metric.Float64Histogram
|
||||
}
|
||||
|
||||
// defaultSocialMetrics returns instruments backed by a no-op meter.
|
||||
@@ -31,7 +33,13 @@ func newSocialMetrics(meter metric.Meter) *socialMetrics {
|
||||
if err != nil {
|
||||
c, _ = noop.NewMeterProvider().Meter(meterName).Int64Counter("chat_messages_total")
|
||||
}
|
||||
return &socialMetrics{messages: c}
|
||||
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
|
||||
@@ -41,9 +49,31 @@ func (svc *Service) SetMetrics(meter metric.Meter) {
|
||||
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)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user