Migrate to types package

This commit is contained in:
Fabian Reinartz 2015-09-25 14:38:57 +02:00
parent c3b5b6288e
commit 1f9886e5ca
2 changed files with 7 additions and 65 deletions

View File

@ -10,6 +10,7 @@ import (
"golang.org/x/net/context"
"github.com/prometheus/alertmanager/provider"
"github.com/prometheus/alertmanager/types"
)
const ResolveTimeout = 30 * time.Second
@ -67,7 +68,7 @@ func (d *Dispatcher) Run() {
d.run(updates)
}
func (d *Dispatcher) run(updates <-chan *Alert) {
func (d *Dispatcher) run(updates <-chan *types.Alert) {
cleanup := time.Tick(30 * time.Second)
for {
@ -128,7 +129,7 @@ func (d *Dispatcher) notifyFunc(dest string) notifyFunc {
// processAlert determins in which aggregation group the alert falls
// and insert it.
func (d *Dispatcher) processAlert(alert *Alert, opts *RouteOpts) {
func (d *Dispatcher) processAlert(alert *types.Alert, opts *RouteOpts) {
group := model.LabelSet{}
for ln, lv := range alert.Labels {
@ -151,58 +152,6 @@ func (d *Dispatcher) processAlert(alert *Alert, opts *RouteOpts) {
ag.insert(alert)
}
type Alert struct {
// Label value pairs for purpose of aggregation, matching, and disposition
// dispatching. This must minimally include an "alertname" label.
Labels model.LabelSet `json:"labels"`
// Extra key/value information which is not used for aggregation.
Payload map[string]string `json:"payload,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
ResolvedAt time.Time `json:"resolved_at,omitempty"`
// The authoritative timestamp.
Timestamp time.Time `json:"timestamp"`
}
// Name returns the name of the alert. It is equivalent to the "alertname" label.
func (a *Alert) Name() string {
return string(a.Labels[model.AlertNameLabel])
}
// func (a *Alert) Merge(o *Alert) bool {
// }
// Fingerprint returns a unique hash for the alert. It is equivalent to
// the fingerprint of the alert's label set.
func (a *Alert) Fingerprint() model.Fingerprint {
return a.Labels.Fingerprint()
}
func (a *Alert) String() string {
s := fmt.Sprintf("%s[%s]", a.Name(), a.Fingerprint())
if a.Resolved() {
return s + "[resolved]"
}
return s + "[active]"
}
func (a *Alert) Resolved() bool {
if a.ResolvedAt.IsZero() {
return false
}
return !a.ResolvedAt.After(time.Now())
}
// alertTimeline is a list of alerts sorted by their timestamp.
type alertTimeline []*Alert
func (at alertTimeline) Len() int { return len(at) }
func (at alertTimeline) Less(i, j int) bool { return at[i].Timestamp.Before(at[j].Timestamp) }
func (at alertTimeline) Swap(i, j int) { at[i], at[j] = at[j], at[i] }
// aggrGroup aggregates alert fingerprints into groups to which a
// common set of routing options applies.
// It emits notifications in the specified intervals.

15
main.go
View File

@ -20,7 +20,7 @@ import (
"github.com/prometheus/common/route"
"github.com/prometheus/log"
"github.com/prometheus/alertmanager/manager"
"github.com/prometheus/alertmanager/provider"
)
var (
@ -33,20 +33,13 @@ func main() {
log.Fatal(err)
}
state := manager.NewPersistentState("data")
memAlerts := provider.NewMemAlerts()
disp := NewDispatcher(memAlerts)
if err = state.Config().Set(conf); err != nil {
log.Fatal(err)
}
disp := manager.NewDispatcher(state, []manager.Notifier{
manager.NewLogNotifier("default"),
})
disp.ApplyConfig(conf)
router := route.New()
go disp.Run()
manager.NewAPI(router.WithPrefix("/api"), state)
http.ListenAndServe(":9091", router)