Add in-memory alert provider

This commit is contained in:
Fabian Reinartz 2015-09-25 14:38:07 +02:00
parent 49f51002d4
commit e861482761
2 changed files with 85 additions and 5 deletions

78
provider/mem.go Normal file
View File

@ -0,0 +1,78 @@
// Copyright 2015 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.
package provider
import (
"fmt"
"sync"
"github.com/prometheus/common/model"
"github.com/prometheus/alertmanager/types"
)
var (
ErrNotFound = fmt.Errorf("item not found")
)
// MemAlerts implements an Alerts provider based on in-memory data.
type MemAlerts struct {
mtx sync.RWMutex
alerts map[model.Fingerprint]*types.Alert
listeners []chan *types.Alert
}
func NewMemAlert() *MemAlerts {
return &MemAlerts{
alerts: map[model.Fingerprint]*types.Alert,
}
}
func (a *MemAlerts) IterActive() <-chan *Alert {
a.mtx.Lock()
defer a.mtx.Unlock()
ch := make(chan *types.Alert)
for _, alert := range a.alerts {
ch <- alert
}
a.listeners = append(a.listeners, ch)
return ch
}
func (a *MemAlerts) Put(alert *Alert) error {
a.mtx.RLock()
defer a.mtx.RUnlock()
a.alerts[alert.Fingerprint()] = alert
for _, ch := range a.listeners {
ch <- alert
}
return nil
}
func (a *MemAlerts) Get(fp model.Fingerprint) (*types.Alert, error) {
a.mtx.RLock()
defer a.mtx.RUnlock()
if a, ok := a.alerts[fp]; ok {
return a, nil
}
return nil, ErrNotFound
}

View File

@ -17,6 +17,7 @@ import (
"github.com/prometheus/common/model"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/types"
)
// Alerts gives access to a set of alerts.
@ -24,17 +25,18 @@ type Alerts interface {
// Iter returns a channel on which all active alerts from the
// beginning of time are sent. They are not guaranteed to be in
// chronological order.
IterActive() <-chan *Alert
IterActive() <-chan *types.Alert
// Get returns the alert for a given fingerprint.
Get(model.Fingerprint) (*Alert, error)
Get(model.Fingerprint) (*types.Alert, error)
// Put adds the given alert to the set.
Put(*Alert) error
// Del deletes the alert for the given fingerprint.
Del(model.Fingerprint) error
Put(*types.Alert) error
}
// Silences gives access to silences.
type Silences interface {
// The Silences provider must implement the Silencer interface
// for all its silences. The data provider may have access to an
// optimized view of the data to perform this evaluation.
Silencer
// All returns all existing silences.