Skip to content

Commit

Permalink
Improve some logging statements (#24)
Browse files Browse the repository at this point in the history
Improve some logging statements
  • Loading branch information
slok authored Dec 20, 2019
2 parents ab033c9 + fd74ec1 commit 56a64b4
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 14 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions internal/notify/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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, &notification.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)
Expand Down
4 changes: 1 addition & 3 deletions testdata/alerts/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -26,7 +26,6 @@
"status": "firing",
"labels": {
"alertname": "ServicePodIsRestarting",
"chatid": "-1001234567890",
"instance": "10.0.0.221:20464",
"job": "kubernetes-metrics",
"owner": "team1",
Expand All @@ -46,7 +45,6 @@
"status": "firing",
"labels": {
"alertname": "ServicePodIsPending",
"chatid": "-1001234567890",
"instance": "10.0.0.221:20464",
"job": "kubernetes-metrics",
"owner": "team1",
Expand Down

0 comments on commit 56a64b4

Please sign in to comment.