Files
galaxy-game/lobby/internal/adapters/metricsintentpub/publisher.go
T
2026-04-25 23:20:55 +02:00

45 lines
1.4 KiB
Go

// Package metricsintentpub wraps a ports.IntentPublisher with the
// `lobby.notification.publish_attempts` counter from
// `lobby/README.md` §Observability.
package metricsintentpub
import (
"context"
"galaxy/lobby/internal/ports"
"galaxy/lobby/internal/telemetry"
"galaxy/notificationintent"
)
// Publisher decorates an inner ports.IntentPublisher and increments
// `lobby.notification.publish_attempts` after each call.
type Publisher struct {
inner ports.IntentPublisher
telemetry *telemetry.Runtime
}
// New constructs one Publisher around inner. When telemetryRuntime is nil,
// the wrapper still delegates Publish but does not record metrics.
func New(inner ports.IntentPublisher, telemetryRuntime *telemetry.Runtime) *Publisher {
return &Publisher{inner: inner, telemetry: telemetryRuntime}
}
// Publish forwards intent to the inner publisher and records the attempt
// outcome under the frozen `result` attribute (`ok`/`error`).
func (publisher *Publisher) Publish(ctx context.Context, intent notificationintent.Intent) (string, error) {
if publisher == nil || publisher.inner == nil {
return "", nil
}
id, err := publisher.inner.Publish(ctx, intent)
result := "ok"
if err != nil {
result = "error"
}
publisher.telemetry.RecordNotificationPublish(ctx, string(intent.NotificationType), result)
return id, err
}
// Compile-time interface assertion.
var _ ports.IntentPublisher = (*Publisher)(nil)