Skip to content

Commit

Permalink
[Plugin] Pass down ctx and use it (#2626)
Browse files Browse the repository at this point in the history
* Pass down cancellable context and update http plugin

* Use context where we can
  • Loading branch information
LaurenceJJones committed Dec 4, 2023
1 parent bfc92ca commit d1bfadd
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 7 deletions.
3 changes: 1 addition & 2 deletions cmd/notification-http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/notification-sentinel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions cmd/notification-slack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/notification-splunk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/csplugin/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}()
Expand Down

0 comments on commit d1bfadd

Please sign in to comment.