Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Update telegram doc and add routing unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgreg committed Mar 19, 2024
1 parent 4c7d36a commit 0655209
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 13 deletions.
12 changes: 6 additions & 6 deletions alerts-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ alertRoutes:
channel: ""
telegram:
low_oncall:
telegram_bot_token: ""
telegram_chat_id: ""
bot_token: ""
chat_id: ""

medium:
slack:
Expand All @@ -21,8 +21,8 @@ alertRoutes:
integration_key: ""
telegram:
medium_oncall:
telegram_bot_token: ""
telegram_chat_id: ""
bot_token: ""
chat_id: ""

high:
slack:
Expand All @@ -36,5 +36,5 @@ alertRoutes:
integration_key: ""
telegram:
high_oncall:
telegrambot_token: ""
telegram_chat_id: ""
bot_token: ""
chat_id: ""
4 changes: 1 addition & 3 deletions docs/alert-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ The AWS_ENDPOINT is optional and is primarily used for testing with localstack.
## Publishing to a Telegram Channel

It's possible to publish alerts to a Telegram channel by adding the channel's
ID and bot token to the `telegram_bot_token` and `telegram_bot_token`
ID and bot token to the `chat_id` and `bot_token`
variables in the `alerts-routing.` configuration file. Generate a bot token by leveraging the
following [guide](https://core.telegram.org/bots#how-do-i-create-a-bot).

> Note: Currently, Pessimism only support one Telegram channel to publish alerts to.
## PagerDuty Severity Mapping

PagerDuty supports the following severities: `critical`, `error`, `warning`,
Expand Down
2 changes: 1 addition & 1 deletion internal/alert/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func (rd *routingDirectory) paramsToRouteDirectory(acc *core.AlertClientCfg, sev
if acc.Telegram != nil {
for name, cfg := range acc.Telegram {
conf := &client.TelegramConfig{
ChatID: cfg.ChatID.String(),
Token: cfg.Token.String(),
ChatID: cfg.ChatID.String(),
}
client, err := client.NewTelegramClient(conf, name)
if err != nil {
Expand Down
50 changes: 50 additions & 0 deletions internal/alert/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func getCfg() *config.Config {
URL: "test1",
},
},
Telegram: map[string]*core.AlertConfig{
"test1": {
Token: "test1",
ChatID: "test1",
},
},
},
Medium: &core.AlertClientCfg{
PagerDuty: map[string]*core.AlertConfig{
Expand All @@ -39,6 +45,12 @@ func getCfg() *config.Config {
URL: "test2",
},
},
Telegram: map[string]*core.AlertConfig{
"test2": {
Token: "test2",
ChatID: "test2",
},
},
},
High: &core.AlertClientCfg{
PagerDuty: map[string]*core.AlertConfig{
Expand All @@ -59,6 +71,16 @@ func getCfg() *config.Config {
URL: "test3",
},
},
Telegram: map[string]*core.AlertConfig{
"test2": {
Token: "test2",
ChatID: "test2",
},
"test3": {
Token: "test3",
ChatID: "test3",
},
},
},
},
},
Expand All @@ -84,11 +106,14 @@ func Test_AlertClientCfgToClientMap(t *testing.T) {
cm.InitializeRouting(cfg.AlertConfig.RoutingParams)

assert.Len(t, cm.GetSlackClients(core.LOW), 1)
assert.Len(t, cm.GetTelegramClients(core.LOW), 1)
assert.Len(t, cm.GetPagerDutyClients(core.LOW), 0)
assert.Len(t, cm.GetSlackClients(core.MEDIUM), 1)
assert.Len(t, cm.GetTelegramClients(core.MEDIUM), 1)
assert.Len(t, cm.GetPagerDutyClients(core.MEDIUM), 1)
assert.Len(t, cm.GetSlackClients(core.HIGH), 2)
assert.Len(t, cm.GetPagerDutyClients(core.HIGH), 2)
assert.Len(t, cm.GetTelegramClients(core.HIGH), 2)
},
},
{
Expand All @@ -107,6 +132,27 @@ func Test_AlertClientCfgToClientMap(t *testing.T) {
assert.Len(t, cm.GetPagerDutyClients(core.MEDIUM), 0)
assert.Len(t, cm.GetSlackClients(core.HIGH), 2)
assert.Len(t, cm.GetPagerDutyClients(core.HIGH), 2)
assert.Len(t, cm.GetTelegramClients(core.HIGH), 2)
},
},
{
name: "Test AlertClientCfgToClientMap Telegram Nil",
description: "Test AlertClientCfgToClientMap doesn't fail when telegram is nil",
testLogic: func(t *testing.T) {
cfg := getCfg()
cfg.AlertConfig.RoutingParams.AlertRoutes.Medium.Telegram = nil
cm := alert.NewRoutingDirectory(cfg.AlertConfig)
assert.NotNil(t, cm, "client map is nil")

cm.InitializeRouting(cfg.AlertConfig.RoutingParams)
assert.Len(t, cm.GetSlackClients(core.LOW), 1)
assert.Len(t, cm.GetPagerDutyClients(core.LOW), 0)
assert.Len(t, cm.GetTelegramClients(core.MEDIUM), 0)
assert.Len(t, cm.GetSlackClients(core.MEDIUM), 1)
assert.Len(t, cm.GetPagerDutyClients(core.MEDIUM), 1)
assert.Len(t, cm.GetSlackClients(core.HIGH), 2)
assert.Len(t, cm.GetPagerDutyClients(core.HIGH), 2)
assert.Len(t, cm.GetTelegramClients(core.HIGH), 2)
},
},
{
Expand All @@ -125,6 +171,7 @@ func Test_AlertClientCfgToClientMap(t *testing.T) {
assert.Len(t, cm.GetPagerDutyClients(core.MEDIUM), 1)
assert.Len(t, cm.GetSlackClients(core.HIGH), 2)
assert.Len(t, cm.GetPagerDutyClients(core.HIGH), 2)
assert.Len(t, cm.GetTelegramClients(core.HIGH), 2)
},
},
{
Expand All @@ -142,10 +189,13 @@ func Test_AlertClientCfgToClientMap(t *testing.T) {

assert.Len(t, cm.GetSlackClients(core.LOW), 0)
assert.Len(t, cm.GetPagerDutyClients(core.LOW), 0)
assert.Len(t, cm.GetTelegramClients(core.LOW), 0)
assert.Len(t, cm.GetSlackClients(core.MEDIUM), 0)
assert.Len(t, cm.GetPagerDutyClients(core.MEDIUM), 0)
assert.Len(t, cm.GetTelegramClients(core.MEDIUM), 0)
assert.Len(t, cm.GetSlackClients(core.HIGH), 0)
assert.Len(t, cm.GetPagerDutyClients(core.HIGH), 0)
assert.Len(t, cm.GetTelegramClients(core.HIGH), 0)
},
},
}
Expand Down
16 changes: 15 additions & 1 deletion internal/alert/test_data/alert-routing-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ alertRoutes:
config:
url: "test-low"
channel: "#test-low"
telegram:
config:
bot_token: "test-low"
chat_id: "test-low"

medium:
slack:
Expand All @@ -13,6 +17,10 @@ alertRoutes:
pagerduty:
config:
integration_key: "test-medium"
telegram:
config:
bot_token: "test-medium"
chat_id: "test-medium"

high:
slack:
Expand All @@ -22,9 +30,15 @@ alertRoutes:
config_2:
url: "test-high-2"
channel: "#test-high-2"

pagerduty:
config:
integration_key: "test-high-1"
config_2:
integration_key: "test-high-2"
telegram:
config:
bot_token: "test-high"
chat_id: "test-high"
config_2:
bot_token: "test-high-2"
chat_id: "test-high-2"
4 changes: 2 additions & 2 deletions internal/core/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ type AlertConfig struct {
URL StringFromEnv `yaml:"url"`
Channel StringFromEnv `yaml:"channel"`
IntegrationKey StringFromEnv `yaml:"integration_key"`
Token StringFromEnv `yaml:"telegram_bot_token"`
ChatID StringFromEnv `yaml:"telegram_chat_id"`
Token StringFromEnv `yaml:"bot_token"`
ChatID StringFromEnv `yaml:"chat_id"`
}

0 comments on commit 0655209

Please sign in to comment.