Skip to content

Commit

Permalink
feat: display details for firing alerts (#2)
Browse files Browse the repository at this point in the history
* feat: display details for firing alerts

* chore: fixed linting
  • Loading branch information
freak12techno authored Aug 29, 2022
1 parent bff1e2e commit de73248
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 12 deletions.
2 changes: 1 addition & 1 deletion alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (g *AlertmanagerStruct) DoQuery(method string, url string, body interface{}
return nil, err
}

if resp.StatusCode >= 400 {
if resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("Could not fetch request. Status code: %d", resp.StatusCode)
}

Expand Down
8 changes: 4 additions & 4 deletions alerts_firing.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ func HandleListFiringAlerts(c tele.Context) error {

var sb strings.Builder
if len(grafanaGroups) > 0 {
sb.WriteString("<strong>Grafana alerts</strong>\n")
sb.WriteString("<strong>Grafana alerts:</strong>\n")
for _, group := range grafanaGroups {
for _, rule := range group.Rules {
sb.WriteString(rule.Serialize(group.Name))
sb.WriteString(rule.SerializeFull(group.Name))
}
}
} else {
sb.WriteString("<strong>No Grafana alerts</strong>\n")
}

if len(prometheusGroups) > 0 {
sb.WriteString("<strong>Prometheus alerts</strong>\n")
sb.WriteString("<strong>Prometheus alerts:</strong>\n")
for _, group := range prometheusGroups {
for _, rule := range group.Rules {
sb.WriteString(rule.Serialize(group.Name))
sb.WriteString(rule.SerializeFull(group.Name))
}
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions alerts_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func HandleListAlerts(c tele.Context) error {

var sb strings.Builder
if len(grafanaGroups) > 0 {
sb.WriteString("<strong>Grafana alerts</strong>\n")
sb.WriteString("<strong>Grafana alerts:</strong>\n")
for _, group := range grafanaGroups {
for _, rule := range group.Rules {
sb.WriteString(rule.Serialize(group.Name))
Expand All @@ -36,7 +36,7 @@ func HandleListAlerts(c tele.Context) error {
}

if len(prometheusGroups) > 0 {
sb.WriteString("<strong>Prometheus alerts</strong>\n")
sb.WriteString("<strong>Prometheus alerts:</strong>\n")
for _, group := range prometheusGroups {
for _, rule := range group.Rules {
sb.WriteString(rule.Serialize(group.Name))
Expand Down
2 changes: 1 addition & 1 deletion grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (g *GrafanaStruct) DoQuery(method string, url string, body interface{}) (io
return nil, err
}

if resp.StatusCode >= 400 {
if resp.StatusCode >= http.StatusBadRequest {
g.Logger.Error().
Str("url", url).
Str("method", method).
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"io/ioutil"
"os"
"time"

Expand Down Expand Up @@ -33,7 +32,7 @@ func Execute(cmd *cobra.Command, args []string) {
log.Fatal().Msg("Cannot start without config")
}

yamlFile, err := ioutil.ReadFile(ConfigPath)
yamlFile, err := os.ReadFile(ConfigPath)
if err != nil {
log.Fatal().Err(err).Msg("Could not read config file")
}
Expand Down
26 changes: 26 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type GrafanaAlertRule struct {
type GrafanaAlert struct {
Labels map[string]string `json:"labels"`
State string `json:"state"`
Value string `json:"value"`
}

type RenderOptions struct {
Expand Down Expand Up @@ -134,6 +135,31 @@ func (alert *GrafanaAlert) Serialize() string {
return fmt.Sprintf("- %s <pre>%s</pre>", GetEmojiByStatus(alert.State), SerializeAlertLabels(alert.Labels))
}

func (rule *GrafanaAlertRule) SerializeFull(groupName string) string {
var sb strings.Builder

sb.WriteString(fmt.Sprintf(
"%s %s -> %s (%d):\n",
GetEmojiByStatus(rule.State),
groupName,
rule.Name,
len(rule.Alerts),
))

for _, alert := range rule.Alerts {
for key, label := range alert.Labels {
if key == "alertname" {
continue
}
sb.WriteString(fmt.Sprintf("%s = %s\n", key, label))
}
sb.WriteString(fmt.Sprintf("value = %.1f\n", StrToFloat64(alert.Value)))
sb.WriteString("\n")
}

return sb.String()
}

func (silence *Silence) Serialize() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("ID: <code>%s</code>\n", silence.ID))
Expand Down
14 changes: 12 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"

tele "gopkg.in/telebot.v3"
)

const MAX_MESSAGE_SIZE = 4096
const MaxMessageSize = 4096

func NormalizeString(input string) string {
reg := regexp.MustCompile("[^a-zA-Z0-9]+")
Expand Down Expand Up @@ -148,7 +149,7 @@ func BotReply(c tele.Context, msg string) error {
var sb strings.Builder

for _, line := range msgsByNewline {
if sb.Len()+len(line) > MAX_MESSAGE_SIZE {
if sb.Len()+len(line) > MaxMessageSize {
if err := c.Reply(sb.String(), tele.ModeHTML); err != nil {
log.Error().Err(err).Msg("Could not send Telegram message")
return err
Expand Down Expand Up @@ -280,3 +281,12 @@ func FilterFiringOrPendingAlertGroups(groups []GrafanaAlertGroup) []GrafanaAlert

return returnGroups
}

func StrToFloat64(s string) float64 {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
panic(err)
}

return f
}

0 comments on commit de73248

Please sign in to comment.