dcd8de8b00
- pkg/telemetry: shared OTel provider bootstrap (none/stdout/otlp + W3C
propagators + Go runtime metrics); backend/internal/telemetry becomes a thin
facade keeping its gin middleware.
- Telemetry parity: gateway and the Telegram connector gain telemetry runtimes
and config (GATEWAY_/TELEGRAM_ SERVICE_NAME + OTEL_*); otelgrpc instruments the
backend push server, the gateway's backend+connector clients and the connector
server. Default exporter stays none (collector/dashboards are Stage 14).
- Operational metrics (variant attribute on game-scoped ones): game_replay_duration,
game_move_validate_duration, games_started_total, games_abandoned_total,
game_cache_active, chat_messages_total{kind}, gateway edge_request_duration.
Wired via the SetMetrics setter pattern (default no-op meter).
- TODO-3: account.GuestReaper deletes guests with no game seat past
BACKEND_GUEST_RETENTION (default 30d, swept every BACKEND_GUEST_REAP_INTERVAL).
- Tests: pkg/telemetry exporter selection; game/social/edge metric recording via
a manual reader; config (otlp accepted, guest knobs); inttest guest reaper.
- Docs: PLAN.md re-scopes Stage 12 and adds Stage 13 (alphabet-on-wire) + Stage 14
(CI/deploy) with the agreed dictionary-versioning resolution; ARCHITECTURE 11/13,
TESTING, the three READMEs and FUNCTIONAL(+ru) updated.
110 lines
4.1 KiB
Go
110 lines
4.1 KiB
Go
package game
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/metric"
|
|
"go.opentelemetry.io/otel/metric/noop"
|
|
"go.uber.org/zap"
|
|
|
|
"scrabble/backend/internal/engine"
|
|
)
|
|
|
|
// meterName scopes the game domain's OpenTelemetry instruments.
|
|
const meterName = "scrabble/backend/game"
|
|
|
|
// gameMetrics holds the game domain's operational instruments. Every game-scoped
|
|
// measurement carries a "variant" attribute (english/russian/erudit). The
|
|
// instruments default to no-ops (see defaultGameMetrics), so recording is always
|
|
// safe; SetMetrics installs the real meter during startup wiring.
|
|
type gameMetrics struct {
|
|
replay metric.Float64Histogram
|
|
validate metric.Float64Histogram
|
|
started metric.Int64Counter
|
|
abandoned metric.Int64Counter
|
|
}
|
|
|
|
// defaultGameMetrics returns instruments backed by a no-op meter, recording
|
|
// nothing until SetMetrics installs a real one.
|
|
func defaultGameMetrics() *gameMetrics {
|
|
return newGameMetrics(noop.NewMeterProvider().Meter(meterName))
|
|
}
|
|
|
|
// newGameMetrics builds the instruments on meter, falling back to no-op
|
|
// instruments on the (rare) construction error so the game domain never fails to
|
|
// start over telemetry.
|
|
func newGameMetrics(meter metric.Meter) *gameMetrics {
|
|
return &gameMetrics{
|
|
replay: histogram(meter, "game_replay_duration", "Seconds to rebuild a live game from its journal on a cache miss."),
|
|
validate: histogram(meter, "game_move_validate_duration", "Seconds to validate and score a tentative play (EvaluatePlay)."),
|
|
started: counter(meter, "games_started_total", "Games created and started."),
|
|
abandoned: counter(meter, "games_abandoned_total", "Player seats dropped by the turn-timeout sweeper."),
|
|
}
|
|
}
|
|
|
|
// SetMetrics installs the meter the game domain records to and registers the
|
|
// observable gauge reporting the live games resident in the cache by variant. 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 = newGameMetrics(meter)
|
|
if _, err := meter.Int64ObservableGauge("game_cache_active",
|
|
metric.WithDescription("Live games currently resident in the in-memory cache, by variant."),
|
|
metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
|
|
for variant, n := range svc.cache.countByVariant() {
|
|
o.Observe(int64(n), metric.WithAttributes(attribute.String("variant", variant)))
|
|
}
|
|
return nil
|
|
}),
|
|
); err != nil {
|
|
svc.log.Warn("game: register cache gauge", zap.Error(err))
|
|
}
|
|
}
|
|
|
|
// recordReplay records the duration of a cache-miss journal replay for variant.
|
|
func (m *gameMetrics) recordReplay(ctx context.Context, v engine.Variant, start time.Time) {
|
|
m.replay.Record(ctx, time.Since(start).Seconds(), variantAttr(v))
|
|
}
|
|
|
|
// recordValidate records the duration of one play validation for variant.
|
|
func (m *gameMetrics) recordValidate(ctx context.Context, v engine.Variant, start time.Time) {
|
|
m.validate.Record(ctx, time.Since(start).Seconds(), variantAttr(v))
|
|
}
|
|
|
|
// recordStarted counts one started game of variant.
|
|
func (m *gameMetrics) recordStarted(ctx context.Context, v engine.Variant) {
|
|
m.started.Add(ctx, 1, variantAttr(v))
|
|
}
|
|
|
|
// recordAbandoned counts one seat dropped by the turn-timeout sweeper in a game of
|
|
// variant.
|
|
func (m *gameMetrics) recordAbandoned(ctx context.Context, v engine.Variant) {
|
|
m.abandoned.Add(ctx, 1, variantAttr(v))
|
|
}
|
|
|
|
// variantAttr is the shared "variant" attribute option, usable for both Record and
|
|
// Add measurements.
|
|
func variantAttr(v engine.Variant) metric.MeasurementOption {
|
|
return metric.WithAttributes(attribute.String("variant", v.String()))
|
|
}
|
|
|
|
func histogram(m metric.Meter, name, desc string) metric.Float64Histogram {
|
|
h, err := m.Float64Histogram(name, metric.WithUnit("s"), metric.WithDescription(desc))
|
|
if err != nil {
|
|
h, _ = noop.NewMeterProvider().Meter(meterName).Float64Histogram(name)
|
|
}
|
|
return h
|
|
}
|
|
|
|
func counter(m metric.Meter, name, desc string) metric.Int64Counter {
|
|
c, err := m.Int64Counter(name, metric.WithDescription(desc))
|
|
if err != nil {
|
|
c, _ = noop.NewMeterProvider().Meter(meterName).Int64Counter(name)
|
|
}
|
|
return c
|
|
}
|