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

Add auth options for WebHook notifier #1149

Closed
Closed
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
4 changes: 4 additions & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"net/url"
"strings"
"time"

pconfig "github.com/prometheus/common/config"
)

var (
Expand Down Expand Up @@ -297,6 +299,8 @@ type WebhookConfig struct {

// URL to send POST request to.
URL string `yaml:"url" json:"url"`
// HTTP client parameters.
HTTPClientConfig pconfig.HTTPClientConfig `yaml:"http_client_config,inline" json:"http_client_config,inline"`

// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
Expand Down
15 changes: 13 additions & 2 deletions notify/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
pconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/common/version"
"golang.org/x/net/context"
Expand Down Expand Up @@ -148,13 +149,23 @@ var userAgentHeader = fmt.Sprintf("Alertmanager/%s", version.Version)
type Webhook struct {
// The URL to which notifications are sent.
URL string
client *http.Client
tmpl *template.Template
logger log.Logger
}

// NewWebhook returns a new Webhook.
func NewWebhook(conf *config.WebhookConfig, t *template.Template, l log.Logger) *Webhook {
return &Webhook{URL: conf.URL, tmpl: t, logger: l}
w := &Webhook{URL: conf.URL, client: http.DefaultClient, tmpl: t, logger: l}

client, err := pconfig.NewHTTPClientFromConfig(&conf.HTTPClientConfig)
if err != nil {
level.Error(l).Log("msg", "Failed to create HTTP client for webhook", "err", err, "url", conf.URL)
} else {
w.client = client
}

return w
}

// WebhookMessage defines the JSON object send to webhook endpoints.
Expand Down Expand Up @@ -193,7 +204,7 @@ func (w *Webhook) Notify(ctx context.Context, alerts ...*types.Alert) (bool, err
req.Header.Set("Content-Type", contentTypeJSON)
req.Header.Set("User-Agent", userAgentHeader)

resp, err := ctxhttp.Do(ctx, http.DefaultClient, req)
resp, err := ctxhttp.Do(ctx, w.client, req)
if err != nil {
return true, err
}
Expand Down
47 changes: 47 additions & 0 deletions vendor/github.com/prometheus/common/config/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

279 changes: 279 additions & 0 deletions vendor/github.com/prometheus/common/config/http_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading