From fd74ec101d07cf352e54920e8c97efea5123e23a Mon Sep 17 00:00:00 2001 From: Xabier Larrakoetxea Date: Fri, 20 Dec 2019 16:21:42 +0100 Subject: [PATCH] Improve some logging statements Signed-off-by: Xabier Larrakoetxea --- README.md | 7 ------- internal/notify/notify.go | 2 +- internal/notify/telegram/telegram.go | 7 ++++--- testdata/alerts/base.json | 4 +--- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 3d030c7..c979e3e 100644 --- a/README.md +++ b/README.md @@ -77,13 +77,6 @@ By default are served on `/metrics` on `0.0.0.0:8081` You can use the `--notify.dry-run` to show the alerts on the terminal instead of forwarding them to telegram. -Note that the required options are required, so I would suggest to do this before starting to develop with dry-run mode: - -```bash -export ALERTGRAM_TELEGRAM_API_TOKEN=fake -export ALERTGRAM_TELEGRAM_CHAT_ID=1234567890 -``` - Also remember that you can use `--debug` flag. ## FAQ diff --git a/internal/notify/notify.go b/internal/notify/notify.go index e0a49d8..bee28cc 100644 --- a/internal/notify/notify.go +++ b/internal/notify/notify.go @@ -30,7 +30,7 @@ func NewLogger(r TemplateRenderer, l log.Logger) forward.Notifier { } func (l logger) Notify(ctx context.Context, n forward.Notification) error { - logger := l.logger.WithValues(log.KV{"alertGroup": n.AlertGroup.ID, "alertsNumber": len(n.AlertGroup.Alerts)}) + logger := l.logger.WithValues(log.KV{"chatID": n.ChatID, "alertGroup": n.AlertGroup.ID, "alertsNumber": len(n.AlertGroup.Alerts)}) alertText, err := l.renderer.Render(ctx, &n.AlertGroup) if err != nil { diff --git a/internal/notify/telegram/telegram.go b/internal/notify/telegram/telegram.go index b2f464d..a1f5136 100644 --- a/internal/notify/telegram/telegram.go +++ b/internal/notify/telegram/telegram.go @@ -94,6 +94,7 @@ func (n notifier) Notify(ctx context.Context, notification forward.Notification) if err != nil { return fmt.Errorf("could not format the alerts to message: %w", err) } + logger = n.logger.WithValues(log.KV{"telegramChatID": msg.ChatID}) res, err := n.client.Send(msg) if err != nil { @@ -119,15 +120,15 @@ func (n notifier) getChatID(notification forward.Notification) (int64, error) { return chatID, nil } -func (n notifier) createMessage(ctx context.Context, notification forward.Notification) (tgbotapi.Chattable, error) { +func (n notifier) createMessage(ctx context.Context, notification forward.Notification) (tgbotapi.MessageConfig, error) { chatID, err := n.getChatID(notification) if err != nil { - return nil, fmt.Errorf("could not get a valid telegran chat ID: %w", err) + return tgbotapi.MessageConfig{}, fmt.Errorf("could not get a valid telegran chat ID: %w", err) } data, err := n.tplRenderer.Render(ctx, ¬ification.AlertGroup) if err != nil { - return nil, fmt.Errorf("error rendering alerts to template: %w", err) + return tgbotapi.MessageConfig{}, fmt.Errorf("error rendering alerts to template: %w", err) } msg := tgbotapi.NewMessage(chatID, data) diff --git a/testdata/alerts/base.json b/testdata/alerts/base.json index 369317c..7eb3780 100644 --- a/testdata/alerts/base.json +++ b/testdata/alerts/base.json @@ -6,7 +6,7 @@ "status": "firing", "labels": { "alertname": "ServicePodIsRestarting", - "chatid": "-1001234567890", + "chat_id": "myCustomChat1", "instance": "10.0.0.221:20464", "job": "kubernetes-metrics", "owner": "team1", @@ -26,7 +26,6 @@ "status": "firing", "labels": { "alertname": "ServicePodIsRestarting", - "chatid": "-1001234567890", "instance": "10.0.0.221:20464", "job": "kubernetes-metrics", "owner": "team1", @@ -46,7 +45,6 @@ "status": "firing", "labels": { "alertname": "ServicePodIsPending", - "chatid": "-1001234567890", "instance": "10.0.0.221:20464", "job": "kubernetes-metrics", "owner": "team1",