Stage 12: observability & performance (OTel/OTLP, domain metrics, guest GC)
Tests · Go / test (push) Successful in 11s
Tests · Integration / integration (push) Successful in 12s
Tests · Go / test (pull_request) Successful in 10s
Tests · Integration / integration (pull_request) Successful in 11s

- 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.
This commit is contained in:
Ilia Denisov
2026-06-04 14:22:15 +02:00
parent 01485d8fc6
commit dcd8de8b00
44 changed files with 1434 additions and 224 deletions
+3
View File
@@ -63,6 +63,9 @@ The bot turns a `/start <payload>` or a notification target into a launch-button
| `TELEGRAM_TEST_ENV` | `false` | route to the Bot API **test environment** (`/bot<token>/test/METHOD`) |
| `TELEGRAM_GAME_CHANNEL_ID` | — | game channel chat id for `SendToGameChannel` |
| `TELEGRAM_LOG_LEVEL` | `info` | zap log level |
| `TELEGRAM_SERVICE_NAME` | `scrabble-telegram` | OpenTelemetry `service.name` |
| `TELEGRAM_OTEL_TRACES_EXPORTER` | `none` | `none`, `stdout` or `otlp` (gRPC; endpoint from `OTEL_EXPORTER_OTLP_*`) |
| `TELEGRAM_OTEL_METRICS_EXPORTER` | `none` | `none`, `stdout` or `otlp` |
The **test environment** is selected by `TELEGRAM_TEST_ENV=true`, which suffixes the
Bot API path with `/test` (the connector appends it to the token, since the client
+22 -1
View File
@@ -12,11 +12,14 @@ import (
"net"
"os/signal"
"syscall"
"time"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.uber.org/zap"
"google.golang.org/grpc"
telegramv1 "scrabble/pkg/proto/telegram/v1"
pkgtel "scrabble/pkg/telemetry"
"scrabble/platform/telegram/internal/bot"
"scrabble/platform/telegram/internal/config"
"scrabble/platform/telegram/internal/connector"
@@ -24,6 +27,9 @@ import (
"scrabble/platform/telegram/internal/loginwidget"
)
// telemetryShutdownTimeout bounds the OpenTelemetry flush during process exit.
const telemetryShutdownTimeout = 5 * time.Second
func main() {
cfg, err := config.Load()
if err != nil {
@@ -46,6 +52,21 @@ func main() {
// run wires the bot and the gRPC server and serves both until the context is
// cancelled.
func run(ctx context.Context, cfg config.Config, logger *zap.Logger) error {
tel, err := pkgtel.New(ctx, cfg.Telemetry)
if err != nil {
return err
}
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), telemetryShutdownTimeout)
defer cancel()
if err := tel.Shutdown(shutdownCtx); err != nil {
logger.Warn("telemetry shutdown", zap.Error(err))
}
}()
if err := tel.StartRuntimeMetrics(); err != nil {
logger.Warn("telemetry: start runtime metrics", zap.Error(err))
}
b, err := bot.New(bot.Config{
Token: cfg.BotToken,
APIBaseURL: cfg.APIBaseURL,
@@ -60,7 +81,7 @@ func run(ctx context.Context, cfg config.Config, logger *zap.Logger) error {
loginwidget.NewHMACValidator(cfg.BotToken),
b, cfg.GameChannelID, logger)
grpcServer := grpc.NewServer()
grpcServer := grpc.NewServer(grpc.StatsHandler(otelgrpc.NewServerHandler()))
telegramv1.RegisterTelegramServer(grpcServer, srv)
lis, err := net.Listen("tcp", cfg.GRPCAddr)
+1
View File
@@ -5,6 +5,7 @@ go 1.26.3
require (
github.com/go-telegram/bot v1.21.0
github.com/google/flatbuffers v23.5.26+incompatible
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.68.0
go.uber.org/zap v1.27.1
google.golang.org/grpc v1.80.0
google.golang.org/protobuf v1.36.11
@@ -6,6 +6,8 @@ import (
"os"
"strconv"
"strings"
pkgtel "scrabble/pkg/telemetry"
)
// Config is the Telegram connector's runtime configuration, read from the
@@ -34,6 +36,8 @@ type Config struct {
GameChannelID int64
// LogLevel is the zap log level (TELEGRAM_LOG_LEVEL, default info).
LogLevel string
// Telemetry configures the OpenTelemetry providers (shared bootstrap).
Telemetry pkgtel.Config
}
// Load reads the connector configuration from the environment, applying defaults
@@ -47,6 +51,11 @@ func Load() (Config, error) {
TestEnv: os.Getenv("TELEGRAM_TEST_ENV") == "true",
LogLevel: envOr("TELEGRAM_LOG_LEVEL", "info"),
}
tel := pkgtel.DefaultConfig("scrabble-telegram")
tel.ServiceName = envOr("TELEGRAM_SERVICE_NAME", tel.ServiceName)
tel.TracesExporter = envOr("TELEGRAM_OTEL_TRACES_EXPORTER", tel.TracesExporter)
tel.MetricsExporter = envOr("TELEGRAM_OTEL_METRICS_EXPORTER", tel.MetricsExporter)
cfg.Telemetry = tel
if cfg.BotToken == "" {
return Config{}, fmt.Errorf("config: TELEGRAM_BOT_TOKEN is required")
}
@@ -60,6 +69,9 @@ func Load() (Config, error) {
}
cfg.GameChannelID = id
}
if err := cfg.Telemetry.Validate(); err != nil {
return Config{}, fmt.Errorf("config: %w", err)
}
return cfg, nil
}
@@ -0,0 +1,41 @@
package config
import (
"testing"
pkgtel "scrabble/pkg/telemetry"
)
// setRequired sets the two required connector variables so Load reaches the
// telemetry checks.
func setRequired(t *testing.T) {
t.Helper()
t.Setenv("TELEGRAM_BOT_TOKEN", "test-token")
t.Setenv("TELEGRAM_MINIAPP_URL", "https://example.org/app")
}
// TestLoadTelemetryDefaults verifies the connector telemetry defaults: the
// "scrabble-telegram" service name and both exporters off.
func TestLoadTelemetryDefaults(t *testing.T) {
setRequired(t)
c, err := Load()
if err != nil {
t.Fatalf("Load: %v", err)
}
if c.Telemetry.ServiceName != "scrabble-telegram" {
t.Errorf("Telemetry.ServiceName = %q, want scrabble-telegram", c.Telemetry.ServiceName)
}
if c.Telemetry.TracesExporter != pkgtel.ExporterNone || c.Telemetry.MetricsExporter != pkgtel.ExporterNone {
t.Errorf("exporters = %q/%q, want none/none", c.Telemetry.TracesExporter, c.Telemetry.MetricsExporter)
}
}
// TestLoadRejectsUnsupportedExporter verifies an exporter outside the supported
// set fails validation.
func TestLoadRejectsUnsupportedExporter(t *testing.T) {
setRequired(t)
t.Setenv("TELEGRAM_OTEL_TRACES_EXPORTER", "jaeger")
if _, err := Load(); err == nil {
t.Fatal("Load: expected an error for an unsupported exporter, got nil")
}
}