Skip to content

Commit 65af02a

Browse files
authored
Merge pull request #86 from NETWAYS/feature/alert-labels
Add labels to alert output
2 parents 821fd65 + b06aa40 commit 65af02a

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

cmd/alert_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func TestAlertCmd(t *testing.T) {
8585
args: []string{"run", "../main.go", "alert"},
8686
expected: `[CRITICAL] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
8787
\_ [OK] [HostOutOfMemory] is inactive
88-
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40
89-
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
88+
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
89+
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
9090
|total=3 firing=1 pending=1 inactive=1
9191
9292
exit status 2
@@ -100,8 +100,8 @@ exit status 2
100100
})),
101101
args: []string{"run", "../main.go", "alert", "--problems"},
102102
expected: `[CRITICAL] - 2 Alerts: 1 Firing - 1 Pending - 0 Inactive
103-
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40
104-
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
103+
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
104+
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
105105
|total=2 firing=1 pending=1 inactive=0
106106
107107
exit status 2
@@ -115,7 +115,7 @@ exit status 2
115115
})),
116116
args: []string{"run", "../main.go", "alert", "--problems", "-g", "TLS"},
117117
expected: `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
118-
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
118+
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
119119
|total=1 firing=1 pending=0 inactive=0
120120
121121
exit status 2
@@ -129,8 +129,8 @@ exit status 2
129129
})),
130130
args: []string{"run", "../main.go", "alert", "--problems", "-g", "SQL", "-g", "TLS"},
131131
expected: `[CRITICAL] - 2 Alerts: 1 Firing - 1 Pending - 0 Inactive
132-
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40
133-
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
132+
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
133+
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
134134
|total=2 firing=1 pending=1 inactive=0
135135
136136
exit status 2
@@ -144,7 +144,7 @@ exit status 2
144144
})),
145145
args: []string{"run", "../main.go", "alert", "--problems", "--exclude-alert", "Sql.*DeniedRate"},
146146
expected: `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
147-
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
147+
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
148148
|total=1 firing=1 pending=0 inactive=0
149149
150150
exit status 2
@@ -196,7 +196,7 @@ exit status 3
196196
args: []string{"run", "../main.go", "alert", "--name", "HostOutOfMemory", "--name", "BlackboxTLS"},
197197
expected: `[CRITICAL] - 2 Alerts: 1 Firing - 0 Pending - 1 Inactive
198198
\_ [OK] [HostOutOfMemory] is inactive
199-
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
199+
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
200200
|total=2 firing=1 pending=0 inactive=1
201201
202202
exit status 2
@@ -210,7 +210,7 @@ exit status 2
210210
})),
211211
args: []string{"run", "../main.go", "alert", "--name", "HostOutOfMemory", "--name", "BlackboxTLS", "--problems"},
212212
expected: `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
213-
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
213+
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
214214
|total=1 firing=1 pending=0 inactive=0
215215
216216
exit status 2

internal/alert/alert.go

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package alert
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"slices"
67
"strconv"
@@ -105,5 +106,13 @@ func (a *Rule) GetOutput() (output string) {
105106
value, _ = strconv.ParseFloat(a.Alert.Value, 32)
106107
out.WriteString(fmt.Sprintf(" is %s - value: %.2f", a.AlertingRule.State, value))
107108

109+
// Add labels to the output
110+
l, err := json.Marshal(a.Alert.Labels)
111+
112+
if err == nil {
113+
out.WriteString(" - ")
114+
out.Write(l)
115+
}
116+
108117
return out.String()
109118
}

internal/alert/alert_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestGetOutput(t *testing.T) {
105105

106106
var expected string
107107

108-
expected = "[HighRequestLatency] - Job: [bar] on Instance: [foo] is firing - value: 1.00"
108+
expected = `[HighRequestLatency] - Job: [bar] on Instance: [foo] is firing - value: 1.00 - {"alertname":"HighRequestLatency","instance":"foo","job":"bar"}`
109109
if r.GetOutput() != expected {
110110
t.Error("\nActual: ", r.GetOutput(), "\nExpected: ", expected)
111111
}
@@ -114,14 +114,14 @@ func TestGetOutput(t *testing.T) {
114114
"alertname": "HighRequestLatency",
115115
}
116116

117-
expected = "[HighRequestLatency] is firing - value: 1.00"
117+
expected = `[HighRequestLatency] is firing - value: 1.00 - {"alertname":"HighRequestLatency"}`
118118
if r.GetOutput() != expected {
119119
t.Error("\nActual: ", r.GetOutput(), "\nExpected: ", expected)
120120
}
121121

122122
r.AlertingRule.State = "inactive"
123123

124-
expected = "[HighRequestLatency] is inactive - value: 1.00"
124+
expected = `[HighRequestLatency] is inactive - value: 1.00 - {"alertname":"HighRequestLatency"}`
125125
if r.GetOutput() != expected {
126126
t.Error("\nActual: ", r.GetOutput(), "\nExpected: ", expected)
127127
}

0 commit comments

Comments
 (0)