Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions cmd/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func TestAlertCmd(t *testing.T) {
args: []string{"run", "../main.go", "alert"},
expected: `[CRITICAL] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
\_ [OK] [HostOutOfMemory] is inactive
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
|total=3 firing=1 pending=1 inactive=1

exit status 2
Expand All @@ -100,8 +100,8 @@ exit status 2
})),
args: []string{"run", "../main.go", "alert", "--problems"},
expected: `[CRITICAL] - 2 Alerts: 1 Firing - 1 Pending - 0 Inactive
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
|total=2 firing=1 pending=1 inactive=0

exit status 2
Expand All @@ -115,7 +115,7 @@ exit status 2
})),
args: []string{"run", "../main.go", "alert", "--problems", "-g", "TLS"},
expected: `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
|total=1 firing=1 pending=0 inactive=0

exit status 2
Expand All @@ -129,8 +129,8 @@ exit status 2
})),
args: []string{"run", "../main.go", "alert", "--problems", "-g", "SQL", "-g", "TLS"},
expected: `[CRITICAL] - 2 Alerts: 1 Firing - 1 Pending - 0 Inactive
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
|total=2 firing=1 pending=1 inactive=0

exit status 2
Expand All @@ -144,7 +144,7 @@ exit status 2
})),
args: []string{"run", "../main.go", "alert", "--problems", "--exclude-alert", "Sql.*DeniedRate"},
expected: `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
|total=1 firing=1 pending=0 inactive=0

exit status 2
Expand Down Expand Up @@ -196,7 +196,7 @@ exit status 3
args: []string{"run", "../main.go", "alert", "--name", "HostOutOfMemory", "--name", "BlackboxTLS"},
expected: `[CRITICAL] - 2 Alerts: 1 Firing - 0 Pending - 1 Inactive
\_ [OK] [HostOutOfMemory] is inactive
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
|total=2 firing=1 pending=0 inactive=1

exit status 2
Expand All @@ -210,7 +210,7 @@ exit status 2
})),
args: []string{"run", "../main.go", "alert", "--name", "HostOutOfMemory", "--name", "BlackboxTLS", "--problems"},
expected: `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
|total=1 firing=1 pending=0 inactive=0

exit status 2
Expand Down
9 changes: 9 additions & 0 deletions internal/alert/alert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package alert

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

// Add labels to the output
l, err := json.Marshal(a.Alert.Labels)

if err == nil {
out.WriteString(" - ")
out.Write(l)
}

return out.String()
}
6 changes: 3 additions & 3 deletions internal/alert/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestGetOutput(t *testing.T) {

var expected string

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

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

r.AlertingRule.State = "inactive"

expected = "[HighRequestLatency] is inactive - value: 1.00"
expected = `[HighRequestLatency] is inactive - value: 1.00 - {"alertname":"HighRequestLatency"}`
if r.GetOutput() != expected {
t.Error("\nActual: ", r.GetOutput(), "\nExpected: ", expected)
}
Expand Down
Loading