Skip to content

Commit

Permalink
prometheus: Better way to determine alert name
Browse files Browse the repository at this point in the history
First it checks if the alert has an instance label. If not it falls back
to the alert name.
This fixes #26
  • Loading branch information
fleaz committed Mar 12, 2019
1 parent fa94edc commit 47af480
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions input/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ func (m PrometheusModule) GetHandler() http.HandlerFunc {
instanceList = instanceList[:0]

for _, alert := range alertList {
name := alert.Labels["instance"].(string)
name = shortenInstanceName(name, m.hostnameFilter)
name := getNameFromLabels(&alert, m.hostnameFilter)
value, ok := alert.Annotations["value"].(string)
if ok {
inst = instance{Name: name, Value: value}
Expand Down Expand Up @@ -194,3 +193,16 @@ func (m PrometheusModule) GetHandler() http.HandlerFunc {
}

}

// getNameFromLabels tries to determine a meaningful name for an alert
// If the alert has no 'instance' label, we use the 'alertname' which should always
// be present in an alert
func getNameFromLabels(alert *alert, filter string) string {
if instance, ok := alert.Labels["instance"]; ok {
return shortenInstanceName(instance.(string), filter)
} else if alertName, ok := alert.Labels["alertname"]; ok {
return alertName.(string)
} else {
return "unknown"
}
}

0 comments on commit 47af480

Please sign in to comment.