Skip to content

Commit

Permalink
support loading webhook URL from a file
Browse files Browse the repository at this point in the history
/cc prometheus#2498

Signed-off-by: Simon Rozet <me@simonrozet.com>
  • Loading branch information
sr committed Jan 19, 2023
1 parent a8e4c16 commit 46d58f0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
10 changes: 7 additions & 3 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ type WebhookConfig struct {
HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`

// URL to send POST request to.
URL *URL `yaml:"url" json:"url"`
URL *URL `yaml:"url" json:"url"`
URLFile string `yaml:"url_file" json:"url_file"`
// MaxAlerts is the maximum number of alerts to be sent per webhook message.
// Alerts exceeding this threshold will be truncated. Setting this to 0
// allows an unlimited number of alerts.
Expand All @@ -487,8 +488,11 @@ func (c *WebhookConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if c.URL == nil {
return fmt.Errorf("missing URL in webhook config")
if c.URL == nil && c.URLFile == "" {
return fmt.Errorf("one of url or url_file must be configured")
}
if c.URL != nil && c.URLFile != "" {
return fmt.Errorf("at most one of url & url_file must be configured")
}
if c.URL.Scheme != "https" && c.URL.Scheme != "http" {
return fmt.Errorf("scheme required for webhook url")
Expand Down
20 changes: 19 additions & 1 deletion config/notifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,25 @@ func TestWebhookURLIsPresent(t *testing.T) {
var cfg WebhookConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)

expected := "missing URL in webhook config"
expected := "one of url or url_file must be configured"

if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}

func TestWebhookURLOrURLFile(t *testing.T) {
in := `
url: 'http://example.com'
url_file: 'http://example.com'
`
var cfg WebhookConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)

expected := "at most one of url & url_file must be configured"

if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
Expand Down
4 changes: 3 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ Pushover notifications are sent via the [Pushover API](https://pushover.net/api)
# Whether to notify about resolved alerts.
[ send_resolved: <boolean> | default = true ]

# The recipient user's key.
# The recipient user's key.
# user_key and user_key_file are mutually exclusive.
user_key: <secret>
user_key_file: <filepath>
Expand Down Expand Up @@ -1116,7 +1116,9 @@ The webhook receiver allows configuring a generic receiver.
[ send_resolved: <boolean> | default = true ]

# The endpoint to send HTTP POST requests to.
# url and url_file are mutually exclusive.
url: <string>
url_file: <filepath>

# The HTTP client's configuration.
[ http_config: <http_config> | default = global.http_config ]
Expand Down
14 changes: 13 additions & 1 deletion notify/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"net/http"
"os"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand Down Expand Up @@ -101,7 +102,18 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er
return false, err
}

resp, err := notify.PostJSON(ctx, n.client, n.conf.URL.String(), &buf)
var url string
if n.conf.URL != nil {
url = n.conf.URL.String()
} else {
content, err := os.ReadFile(n.conf.URLFile)
if err != nil {
return false, fmt.Errorf("read url_file: %w", err)
}
url = string(content)
}

resp, err := notify.PostJSON(ctx, n.client, url, &buf)
if err != nil {
return true, err
}
Expand Down

0 comments on commit 46d58f0

Please sign in to comment.