Skip to content

Commit

Permalink
feat: add alert firing since (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Dec 11, 2023
1 parent 0b7871f commit c75ff59
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ main
.idea
dist/
grafana-interacter
**/.DS_Store
2 changes: 2 additions & 0 deletions pkg/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (manager *TemplateManager) GetTemplate(name string) (*template.Template, er
"GetEmojiByStatus": utils.GetEmojiByStatus,
"GetEmojiBySilenceStatus": utils.GetEmojiBySilenceStatus,
"StrToFloat64": utils.StrToFloat64,
"FormatDuration": utils.FormatDuration,
"FormatDate": utils.FormatDate,
}).ParseFS(templatesList.Templates, filename)
if err != nil {
return nil, err
Expand Down
12 changes: 9 additions & 3 deletions pkg/types/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"main/pkg/utils/normalize"
"strings"
"time"
)

type GrafanaAlertRulesResponse struct {
Expand All @@ -26,9 +27,14 @@ type GrafanaAlertRule struct {
}

type GrafanaAlert struct {
Labels map[string]string `json:"labels"`
State string `json:"state"`
Value string `json:"value"`
Labels map[string]string `json:"labels"`
State string `json:"state"`
Value string `json:"value"`
ActiveAt time.Time `json:"activeAt"`
}

func (a GrafanaAlert) ActiveSince() time.Duration {
return time.Since(a.ActiveAt)
}

type GrafanaAlertGroups []GrafanaAlertGroup
Expand Down
37 changes: 37 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"main/pkg/logger"
"main/pkg/types"
"math"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -192,3 +193,39 @@ func StrToFloat64(s string) float64 {

return f
}

func FormatDuration(duration time.Duration) string {
days := int64(duration.Hours() / 24)
hours := int64(math.Mod(duration.Hours(), 24))
minutes := int64(math.Mod(duration.Minutes(), 60))
seconds := int64(math.Mod(duration.Seconds(), 60))

chunks := []struct {
singularName string
amount int64
}{
{"day", days},
{"hour", hours},
{"minute", minutes},
{"second", seconds},
}

parts := []string{}

for _, chunk := range chunks {
switch chunk.amount {
case 0:
continue
case 1:
parts = append(parts, fmt.Sprintf("%d %s", chunk.amount, chunk.singularName))
default:
parts = append(parts, fmt.Sprintf("%d %ss", chunk.amount, chunk.singularName))
}
}

return strings.Join(parts, " ")
}

func FormatDate(date time.Time) string {
return date.Format(time.RFC1123)
}
31 changes: 18 additions & 13 deletions templates/alerts_firing.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<strong>Grafana alerts:</strong>
{{- if not .Data.GrafanaGroups }}
No Grafana alerts
{{- if and (not .Data.GrafanaGroups) (not .Data.PrometheusGroups) }}
No firing alerts.
{{ end }}

{{- if .Data.GrafanaGroups }}
<strong>Grafana alerts ({{ len .Data.GrafanaGroups }}):</strong>
{{- end }}
{{- range $groupId, $group := .Data.GrafanaGroups }}
{{- range $ruleId, $rule := $group.Rules }}
Expand All @@ -14,26 +17,28 @@
{{- if $alert.Value }}
value = {{ StrToFloat64 $alert.Value }}
{{- end }}
{{ end }}
firing for {{ FormatDuration $alert.ActiveSince }} (since {{ FormatDate $alert.ActiveAt }})
{{- end }}
{{- end }}
{{ end }}


<strong>Prometheus alerts:</strong>
{{- if not .Data.PrometheusGroups }}
No Prometheus alerts
{{- if .Data.PrometheusGroups }}
<strong>Prometheus alerts ({{ len .Data.PrometheusGroups }}):</strong>
{{- end }}
{{- range $groupId, $group := .Data.PrometheusGroups }}
{{ range $groupId, $group := .Data.PrometheusGroups }}
{{- range $ruleId, $rule := $group.Rules }}
- {{ GetEmojiByStatus $rule.State }} {{ $group.Name }} -> {{ $rule.Name }} ({{ len ($rule.Alerts) }}):
{{- range $alertId, $alert := $rule.Alerts }}
{{ range $alertId, $alert := $rule.Alerts }}
<strong>Firing for:</strong> {{ FormatDuration $alert.ActiveSince }} (since {{ FormatDate $alert.ActiveAt }})
{{- if $alert.Value }}
<strong>Value: </strong>{{ StrToFloat64 $alert.Value }}
{{- end }}
<strong>Labels: </strong>
{{- range $key, $label := $alert.Labels }}
{{- if ne $key "alertname" }}
{{ $key }} = {{ $label }}
{{- end }}
{{ $key }} = {{ $label }}
{{- end }}
{{- if $alert.Value }}
value = {{ StrToFloat64 $alert.Value }}
{{- end }}
{{ end }}
{{- end }}
Expand Down

0 comments on commit c75ff59

Please sign in to comment.