From 3c4de5a8c3245802b78bf3911a2f388a4e648da1 Mon Sep 17 00:00:00 2001 From: William Perron Date: Sun, 3 Mar 2024 16:37:43 -0500 Subject: [PATCH] enable propagation of the weekly notification spans --- cmd/themis-server/main.go | 8 ++++---- notify.go | 32 ++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/cmd/themis-server/main.go b/cmd/themis-server/main.go index 90dee27..3d46106 100644 --- a/cmd/themis-server/main.go +++ b/cmd/themis-server/main.go @@ -110,7 +110,7 @@ func main() { log.Fatal().Err(err).Msg("failed to initialize tracing") } - notifChan := make(chan struct{}) + notifChan := make(chan context.Context) notifier := themis.NewNotifier(notifChan) go notifier.Start(ctx) @@ -626,7 +626,7 @@ func main() { return nil }, "send-schedule": func(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) error { - notifier.Send() + notifier.Send(ctx) if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -731,11 +731,11 @@ func main() { } log.Info().Int("count", len(created)).Dur("startup_latency_ms", time.Since(start)).Msg("registered commands, ready to operate") - go notifier.NotifyFunc(ctx, func() { + go notifier.NotifyFunc(ctx, func(ctx context.Context) { ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() - ctx, span := tracer.Start(ctx, "weekly notification", trace.WithNewRoot()) + ctx, span := tracer.Start(ctx, "weekly_notification") defer span.End() log.Info().Msg("sending weekly reminder") diff --git a/notify.go b/notify.go index b54ba0d..2e52ed3 100644 --- a/notify.go +++ b/notify.go @@ -6,6 +6,7 @@ import ( "time" "github.com/rs/zerolog/log" + "go.opentelemetry.io/otel/trace" ) var loc *time.Location @@ -15,10 +16,10 @@ func init() { } type Notifier struct { - c chan struct{} + c chan context.Context } -func NewNotifier(c chan struct{}) *Notifier { +func NewNotifier(c chan context.Context) *Notifier { return &Notifier{ c: c, } @@ -45,8 +46,11 @@ func (n *Notifier) Start(ctx context.Context) { log.Debug().Msg("context deadline exceeded, exiting notifier") return default: - log.Debug().Msg("notifier tick") - n.c <- struct{}{} + log.Debug().Str("parent", "ticker").Msg("notifier tick") + ctx, span := tracer.Start(ctx, "notifier_tick", trace.WithNewRoot()) + + n.c <- ctx + span.End() } ticker := time.NewTicker(time.Hour * 24 * 7) @@ -56,28 +60,32 @@ func (n *Notifier) Start(ctx context.Context) { log.Debug().Msg("context deadline exceeded, exiting notifier") return case <-ticker.C: - log.Debug().Msg("notifier tick") - n.c <- struct{}{} + log.Debug().Str("parent", "ticker").Msg("notifier tick") + ctx, span := tracer.Start(ctx, "notifier_tick", trace.WithNewRoot()) + + n.c <- ctx + span.End() } time.Sleep(time.Second) } } // Trigger the notifier manually. Should be used for testing purposes only. -func (n *Notifier) Send() { - n.c <- struct{}{} +func (n *Notifier) Send(ctx context.Context) { + log.Debug().Str("parent", "trigger").Ctx(ctx).Msg("notifier tick") + n.c <- ctx } // TODO(wperron) is there a (nice) way to instrument this function? -func (n *Notifier) NotifyFunc(ctx context.Context, f func()) { +func (n *Notifier) NotifyFunc(ctx context.Context, f func(context.Context)) { for { select { case <-ctx.Done(): log.Debug().Msg("context deadline exceeded, exiting notify func") return - case <-n.c: - log.Debug().Msg("tick received, notifying function") - f() + case innerCtx := <-n.c: + log.Debug().Ctx(innerCtx).Msg("tick received, notifying function") + f(innerCtx) } time.Sleep(time.Second) }