1 // Copyright 2015 Prometheus Team 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package provider 15 16 import ( 17 "fmt" 18 19 "github.com/prometheus/common/model" 20 21 "github.com/prometheus/alertmanager/types" 22 ) 23 24 // ErrNotFound is returned if a provider cannot find a requested item. 25 var ErrNotFound = fmt.Errorf("item not found") 26 27 // Iterator provides the functions common to all iterators. To be useful, a 28 // specific iterator interface (e.g. AlertIterator) has to be implemented that 29 // provides a Next method. 30 type Iterator interface { 31 // Err returns the current error. It is not safe to call it concurrently 32 // with other iterator methods or while reading from a channel returned 33 // by the iterator. 34 Err() error 35 // Close must be called to release resources once the iterator is not 36 // used anymore. 37 Close() 38 } 39 40 // AlertIterator is an Iterator for Alerts. 41 type AlertIterator interface { 42 Iterator 43 // Next returns a channel that will be closed once the iterator is 44 // exhausted. It is not necessary to exhaust the iterator but Close must 45 // be called in any case to release resources used by the iterator (even 46 // if the iterator is exhausted). 47 Next() <-chan *types.Alert 48 } 49 50 // NewAlertIterator returns a new AlertIterator based on the generic alertIterator type 51 func NewAlertIterator(ch <-chan *types.Alert, done chan struct{}, err error) AlertIterator { 52 return &alertIterator{ 53 ch: ch, 54 done: done, 55 err: err, 56 } 57 } 58 59 // alertIterator implements AlertIterator. So far, this one fits all providers. 60 type alertIterator struct { 61 ch <-chan *types.Alert 62 done chan struct{} 63 err error 64 } 65 66 func (ai alertIterator) Next() <-chan *types.Alert { 67 return ai.ch 68 } 69 70 func (ai alertIterator) Err() error { return ai.err } 71 func (ai alertIterator) Close() { close(ai.done) } 72 73 // Alerts gives access to a set of alerts. All methods are goroutine-safe. 74 type Alerts interface { 75 // Subscribe returns an iterator over active alerts that have not been 76 // resolved and successfully notified about. 77 // They are not guaranteed to be in chronological order. 78 Subscribe() AlertIterator 79 // GetPending returns an iterator over all alerts that have 80 // pending notifications. 81 GetPending() AlertIterator 82 // Get returns the alert for a given fingerprint. 83 Get(model.Fingerprint) (*types.Alert, error) 84 // Put adds the given set of alerts to the set. 85 Put(...*types.Alert) error 86 } 87