-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
364 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2019 Prometheus Team | ||
// 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 webhook_template | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
commoncfg "github.com/prometheus/common/config" | ||
|
||
"github.com/prometheus/alertmanager/config" | ||
"github.com/prometheus/alertmanager/notify" | ||
"github.com/prometheus/alertmanager/template" | ||
"github.com/prometheus/alertmanager/types" | ||
) | ||
|
||
// Notifier implements a Notifier for generic webhooks. | ||
type Notifier struct { | ||
conf *config.WebhookTemplateConfig | ||
tmpl *template.Template | ||
logger *slog.Logger | ||
client *http.Client | ||
retrier *notify.Retrier | ||
} | ||
|
||
// New returns a new Webhook. | ||
func New(conf *config.WebhookTemplateConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) { | ||
client, err := commoncfg.NewClientFromConfig(*conf.HTTPConfig, "webhook_template", httpOpts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Notifier{ | ||
conf: conf, | ||
tmpl: t, | ||
logger: l, | ||
client: client, | ||
// Webhooks are assumed to respond with 2xx response codes on a successful | ||
// request and 5xx response codes are assumed to be recoverable. | ||
retrier: ¬ify.Retrier{}, | ||
}, nil | ||
} | ||
|
||
// Notify implements the Notifier interface. | ||
func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, error) { | ||
key, err := notify.ExtractGroupKey(ctx) | ||
if err != nil { | ||
return false, err | ||
} | ||
n.logger.Debug("extracted group key", "key", key) | ||
|
||
data := notify.GetTemplateData(ctx, n.tmpl, alerts, n.logger) | ||
tmpl := notify.TmplText(n.tmpl, data, &err) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
body := tmpl(n.conf.Template) | ||
|
||
var buf bytes.Buffer | ||
if err := json.NewEncoder(&buf).Encode(body); err != nil { | ||
return false, err | ||
} | ||
|
||
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 = strings.TrimSpace(string(content)) | ||
} | ||
|
||
if n.conf.Timeout > 0 { | ||
postCtx, cancel := context.WithTimeoutCause(ctx, n.conf.Timeout, fmt.Errorf("configured webhook template timeout reached (%s)", n.conf.Timeout)) | ||
defer cancel() | ||
ctx = postCtx | ||
} | ||
|
||
resp, err := notify.PostJSON(ctx, n.client, url, &buf) | ||
if err != nil { | ||
if ctx.Err() != nil { | ||
err = fmt.Errorf("%w: %w", err, context.Cause(ctx)) | ||
} | ||
return true, notify.RedactURL(err) | ||
} | ||
defer notify.Drain(resp) | ||
|
||
shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body) | ||
if err != nil { | ||
return shouldRetry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err) | ||
} | ||
return shouldRetry, err | ||
} |
Oops, something went wrong.