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

Improve some logging statements #24

Merged
merged 1 commit into from Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 0 additions & 7 deletions README.md
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
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
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
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