Example #1
0
// New creates a alerter.
func New(cfg *config.Config, db *storage.DB) *Alerter {
	al := new(Alerter)
	al.cfg = cfg
	al.db = db
	al.In = make(chan *models.Event, bufferedMetricResultsLimit)
	al.m = safemap.New()
	al.c = safemap.New()
	return al
}
Example #2
0
// New creates a filter.
func New() *Filter {
	return &Filter{
		addRuleCh:        make(chan *models.Rule, bufferedChangedRulesLimit),
		delRuleCh:        make(chan *models.Rule, bufferedChangedRulesLimit),
		children:         safemap.New(),
		hitCounters:      safemap.New(),
		intervalHitLimit: 100,
	}
}
Example #3
0
// New creates a new Alerter.
func New(cfg *config.Config, db *storage.DB) *Alerter {
	return &Alerter{
		cfg:          cfg,
		db:           db,
		In:           make(chan *models.Event, bufferedEventsLimit),
		alertAts:     safemap.New(),
		alertNums:    safemap.New(),
		alertRecords: safemap.New(),
		lock:         &sync.RWMutex{},
	}
}
Example #4
0
// newRulesCache creates a rulesCache.
func newRulesCache() *rulesCache {
	c := new(rulesCache)
	c.rules = safemap.New()
	c.lnsAdd = make([]chan *models.Rule, 0)
	c.lnsDel = make([]chan *models.Rule, 0)
	return c
}
Example #5
0
// addRule add a rule to the suffix tree
func (f *Filter) addRule(rule *models.Rule) {
	//split the rule.Pattern into ordered words
	l := strings.Split(rule.Pattern, ".")
	//if suffix tree root do not has the same node, add it
	if !f.children.Has(l[0]) {
		f.children.Set(l[0], newChildFilter())
	}
	//check if suffix has the same word of the pattern by level step, if not add it
	v, _ := f.children.Get(l[0])
	ch := v.(*childFilter)
	l = l[1:]
	for len(l) > 0 {
		if ch.children == nil {
			ch.children = safemap.New()
		}
		if ch.children.Has(l[0]) {
			v, _ = ch.children.Get(l[0])
			ch = v.(*childFilter)
		} else {
			ch.children.Set(l[0], newChildFilter())
			v, _ = ch.children.Get(l[0])
			ch = v.(*childFilter)
		}
		l = l[1:]
	}
	ch.lock.Lock()
	defer ch.lock.Unlock()
	ch.matchedRules = append(ch.matchedRules, rule)
}
Example #6
0
// New creates a filter.
func New() *Filter {
	return &Filter{
		addRuleCh: make(chan *models.Rule, bufferedChangedRulesLimit),
		delRuleCh: make(chan *models.Rule, bufferedChangedRulesLimit),
		children:  safemap.New(),
	}
}
Example #7
0
// New creates a alerter.
func New(cfg *config.Config, db *storage.DB, filter *filter.Filter) *Alerter {
	al := new(Alerter)
	al.cfg = cfg
	al.db = db
	al.filter = filter
	al.In = make(chan *models.Metric, bufferedMetricResultsLimit)
	al.m = safemap.New()
	return al
}
Example #8
0
func TestAlertRecordAlertNotifyAfterConfigDisabled(t *testing.T) {
	cfg := config.New()
	cfg.Alerter.NotifyAfter = 0
	a := &Alerter{cfg: cfg, alertRecords: safemap.New(), lock: &sync.RWMutex{}}
	metrics := &models.Metric{Name: "test", Stamp: 0, Value: 80}

	for i := 0; i <= 100; i++ {
		metrics.Stamp = uint32(i)
		util.Must(t, !a.checkAlertCount(metrics))
		a.setAlertRecord(metrics)
	}
}
Example #9
0
// Open a DB by fileName.
func Open(fileName string) (*DB, error) {
	ldb, err := leveldb.OpenFile(fileName, nil)
	if err != nil {
		return nil, err
	}
	m := safemap.New()
	db := new(DB)
	db.db = ldb
	db.m = m
	db.load()
	return db, nil
}
Example #10
0
func TestAlertRecordAlertNotifyAfterConfigSetNotifyAfterToOne(t *testing.T) {
	cfg := config.New()
	cfg.Alerter.NotifyAfter = 1
	a := &Alerter{cfg: cfg, alertRecords: safemap.New(), lock: &sync.RWMutex{}}

	metrics := &models.Metric{Name: "test", Stamp: 80, Value: 80}
	util.Must(t, a.checkAlertCount(metrics))
	a.setAlertRecord(metrics)

	metrics.Stamp = 81
	util.Must(t, !a.checkAlertCount(metrics))
	a.setAlertRecord(metrics)
}