Skip to content

Commit 7dc1191

Browse files
committed
refactor services to use newService HttpClient
1 parent 57306f0 commit 7dc1191

File tree

11 files changed

+40
-127
lines changed

11 files changed

+40
-127
lines changed

pkg/services/alertmanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (s alertmanagerService) Send(notification Notification, dest Destination) e
211211
func (s alertmanagerService) sendOneTarget(ctx context.Context, target string, rawBody []byte) (err error) {
212212
rawURL := fmt.Sprintf("%v://%v%v", s.opts.Scheme, target, s.opts.APIPath)
213213

214-
client, err := httputil.NewServiceHTTPClient(s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, s.opts.InsecureSkipVerify, rawURL, "alertmeanger")
214+
client, err := httputil.NewServiceHTTPClient(s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, s.opts.InsecureSkipVerify, rawURL, "alertmanager")
215215
if err != nil {
216216
return err
217217
}

pkg/services/github.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/bradleyfalzon/ghinstallation/v2"
1515
giturls "github.com/chainguard-dev/git-urls"
1616
"github.com/google/go-github/v69/github"
17-
log "github.com/sirupsen/logrus"
1817
"github.com/spf13/cast"
1918

2019
httputil "github.com/argoproj/notifications-engine/pkg/util/http"
@@ -400,33 +399,28 @@ func NewGitHubService(opts GitHubOptions) (*gitHubService, error) {
400399
return nil, err
401400
}
402401

403-
var idleConnTimeout time.Duration
404-
if opts.IdleConnTimeout != "" {
405-
idleConnTimeout, err = time.ParseDuration(opts.IdleConnTimeout)
406-
if err != nil {
407-
return nil, fmt.Errorf("failed to parse idle connection timeout: %w", err)
408-
}
402+
client, err := httputil.NewServiceHTTPClient(opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxConnsPerHost, opts.IdleConnTimeout, opts.InsecureSkipVerify, url, "github")
403+
if err != nil {
404+
return nil, err
409405
}
410-
tr := httputil.NewLoggingRoundTripper(
411-
httputil.NewTransport(url, opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxConnsPerHost, idleConnTimeout, false), log.WithField("service", "github"))
412-
itr, err := ghinstallation.New(tr, appID, installationID, []byte(opts.PrivateKey))
406+
itr, err := ghinstallation.New(client.Transport, appID, installationID, []byte(opts.PrivateKey))
413407
if err != nil {
414408
return nil, err
415409
}
416410

417-
var client *github.Client
411+
var ghclient *github.Client
418412
if opts.EnterpriseBaseURL == "" {
419-
client = github.NewClient(&http.Client{Transport: itr})
413+
ghclient = github.NewClient(&http.Client{Transport: itr})
420414
} else {
421415
itr.BaseURL = opts.EnterpriseBaseURL
422-
client, err = github.NewClient(&http.Client{Transport: itr}).WithEnterpriseURLs(opts.EnterpriseBaseURL, "")
416+
ghclient, err = github.NewClient(&http.Client{Transport: itr}).WithEnterpriseURLs(opts.EnterpriseBaseURL, "")
423417
if err != nil {
424418
return nil, err
425419
}
426420
}
427421

428422
return &gitHubService{
429-
client: &githubClientAdapter{client: client},
423+
client: &githubClientAdapter{client: ghclient},
430424
}, nil
431425
}
432426

pkg/services/googlechat.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import (
88
"net/http"
99
"net/url"
1010
texttemplate "text/template"
11-
"time"
1211

1312
"github.com/google/uuid"
1413

15-
log "github.com/sirupsen/logrus"
1614
"sigs.k8s.io/yaml"
1715

1816
"google.golang.org/api/chat/v1"
@@ -112,16 +110,10 @@ func (s googleChatService) getClient(recipient string) (googlechatclient *google
112110
if !ok {
113111
return nil, fmt.Errorf("no Google chat webhook configured for recipient %s", recipient)
114112
}
115-
var idleConnTimeout time.Duration
116-
if s.opts.IdleConnTimeout != "" {
117-
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
118-
if err != nil {
119-
return nil, fmt.Errorf("failed to parse idle connection timeout: %w", err)
120-
}
121-
}
122-
transport := httputil.NewTransport(webhookUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, false)
123-
client := &http.Client{
124-
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "googlechat")),
113+
114+
client, err := httputil.NewServiceHTTPClient(s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, s.opts.InsecureSkipVerify, webhookUrl, "googlechat")
115+
if err != nil {
116+
return nil, err
125117
}
126118
return &googlechatClient{httpClient: client, url: webhookUrl}, nil
127119
}

pkg/services/grafana.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,10 @@ func (s *grafanaService) Send(notification Notification, dest Destination) (err
5252
if notification.Message == "" {
5353
log.Warnf("Message is an empty string or not provided in the notifications template")
5454
}
55-
var idleConnTimeout time.Duration
56-
if s.opts.IdleConnTimeout != "" {
57-
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
58-
if err != nil {
59-
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
60-
}
61-
}
62-
client := &http.Client{
63-
Transport: httputil.NewLoggingRoundTripper(
64-
httputil.NewTransport(s.opts.ApiUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, s.opts.InsecureSkipVerify), log.WithField("service", "grafana")),
55+
56+
client, err := httputil.NewServiceHTTPClient(s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, s.opts.InsecureSkipVerify, s.opts.ApiUrl, "grafana")
57+
if err != nil {
58+
return err
6559
}
6660

6761
jsonValue, _ := json.Marshal(ga)

pkg/services/mattermost.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
"io"
88
"net/http"
99
texttemplate "text/template"
10-
"time"
11-
12-
log "github.com/sirupsen/logrus"
1310

1411
httputil "github.com/argoproj/notifications-engine/pkg/util/http"
1512
)
@@ -56,16 +53,9 @@ func NewMattermostService(opts MattermostOptions) NotificationService {
5653
}
5754

5855
func (m *mattermostService) Send(notification Notification, dest Destination) (err error) {
59-
var idleConnTimeout time.Duration
60-
if m.opts.IdleConnTimeout != "" {
61-
idleConnTimeout, err = time.ParseDuration(m.opts.IdleConnTimeout)
62-
if err != nil {
63-
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
64-
}
65-
}
66-
transport := httputil.NewTransport(m.opts.ApiURL, m.opts.MaxIdleConns, m.opts.MaxIdleConnsPerHost, m.opts.MaxConnsPerHost, idleConnTimeout, m.opts.InsecureSkipVerify)
67-
client := &http.Client{
68-
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "mattermost")),
56+
client, err := httputil.NewServiceHTTPClient(m.opts.MaxIdleConns, m.opts.MaxIdleConnsPerHost, m.opts.MaxConnsPerHost, m.opts.IdleConnTimeout, m.opts.InsecureSkipVerify, m.opts.ApiURL, "mattermost")
57+
if err != nil {
58+
return err
6959
}
7060

7161
attachments := []interface{}{}

pkg/services/newrelic.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/http"
99
"strings"
1010
texttemplate "text/template"
11-
"time"
1211

1312
log "github.com/sirupsen/logrus"
1413

@@ -138,16 +137,9 @@ func (s newrelicService) Send(notification Notification, dest Destination) (err
138137
},
139138
}
140139

141-
var idleConnTimeout time.Duration
142-
if s.opts.IdleConnTimeout != "" {
143-
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
144-
if err != nil {
145-
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
146-
}
147-
}
148-
client := &http.Client{
149-
Transport: httputil.NewLoggingRoundTripper(
150-
httputil.NewTransport(s.opts.ApiURL, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, false), log.WithField("service", dest.Service)),
140+
client, err := httputil.NewServiceHTTPClient(s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, s.opts.InsecureSkipVerify, s.opts.ApiURL, "newrelic")
141+
if err != nil {
142+
return err
151143
}
152144

153145
jsonValue, err := json.Marshal(deploymentMarker)

pkg/services/opsgenie.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"net/http"
87
texttemplate "text/template"
9-
"time"
108

119
"github.com/opsgenie/opsgenie-go-sdk-v2/alert"
1210
"github.com/opsgenie/opsgenie-go-sdk-v2/client"
13-
log "github.com/sirupsen/logrus"
1411

1512
httputil "github.com/argoproj/notifications-engine/pkg/util/http"
1613
)
@@ -255,20 +252,14 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) (err
255252
if !ok {
256253
return fmt.Errorf("no API key configured for recipient %s", dest.Recipient)
257254
}
258-
var idleConnTimeout time.Duration
259-
if s.opts.IdleConnTimeout != "" {
260-
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
261-
if err != nil {
262-
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
263-
}
255+
opsclient, err := httputil.NewServiceHTTPClient(s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, s.opts.InsecureSkipVerify, s.opts.ApiUrl, "opsgenie")
256+
if err != nil {
257+
return err
264258
}
265259
alertClient, _ := alert.NewClient(&client.Config{
266260
ApiKey: apiKey,
267261
OpsGenieAPIURL: client.ApiUrl(s.opts.ApiUrl),
268-
HttpClient: &http.Client{
269-
Transport: httputil.NewLoggingRoundTripper(
270-
httputil.NewTransport(s.opts.ApiUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, false), log.WithField("service", "opsgenie")),
271-
},
262+
HttpClient: opsclient,
272263
})
273264

274265
var description, alias, note, entity, user string

pkg/services/slack.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"net/http"
98
"net/url"
109
"regexp"
1110
texttemplate "text/template"
12-
"time"
1311

1412
httputil "github.com/argoproj/notifications-engine/pkg/util/http"
1513
slackutil "github.com/argoproj/notifications-engine/pkg/util/slack"
@@ -206,16 +204,9 @@ func newSlackClient(opts SlackOptions) (slackclient *slack.Client, err error) {
206204
if opts.ApiURL != "" {
207205
apiURL = opts.ApiURL
208206
}
209-
var idleConnTimeout time.Duration
210-
if opts.IdleConnTimeout != "" {
211-
idleConnTimeout, err = time.ParseDuration(opts.IdleConnTimeout)
212-
if err != nil {
213-
return nil, fmt.Errorf("failed to parse idle connection timeout: %w", err)
214-
}
215-
}
216-
transport := httputil.NewTransport(apiURL, opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxIdleConns, idleConnTimeout, opts.InsecureSkipVerify)
217-
client := &http.Client{
218-
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "slack")),
207+
client, err := httputil.NewServiceHTTPClient(opts.MaxIdleConns, opts.MaxIdleConnsPerHost, opts.MaxConnsPerHost, opts.IdleConnTimeout, opts.InsecureSkipVerify, apiURL, "slack")
208+
if err != nil {
209+
return nil, err
219210
}
220211
return slack.New(opts.Token, slack.OptionHTTPClient(client), slack.OptionAPIURL(apiURL)), nil
221212
}

pkg/services/teams.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"net/http"
98
texttemplate "text/template"
10-
"time"
11-
12-
log "github.com/sirupsen/logrus"
139

1410
httputil "github.com/argoproj/notifications-engine/pkg/util/http"
1511
)
@@ -161,16 +157,9 @@ func (s teamsService) Send(notification Notification, dest Destination) (err err
161157
if !ok {
162158
return fmt.Errorf("no teams webhook configured for recipient %s", dest.Recipient)
163159
}
164-
var idleConnTimeout time.Duration
165-
if s.opts.IdleConnTimeout != "" {
166-
idleConnTimeout, err = time.ParseDuration(s.opts.IdleConnTimeout)
167-
if err != nil {
168-
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
169-
}
170-
}
171-
transport := httputil.NewTransport(webhookUrl, s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, idleConnTimeout, false)
172-
client := &http.Client{
173-
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "teams")),
160+
client, err := httputil.NewServiceHTTPClient(s.opts.MaxIdleConns, s.opts.MaxIdleConnsPerHost, s.opts.MaxConnsPerHost, s.opts.IdleConnTimeout, s.opts.InsecureSkipVerify, webhookUrl, "teams")
161+
if err != nil {
162+
return err
174163
}
175164

