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

feat(backend): alerting #40

Merged
merged 2 commits into from
Sep 9, 2024
Merged

feat(backend): alerting #40

merged 2 commits into from
Sep 9, 2024

Conversation

YogiPristiawan
Copy link
Collaborator

@YogiPristiawan YogiPristiawan commented May 27, 2024

Close #27

NOTE: The alerting feature is currently only available for the Telegram provider.

  • Some environment variables below need to be added:

    • TELEGRAM_URL (including bot token)
    • TELEGRAM_CHAT_ID channel or chat id
  • The alert_provider field must be added to the monitor configuration file with a value of either telegram or discord. If not value is set, the default telegram provider will be use

@YogiPristiawan YogiPristiawan requested a review from a team as a code owner May 27, 2024 12:09
@aldy505
Copy link
Member

aldy505 commented May 30, 2024

We'll hold this one for a while.

@YogiPristiawan
Copy link
Collaborator Author

why?

Copy link
Member

@aldy505 aldy505 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are lots of things that we need to refactor, but we'll accept this for now.

Comment on lines +25 to +87
type TelegramProvider struct {
url string
chatID string
}

type TelegramProviderConfig struct {
Url string
ChatID string
}

func NewTelegramAlertProvider(config TelegramProviderConfig) *TelegramProvider {
return &TelegramProvider{
url: config.Url,
}
}

func (t TelegramProvider) Send(ctx context.Context, msg AlertMessage) error {
if t.url == "" || t.chatID == "" {
return fmt.Errorf("can't make a telegram alert request: some config is not set")
}

// Perhaps we can use a template file instead.
title := "🔴 Down"
if msg.Success {
title = "✅ Up"
}
text := fmt.Sprintf(title+`

**MonitorID:** %s
**MonitorName:** %s
**StatusCode:** %d
**Latency:** %d
**Timestamp:** %s`,
msg.MonitorID,
msg.MonitorName,
msg.StatusCode,
msg.Latency,
msg.Timestamp.Format(time.RFC3339),
)
payload := map[string]any{
"chat_id": t.chatID,
"text": text,
"parse_mode": "Markdown",
}
payloadByte, _ := json.Marshal(payload)

req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.url, bytes.NewReader(payloadByte))
if err != nil {
return fmt.Errorf("failed to send telegram alert: %w", err)
}
defer req.Body.Close()

req.Header.Set("Content-Type", "application/json")

client := http.Client{Timeout: time.Second * 3}

_, err = client.Do(req)
if err != nil {
return fmt.Errorf("failed to make request: %w", err)
}

return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is supposed to be moved to another file

Comment on lines +61 to +69
telegramChatID, ok := os.LookupEnv("TELEGRAM_CHAT_ID")
if !ok {
log.Warn().Msg("TELEGRAM_CHAT_ID is not set")
}

telegramUrl, ok := os.LookupEnv("TELEGRAM_URL is not set")
if !ok {
log.Warn().Msg("TELEGRAM_URL is not set")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we want to support other kind of alerting system, we shouldn't make this as a warn log if the value does not exists.

Comment on lines +113 to +118
processor := &Processor{
telegramAlertProvider: NewTelegramAlertProvider(TelegramProviderConfig{
Url: telegramUrl,
ChatID: telegramChatID,
}),
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, this should be array of available alerter. But we'll work on refactoring that later.

@aldy505 aldy505 merged commit 57c8f08 into master Sep 9, 2024
3 checks passed
@aldy505 aldy505 deleted the feat/alerter branch September 9, 2024 06:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Backend: Add alerting integrations
2 participants