Skip to content

Commit b902c33

Browse files
committed
feat: reuse jira clients to reuse connections
Signed-off-by: Luis Davim <[email protected]>
1 parent f58ae33 commit b902c33

File tree

2 files changed

+85
-17
lines changed

2 files changed

+85
-17
lines changed

cmd/jiralert/main.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"encoding/json"
1818
"flag"
1919
"fmt"
20-
"github.com/andygrunwald/go-jira"
2120
"net/http"
2221
"os"
2322
"runtime"
@@ -26,6 +25,7 @@ import (
2625
"github.com/go-kit/log"
2726
"github.com/go-kit/log/level"
2827
"github.com/prometheus-community/jiralert/pkg/alertmanager"
28+
"github.com/prometheus-community/jiralert/pkg/clientset"
2929
"github.com/prometheus-community/jiralert/pkg/config"
3030
"github.com/prometheus-community/jiralert/pkg/notify"
3131
"github.com/prometheus-community/jiralert/pkg/template"
@@ -82,6 +82,8 @@ func main() {
8282
os.Exit(1)
8383
}
8484

85+
var cs clientset.ClientSet
86+
8587
http.HandleFunc("/alert", func(w http.ResponseWriter, req *http.Request) {
8688
level.Debug(logger).Log("msg", "handling /alert webhook request")
8789
defer func() { _ = req.Body.Close() }()
@@ -100,22 +102,7 @@ func main() {
100102
}
101103
level.Debug(logger).Log("msg", " matched receiver", "receiver", conf.Name)
102104

103-
// TODO: Consider reusing notifiers or just jira clients to reuse connections.
104-
var client *jira.Client
105-
var err error
106-
if conf.User != "" && conf.Password != "" {
107-
tp := jira.BasicAuthTransport{
108-
Username: conf.User,
109-
Password: string(conf.Password),
110-
}
111-
client, err = jira.NewClient(tp.Client(), conf.APIURL)
112-
} else if conf.PersonalAccessToken != "" {
113-
tp := jira.PATAuthTransport{
114-
Token: string(conf.PersonalAccessToken),
115-
}
116-
client, err = jira.NewClient(tp.Client(), conf.APIURL)
117-
}
118-
105+
client, err := cs.GetOrCreateJira(conf)
119106
if err != nil {
120107
errorHandler(w, http.StatusInternalServerError, err, conf.Name, &data, logger)
121108
return

pkg/clientset/clientset.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2017 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package clientset
15+
16+
import (
17+
"errors"
18+
"fmt"
19+
"sync"
20+
21+
"github.com/andygrunwald/go-jira"
22+
"github.com/prometheus-community/jiralert/pkg/config"
23+
)
24+
25+
type ClientSet struct {
26+
jira map[string]*jira.Client
27+
sync.RWMutex
28+
}
29+
30+
var errorClientExists = errors.New("client already exists")
31+
32+
func (c *ClientSet) getJira(userName string) (*jira.Client, bool) {
33+
c.RLock()
34+
jc, ok := c.jira[userName]
35+
c.RUnlock()
36+
37+
return jc, ok
38+
}
39+
40+
func (c *ClientSet) newJira(conf *config.ReceiverConfig) (*jira.Client, error) {
41+
if conf == nil {
42+
return nil, fmt.Errorf("missing receiver config")
43+
}
44+
45+
if jc, ok := c.getJira(conf.User); ok {
46+
return jc, fmt.Errorf("jira %w: %s", errorClientExists, conf.User)
47+
}
48+
49+
var (
50+
client *jira.Client
51+
err error
52+
)
53+
if conf.User != "" && conf.Password != "" {
54+
tp := jira.BasicAuthTransport{
55+
Username: conf.User,
56+
Password: string(conf.Password),
57+
}
58+
client, err = jira.NewClient(tp.Client(), conf.APIURL)
59+
} else if conf.PersonalAccessToken != "" {
60+
tp := jira.PATAuthTransport{
61+
Token: string(conf.PersonalAccessToken),
62+
}
63+
client, err = jira.NewClient(tp.Client(), conf.APIURL)
64+
}
65+
66+
if err == nil {
67+
c.Lock()
68+
c.jira[conf.User] = client
69+
c.Unlock()
70+
}
71+
72+
return client, err
73+
}
74+
75+
func (c *ClientSet) GetOrCreateJira(conf *config.ReceiverConfig) (*jira.Client, error) {
76+
jc, err := c.newJira(conf)
77+
if errors.Is(err, errorClientExists) {
78+
err = nil
79+
}
80+
return jc, err
81+
}

0 commit comments

Comments
 (0)