package social import ( "context" "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 } // 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") } return &socialMetrics{messages: c} } // 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) } // 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))) }