176165
message, err := teamsNotificationToReader(notification)

pkg/services/webex.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
"net/http"
99
"regexp"
1010
"strings"
11-
"time"
12-
13-
log "github.com/sirupsen/logrus"
1411

1512
httputil "github.com/argoproj/notifications-engine/pkg/util/http"
1613
)
@@ -49,16 +46,9 @@ var validEmail = regexp.MustCompile(`^\S+@\S+\.\S+$`)
4946
func (w webexService) Send(notification Notification, dest Destination) (err error) {
5047
requestURL := fmt.Sprintf("%s/v1/messages", w.opts.ApiURL)
5148

52-
var idleConnTimeout time.Duration
53-
if w.opts.IdleConnTimeout != "" {
54-
idleConnTimeout, err = time.ParseDuration(w.opts.IdleConnTimeout)
55-
if err != nil {
56-
return fmt.Errorf("failed to parse idle connection timeout: %w", err)
57-
}
58-
}
59-
client := &http.Client{
60-
Transport: httputil.NewLoggingRoundTripper(
61-
httputil.NewTransport(requestURL, w.opts.MaxIdleConns, w.opts.MaxIdleConnsPerHost, w.opts.MaxConnsPerHost, idleConnTimeout, false), log.WithField("service", dest.Service)),
49+
client, err := httputil.NewServiceHTTPClient(w.opts.MaxIdleConns, w.opts.MaxIdleConnsPerHost, w.opts.MaxConnsPerHost, w.opts.IdleConnTimeout, w.opts.InsecureSkipVerify, requestURL, "webex")
50+
if err != nil {
51+
return err
6252
}
6353

6454
message := webexMessage{

0 commit comments

Comments
 (0)