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

Commit 0655209

Browse files
committed
Update telegram doc and add routing unit test.
1 parent 4c7d36a commit 0655209

File tree

6 files changed

+75
-13
lines changed

6 files changed

+75
-13
lines changed

alerts-template.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ alertRoutes:
77
channel: ""
88
telegram:
99
low_oncall:
10-
telegram_bot_token: ""
11-
telegram_chat_id: ""
10+
bot_token: ""
11+
chat_id: ""
1212

1313
medium:
1414
slack:
@@ -21,8 +21,8 @@ alertRoutes:
2121
integration_key: ""
2222
telegram:
2323
medium_oncall:
24-
telegram_bot_token: ""
25-
telegram_chat_id: ""
24+
bot_token: ""
25+
chat_id: ""
2626

2727
high:
2828
slack:
@@ -36,5 +36,5 @@ alertRoutes:
3636
integration_key: ""
3737
telegram:
3838
high_oncall:
39-
telegrambot_token: ""
40-
telegram_chat_id: ""
39+
bot_token: ""
40+
chat_id: ""

docs/alert-routing.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ The AWS_ENDPOINT is optional and is primarily used for testing with localstack.
6262
## Publishing to a Telegram Channel
6363

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

69-
> Note: Currently, Pessimism only support one Telegram channel to publish alerts to.
70-
7169
## PagerDuty Severity Mapping
7270

7371
PagerDuty supports the following severities: `critical`, `error`, `warning`,

internal/alert/routing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func (rd *routingDirectory) paramsToRouteDirectory(acc *core.AlertClientCfg, sev
126126
if acc.Telegram != nil {
127127
for name, cfg := range acc.Telegram {
128128
conf := &client.TelegramConfig{
129-
ChatID: cfg.ChatID.String(),
130129
Token: cfg.Token.String(),
130+
ChatID: cfg.ChatID.String(),
131131
}
132132
client, err := client.NewTelegramClient(conf, name)
133133
if err != nil {

internal/alert/routing_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ func getCfg() *config.Config {
2626
URL: "test1",
2727
},
2828
},
29+
Telegram: map[string]*core.AlertConfig{
30+
"test1": {
31+
Token: "test1",
32+
ChatID: "test1",
33+
},
34+
},
2935
},
3036
Medium: &core.AlertClientCfg{
3137
PagerDuty: map[string]*core.AlertConfig{
@@ -39,6 +45,12 @@ func getCfg() *config.Config {
3945
URL: "test2",
4046
},
4147
},
48+
Telegram: map[string]*core.AlertConfig{
49+
"test2": {
50+
Token: "test2",
51+
ChatID: "test2",
52+
},
53+
},
4254
},
4355
High: &core.AlertClientCfg{
4456
PagerDuty: map[string]*core.AlertConfig{
@@ -59,6 +71,16 @@ func getCfg() *config.Config {
5971
URL: "test3",
6072
},
6173
},
74+
Telegram: map[string]*core.AlertConfig{
75+
"test2": {
76+
Token: "test2",
77+
ChatID: "test2",
78+
},
79+
"test3": {
80+
Token: "test3",
81+
ChatID: "test3",
82+
},
83+
},
6284
},
6385
},
6486
},
@@ -84,11 +106,14 @@ func Test_AlertClientCfgToClientMap(t *testing.T) {
84106
cm.InitializeRouting(cfg.AlertConfig.RoutingParams)
85107

86108
assert.Len(t, cm.GetSlackClients(core.LOW), 1)
109+
assert.Len(t, cm.GetTelegramClients(core.LOW), 1)
87110
assert.Len(t, cm.GetPagerDutyClients(core.LOW), 0)
88111
assert.Len(t, cm.GetSlackClients(core.MEDIUM), 1)
112+
assert.Len(t, cm.GetTelegramClients(core.MEDIUM), 1)
89113
assert.Len(t, cm.GetPagerDutyClients(core.MEDIUM), 1)
90114
assert.Len(t, cm.GetSlackClients(core.HIGH), 2)
91115
assert.Len(t, cm.GetPagerDutyClients(core.HIGH), 2)
116+
assert.Len(t, cm.GetTelegramClients(core.HIGH), 2)
92117
},
93118
},
94119
{
@@ -107,6 +132,27 @@ func Test_AlertClientCfgToClientMap(t *testing.T) {
107132
assert.Len(t, cm.GetPagerDutyClients(core.MEDIUM), 0)
108133
assert.Len(t, cm.GetSlackClients(core.HIGH), 2)
109134
assert.Len(t, cm.GetPagerDutyClients(core.HIGH), 2)
135+
assert.Len(t, cm.GetTelegramClients(core.HIGH), 2)
136+
},
137+
},
138+
{
139+
name: "Test AlertClientCfgToClientMap Telegram Nil",
140+
description: "Test AlertClientCfgToClientMap doesn't fail when telegram is nil",
141+
testLogic: func(t *testing.T) {
142+
cfg := getCfg()
143+
cfg.AlertConfig.RoutingParams.AlertRoutes.Medium.Telegram = nil
144+
cm := alert.NewRoutingDirectory(cfg.AlertConfig)
145+
assert.NotNil(t, cm, "client map is nil")
146+
147+
cm.InitializeRouting(cfg.AlertConfig.RoutingParams)
148+
assert.Len(t, cm.GetSlackClients(core.LOW), 1)
149+
assert.Len(t, cm.GetPagerDutyClients(core.LOW), 0)
150+
assert.Len(t, cm.GetTelegramClients(core.MEDIUM), 0)
151+
assert.Len(t, cm.GetSlackClients(core.MEDIUM), 1)
152+
assert.Len(t, cm.GetPagerDutyClients(core.MEDIUM), 1)
153+
assert.Len(t, cm.GetSlackClients(core.HIGH), 2)
154+
assert.Len(t, cm.GetPagerDutyClients(core.HIGH), 2)
155+
assert.Len(t, cm.GetTelegramClients(core.HIGH), 2)
110156
},
111157
},
112158
{
@@ -125,6 +171,7 @@ func Test_AlertClientCfgToClientMap(t *testing.T) {
125171
assert.Len(t, cm.GetPagerDutyClients(core.MEDIUM), 1)
126172
assert.Len(t, cm.GetSlackClients(core.HIGH), 2)
127173
assert.Len(t, cm.GetPagerDutyClients(core.HIGH), 2)
174+
assert.Len(t, cm.GetTelegramClients(core.HIGH), 2)
128175
},
129176
},
130177
{
@@ -142,10 +189,13 @@ func Test_AlertClientCfgToClientMap(t *testing.T) {
142189

143190
assert.Len(t, cm.GetSlackClients(core.LOW), 0)
144191
assert.Len(t, cm.GetPagerDutyClients(core.LOW), 0)
192+
assert.Len(t, cm.GetTelegramClients(core.LOW), 0)
145193
assert.Len(t, cm.GetSlackClients(core.MEDIUM), 0)
146194
assert.Len(t, cm.GetPagerDutyClients(core.MEDIUM), 0)
195+
assert.Len(t, cm.GetTelegramClients(core.MEDIUM), 0)
147196
assert.Len(t, cm.GetSlackClients(core.HIGH), 0)
148197
assert.Len(t, cm.GetPagerDutyClients(core.HIGH), 0)
198+
assert.Len(t, cm.GetTelegramClients(core.HIGH), 0)
149199
},
150200
},
151201
}

