Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reuse jira clients to reuse connections #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions cmd/jiralert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
"runtime"
"strconv"

"github.com/andygrunwald/go-jira"

Check failure on line 25 in cmd/jiralert/main.go

View workflow job for this annotation

GitHub Actions / test

imported and not used: "github.com/andygrunwald/go-jira" as jira (typecheck)
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/jiralert/pkg/alertmanager"
"github.com/prometheus-community/jiralert/pkg/clientset"
"github.com/prometheus-community/jiralert/pkg/config"
"github.com/prometheus-community/jiralert/pkg/notify"
"github.com/prometheus-community/jiralert/pkg/template"
Expand Down Expand Up @@ -87,6 +88,8 @@
os.Exit(1)
}

var cs clientset.ClientSet

http.HandleFunc("/alert", func(w http.ResponseWriter, req *http.Request) {
level.Debug(logger).Log("msg", "handling /alert webhook request")
defer func() { _ = req.Body.Close() }()
Expand All @@ -105,22 +108,7 @@
}
level.Debug(logger).Log("msg", " matched receiver", "receiver", conf.Name)

// TODO: Consider reusing notifiers or just jira clients to reuse connections.
var client *jira.Client
var err error
if conf.User != "" && conf.Password != "" {
tp := jira.BasicAuthTransport{
Username: conf.User,
Password: string(conf.Password),
}
client, err = jira.NewClient(tp.Client(), conf.APIURL)
} else if conf.PersonalAccessToken != "" {
tp := jira.PATAuthTransport{
Token: string(conf.PersonalAccessToken),
}
client, err = jira.NewClient(tp.Client(), conf.APIURL)
}

client, err := cs.GetOrCreateJira(conf)
if err != nil {
errorHandler(w, http.StatusInternalServerError, err, conf.Name, &data, logger)
return
Expand Down
84 changes: 84 additions & 0 deletions pkg/clientset/clientset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package clientset

import (
"errors"
"fmt"
"sync"

"github.com/andygrunwald/go-jira"
"github.com/prometheus-community/jiralert/pkg/config"
)

type ClientSet struct {
jira map[string]*jira.Client
sync.RWMutex
}

var errorClientExists = errors.New("client already exists")

func (c *ClientSet) getJira(userName string) (*jira.Client, bool) {
c.RLock()
if c.jira == nil {
c.jira = map[string]*jira.Client{}
}
jc, ok := c.jira[userName]
c.RUnlock()

return jc, ok
}

func (c *ClientSet) newJira(conf *config.ReceiverConfig) (*jira.Client, error) {
if conf == nil {
return nil, fmt.Errorf("missing receiver config")
}

if jc, ok := c.getJira(conf.User); ok {
return jc, fmt.Errorf("jira %w: %s", errorClientExists, conf.User)
}

var (
client *jira.Client
err error
)
if conf.User != "" && conf.Password != "" {
tp := jira.BasicAuthTransport{
Username: conf.User,
Password: string(conf.Password),
}
client, err = jira.NewClient(tp.Client(), conf.APIURL)
} else if conf.PersonalAccessToken != "" {
tp := jira.PATAuthTransport{
Token: string(conf.PersonalAccessToken),
}
client, err = jira.NewClient(tp.Client(), conf.APIURL)
}

if err == nil {
c.Lock()
c.jira[conf.User] = client
c.Unlock()
}

return client, err
}

func (c *ClientSet) GetOrCreateJira(conf *config.ReceiverConfig) (*jira.Client, error) {
jc, err := c.newJira(conf)
if errors.Is(err, errorClientExists) {
err = nil
}
return jc, err
}
Loading