From d1bfaddb6985cfb3a1cec36084edbe473934c543 Mon Sep 17 00:00:00 2001 From: Laurence Jones Date: Mon, 4 Dec 2023 12:05:26 +0000 Subject: [PATCH] [Plugin] Pass down ctx and use it (#2626) * Pass down cancellable context and update http plugin * Use context where we can --- cmd/notification-http/main.go | 3 +-- cmd/notification-sentinel/main.go | 2 +- cmd/notification-slack/main.go | 3 +-- cmd/notification-splunk/main.go | 2 +- pkg/csplugin/notifier.go | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/cmd/notification-http/main.go b/cmd/notification-http/main.go index f7908ddda..6d1da788e 100644 --- a/cmd/notification-http/main.go +++ b/cmd/notification-http/main.go @@ -58,13 +58,12 @@ func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notific if err != nil { return nil, err } - for headerName, headerValue := range cfg.Headers { logger.Debug(fmt.Sprintf("adding header %s: %s", headerName, headerValue)) request.Header.Add(headerName, headerValue) } logger.Debug(fmt.Sprintf("making HTTP %s call to %s with body %s", cfg.Method, cfg.URL, notification.Text)) - resp, err := client.Do(request) + resp, err := client.Do(request.WithContext(ctx)) if err != nil { logger.Error(fmt.Sprintf("Failed to make HTTP request : %s", err)) return nil, err diff --git a/cmd/notification-sentinel/main.go b/cmd/notification-sentinel/main.go index 18eff1b05..c627f9271 100644 --- a/cmd/notification-sentinel/main.go +++ b/cmd/notification-sentinel/main.go @@ -90,7 +90,7 @@ func (s *SentinelPlugin) Notify(ctx context.Context, notification *protobufs.Not req.Header.Set("x-ms-date", now) client := &http.Client{} - resp, err := client.Do(req) + resp, err := client.Do(req.WithContext(ctx)) if err != nil { logger.Error("failed to send request", "error", err) return &protobufs.Empty{}, err diff --git a/cmd/notification-slack/main.go b/cmd/notification-slack/main.go index 901832381..373cd9527 100644 --- a/cmd/notification-slack/main.go +++ b/cmd/notification-slack/main.go @@ -38,10 +38,9 @@ func (n *Notify) Notify(ctx context.Context, notification *protobufs.Notificatio if cfg.LogLevel != nil && *cfg.LogLevel != "" { logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel)) } - logger.Info(fmt.Sprintf("found notify signal for %s config", notification.Name)) logger.Debug(fmt.Sprintf("posting to %s webhook, message %s", cfg.Webhook, notification.Text)) - err := slack.PostWebhook(n.ConfigByName[notification.Name].Webhook, &slack.WebhookMessage{ + err := slack.PostWebhookContext(ctx, n.ConfigByName[notification.Name].Webhook, &slack.WebhookMessage{ Text: notification.Text, }) if err != nil { diff --git a/cmd/notification-splunk/main.go b/cmd/notification-splunk/main.go index 826986877..b24aa538f 100644 --- a/cmd/notification-splunk/main.go +++ b/cmd/notification-splunk/main.go @@ -65,7 +65,7 @@ func (s *Splunk) Notify(ctx context.Context, notification *protobufs.Notificatio req.Header.Add("Authorization", fmt.Sprintf("Splunk %s", cfg.Token)) logger.Debug(fmt.Sprintf("posting event %s to %s", string(data), req.URL)) - resp, err := s.Client.Do(req) + resp, err := s.Client.Do(req.WithContext(ctx)) if err != nil { return &protobufs.Empty{}, err } diff --git a/pkg/csplugin/notifier.go b/pkg/csplugin/notifier.go index 8ab1aa923..a4f5bbc0e 100644 --- a/pkg/csplugin/notifier.go +++ b/pkg/csplugin/notifier.go @@ -26,7 +26,7 @@ func (m *GRPCClient) Notify(ctx context.Context, notification *protobufs.Notific done := make(chan error) go func() { _, err := m.client.Notify( - context.Background(), &protobufs.Notification{Text: notification.Text, Name: notification.Name}, + ctx, &protobufs.Notification{Text: notification.Text, Name: notification.Name}, ) done <- err }()