2013-07-16 15:03:56 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-07-17 15:45:01 +00:00
|
|
|
package manager
|
2013-07-16 15:03:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hash/fnv"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
const AlertNameLabel = "alertname"
|
2013-07-23 08:40:23 +00:00
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
type AlertFingerprint uint64
|
2013-07-18 12:49:37 +00:00
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
type AlertLabelSet map[string]string
|
|
|
|
type AlertLabelSets []AlertLabelSet
|
2013-07-23 08:40:23 +00:00
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
type AlertPayload map[string]string
|
|
|
|
|
|
|
|
type Alerts []*Alert
|
|
|
|
|
|
|
|
// Alert models an action triggered by Prometheus.
|
|
|
|
type Alert struct {
|
|
|
|
// Short summary of alert.
|
2013-07-30 11:18:11 +00:00
|
|
|
Summary string
|
2013-08-27 13:32:08 +00:00
|
|
|
// Long description of alert.
|
2013-07-30 11:18:11 +00:00
|
|
|
Description string
|
2013-07-16 15:03:56 +00:00
|
|
|
// Label value pairs for purpose of aggregation, matching, and disposition
|
2013-07-30 11:18:11 +00:00
|
|
|
// dispatching. This must minimally include an "alertname" label.
|
2013-08-27 13:32:08 +00:00
|
|
|
Labels AlertLabelSet
|
2013-07-18 12:49:37 +00:00
|
|
|
// Extra key/value information which is not used for aggregation.
|
2013-08-27 13:32:08 +00:00
|
|
|
Payload AlertPayload
|
2013-07-23 08:40:23 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
func (a *Alert) Name() string {
|
|
|
|
return a.Labels[AlertNameLabel]
|
2013-07-16 15:03:56 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
func (a *Alert) Fingerprint() AlertFingerprint {
|
|
|
|
return a.Labels.Fingerprint()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l AlertLabelSet) Fingerprint() AlertFingerprint {
|
2013-07-16 15:03:56 +00:00
|
|
|
keys := []string{}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
for k := range l {
|
2013-07-16 15:03:56 +00:00
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
summer := fnv.New64a()
|
|
|
|
|
2013-07-30 11:12:24 +00:00
|
|
|
separator := string([]byte{0})
|
2013-07-16 15:03:56 +00:00
|
|
|
for _, k := range keys {
|
2013-08-27 13:32:08 +00:00
|
|
|
fmt.Fprintf(summer, "%s%s%s%s", k, separator, l[k], separator)
|
2013-07-16 15:03:56 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
return AlertFingerprint(summer.Sum64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l AlertLabelSet) Equal(o AlertLabelSet) bool {
|
|
|
|
if len(l) != len(o) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for k, v := range l {
|
|
|
|
if o[k] != v {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2013-07-16 15:03:56 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
func (l AlertLabelSet) MatchOnLabels(o AlertLabelSet, labels []string) bool {
|
|
|
|
for _, k := range labels {
|
|
|
|
if l[k] != o[k] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|