internal/alert/test_data/alert-routing-test.yaml

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ alertRoutes:
44
config:
55
url: "test-low"
66
channel: "#test-low"
7+
telegram:
8+
config:
9+
bot_token: "test-low"
10+
chat_id: "test-low"
711

812
medium:
913
slack:
@@ -13,6 +17,10 @@ alertRoutes:
1317
pagerduty:
1418
config:
1519
integration_key: "test-medium"
20+
telegram:
21+
config:
22+
bot_token: "test-medium"
23+
chat_id: "test-medium"
1624

1725
high:
1826
slack:
@@ -22,9 +30,15 @@ alertRoutes:
2230
config_2:
2331
url: "test-high-2"
2432
channel: "#test-high-2"
25-
2633
pagerduty:
2734
config:
2835
integration_key: "test-high-1"
2936
config_2:
3037
integration_key: "test-high-2"
38+
telegram:
39+
config:
40+
bot_token: "test-high"
41+
chat_id: "test-high"
42+
config_2:
43+
bot_token: "test-high-2"
44+
chat_id: "test-high-2"

internal/core/alert.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,6 @@ type AlertConfig struct {
144144
URL StringFromEnv `yaml:"url"`
145145
Channel StringFromEnv `yaml:"channel"`
146146
IntegrationKey StringFromEnv `yaml:"integration_key"`
147-
Token StringFromEnv `yaml:"telegram_bot_token"`
148-
ChatID StringFromEnv `yaml:"telegram_chat_id"`
147+
Token StringFromEnv `yaml:"bot_token"`
148+
ChatID StringFromEnv `yaml:"chat_id"`
149149
}

0 commit comments

Comments
 (0)