Skip to content

Commit

Permalink
🐛 send alerts to channels (#306)
Browse files Browse the repository at this point in the history

* 🐛 send alerts to channels
  • Loading branch information
abahmed authored May 31, 2024
1 parent 4c51d37 commit 436d786
Show file tree
Hide file tree
Showing 30 changed files with 183 additions and 194 deletions.
10 changes: 5 additions & 5 deletions alertmanager/dingtalk/dingtalk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ func TestSendEvent(t *testing.T) {
c.url = s.URL + "/send?accessToken=%s"

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand Down
4 changes: 2 additions & 2 deletions alertmanager/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ func (s *Discord) SendEvent(ev *event.Event) error {
},
{
Name: "Name",
Value: ev.Name,
Value: ev.PodName,
Inline: true,
},
{
Name: "Container",
Value: ev.Container,
Value: ev.ContainerName,
Inline: true,
},
{
Expand Down
8 changes: 4 additions & 4 deletions alertmanager/discord/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func TestSendEvent(t *testing.T) {
c.send = mockedSend

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "Nam quis nulla. Integer malesuada. In in enim a arcu " +
"imperdiet malesuada. Sed vel lectus. Donec odio urna, tempus " +
"molestie, porttitor ut, iaculis quis, sem. Phasellus rhoncus.\n" +
Expand Down
6 changes: 3 additions & 3 deletions alertmanager/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ func (e *Email) buildMessageSubjectAndBody(
logsText = util.JsonEscape(ev.Logs)
}

subject := fmt.Sprintf("⛑ Kwatch detected a crash in pod %s ", ev.Container)
subject := fmt.Sprintf("⛑ Kwatch detected a crash in pod %s ", ev.ContainerName)
body := fmt.Sprintf(
"An alert for cluster: *%s* Name: *%s* Container: *%s* "+
"Namespace: *%s* "+
"has been triggered:\\n—\\n "+
"Logs: *%s* \\n "+
"Events: *%s* ",
e.appCfg.ClusterName,
ev.Name,
ev.Container,
ev.PodName,
ev.ContainerName,
ev.Namespace,
logsText,
eventsText,
Expand Down
10 changes: 5 additions & 5 deletions alertmanager/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ func TestSendEvent(t *testing.T) {

c.send = mockedSend
ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand Down
22 changes: 11 additions & 11 deletions alertmanager/feishu/feishu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func TestBuildRequestBodyFeiShu(t *testing.T) {
c := NewFeiShu(configMap, &config.App{ClusterName: "dev"})
assertions.NotNil(c)
ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "test",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "test",
}
formattedMsg := ev.FormatMarkdown(c.appCfg.ClusterName, "", "")

Expand Down Expand Up @@ -113,11 +113,11 @@ func TestSendEvent(t *testing.T) {
assertions.NotNil(c)

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand Down
10 changes: 5 additions & 5 deletions alertmanager/googlechat/googlechat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func TestSendEvent(t *testing.T) {
assert.NotNil(c)

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand Down
10 changes: 5 additions & 5 deletions alertmanager/matrix/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ func TestSendEvent(t *testing.T) {
assert.NotNil(c)

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand Down
4 changes: 2 additions & 2 deletions alertmanager/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ func (m *Mattermost) buildMessage(e *event.Event, msg *string) []byte {
},
{
Title: "Name",
Value: e.Name,
Value: e.PodName,
Short: true,
},
{
Title: "Container",
Value: e.Container,
Value: e.ContainerName,
Short: true,
},
{
Expand Down
10 changes: 5 additions & 5 deletions alertmanager/mattermost/mattermost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func TestSendEvent(t *testing.T) {
assert.NotNil(c)

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand Down
8 changes: 4 additions & 4 deletions alertmanager/opsgenie/opsgenie.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,21 @@ func (m *Opsgenie) buildMessage(e *event.Event) []byte {
// use custom title if it's provided, otherwise use default
title := m.title
if len(title) == 0 {
title = fmt.Sprintf(defaultOpsgenieTitle, e.Name)
title = fmt.Sprintf(defaultOpsgenieTitle, e.PodName)
}
payload.Message = title

// use custom text if it's provided, otherwise use default
text := m.text
if len(text) == 0 {
text = fmt.Sprintf(defaultOpsgenieText, e.Container, e.Name)
text = fmt.Sprintf(defaultOpsgenieText, e.ContainerName, e.PodName)
}

payload.Description = text
payload.Details = map[string]string{
"Cluster": m.appCfg.ClusterName,
"Name": e.Name,
"Container": e.Container,
"Name": e.PodName,
"Container": e.ContainerName,
"Namespace": e.Namespace,
"Reason": e.Reason,
"Events": events,
Expand Down
30 changes: 15 additions & 15 deletions alertmanager/opsgenie/opsgenie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func TestSendEvent(t *testing.T) {
c.url = s.URL

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand All @@ -91,11 +91,11 @@ func TestSendEventError(t *testing.T) {
c.url = s.URL

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand All @@ -113,11 +113,11 @@ func TestInvaildHttpRequest(t *testing.T) {
c.url = "h ttp://localhost"

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand Down
8 changes: 4 additions & 4 deletions alertmanager/pagerduty/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ func (s *Pagerduty) buildRequestBodyPagerDuty(
}
}`,
key,
fmt.Sprintf(defaultEventTitle, ev.Container),
ev.Container,
fmt.Sprintf(defaultEventTitle, ev.ContainerName),
ev.ContainerName,
s.appCfg.ClusterName,
ev.Name,
ev.Container,
ev.PodName,
ev.ContainerName,
ev.Namespace,
ev.Reason,
eventsText,
Expand Down
30 changes: 15 additions & 15 deletions alertmanager/pagerduty/pagerduty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func TestSendEvent(t *testing.T) {
assert.NotNil(c)

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand All @@ -88,11 +88,11 @@ func TestSendEventError(t *testing.T) {
c.url = s.URL

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand All @@ -110,11 +110,11 @@ func TestInvaildHttpRequest(t *testing.T) {
c.url = "h ttp://localhost"

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand Down
10 changes: 5 additions & 5 deletions alertmanager/rocketchat/rocketchat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func TestSendEvent(t *testing.T) {
assert.NotNil(c)

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand Down
4 changes: 2 additions & 2 deletions alertmanager/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (s *Slack) SendEvent(ev *event.Event) error {
Type: "section",
Fields: []*slackClient.TextBlockObject{
markdownF("*Cluster*\n%s", s.appCfg.ClusterName),
markdownF("*Name*\n%s", ev.Name),
markdownF("*Container*\n%s", ev.Container),
markdownF("*Name*\n%s", ev.PodName),
markdownF("*Container*\n%s", ev.ContainerName),
markdownF("*Namespace*\n%s", ev.Namespace),
markdownF("*Reason*\n%s", ev.Reason),
},
Expand Down
8 changes: 4 additions & 4 deletions alertmanager/slack/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func TestSendEvent(t *testing.T) {
s.send = mockedSend

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "Nam quis nulla. Integer malesuada. In in enim a arcu " +
"imperdiet malesuada. Sed vel lectus. Donec odio urna, tempus " +
"molestie, porttitor ut, iaculis quis, sem. Phasellus rhoncus.\n" +
Expand Down
10 changes: 5 additions & 5 deletions alertmanager/teams/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func TestSendEvent(t *testing.T) {
assert.NotNil(c)

ev := event.Event{
Name: "test-pod",
Container: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
PodName: "test-pod",
ContainerName: "test-container",
Namespace: "default",
Reason: "OOMKILLED",
Logs: "test\ntestlogs",
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" +
"event3\nevent5\nevent6-event8-event11-event12",
}
Expand Down
Loading

0 comments on commit 436d786

Please sign in to comment.