Refactoring

This commit is contained in:
Holger Obermaier 2022-01-25 12:22:14 +01:00
parent 6bb214fc18
commit e6c636be90

View File

@ -105,20 +105,19 @@ func (r *metricRouter) EvalCondition(Cond string, point lp.CCMetric) (bool, erro
func (r *metricRouter) DoAddTags(point lp.CCMetric) { func (r *metricRouter) DoAddTags(point lp.CCMetric) {
for _, m := range r.config.AddTags { for _, m := range r.config.AddTags {
var res bool var conditionMatches bool
var err error
if m.Condition == "*" { if m.Condition == "*" {
res = true conditionMatches = true
err = nil
} else { } else {
res, err = r.EvalCondition(m.Condition, point) var err error
conditionMatches, err = r.EvalCondition(m.Condition, point)
if err != nil { if err != nil {
log.Print(err.Error()) log.Print(err.Error())
res = false conditionMatches = false
} }
} }
if res == true { if conditionMatches {
point.AddTag(m.Key, m.Value) point.AddTag(m.Key, m.Value)
} }
} }
@ -126,19 +125,19 @@ func (r *metricRouter) DoAddTags(point lp.CCMetric) {
func (r *metricRouter) DoDelTags(point lp.CCMetric) { func (r *metricRouter) DoDelTags(point lp.CCMetric) {
for _, m := range r.config.DelTags { for _, m := range r.config.DelTags {
var res bool var conditionMatches bool
var err error
if m.Condition == "*" { if m.Condition == "*" {
res = true conditionMatches = true
err = nil
} else { } else {
res, err = r.EvalCondition(m.Condition, point) var err error
conditionMatches, err = r.EvalCondition(m.Condition, point)
if err != nil { if err != nil {
log.Print(err.Error()) log.Print(err.Error())
res = false conditionMatches = false
} }
} }
if res == true { if conditionMatches {
point.RemoveTag(m.Key) point.RemoveTag(m.Key)
} }
} }
@ -147,7 +146,7 @@ func (r *metricRouter) DoDelTags(point lp.CCMetric) {
func (r *metricRouter) Start() { func (r *metricRouter) Start() {
r.wg.Add(1) r.wg.Add(1)
r.timestamp = time.Now() r.timestamp = time.Now()
if r.config.IntervalStamp == true { if r.config.IntervalStamp {
r.StartTimer() r.StartTimer()
} }
go func() { go func() {
@ -170,7 +169,7 @@ func (r *metricRouter) Start() {
log.Print("[MetricRouter] FORWARD ", p) log.Print("[MetricRouter] FORWARD ", p)
r.DoAddTags(p) r.DoAddTags(p)
r.DoDelTags(p) r.DoDelTags(p)
if r.config.IntervalStamp == true { if r.config.IntervalStamp {
p.SetTime(r.timestamp) p.SetTime(r.timestamp)
} }
for _, o := range r.outputs { for _, o := range r.outputs {
@ -200,7 +199,7 @@ func (r *metricRouter) Close() {
} }
func New(ticker mct.MultiChanTicker, wg *sync.WaitGroup, routerConfigFile string) (MetricRouter, error) { func New(ticker mct.MultiChanTicker, wg *sync.WaitGroup, routerConfigFile string) (MetricRouter, error) {
r := &metricRouter{} r := new(metricRouter)
err := r.Init(ticker, wg, routerConfigFile) err := r.Init(ticker, wg, routerConfigFile)
if err != nil { if err != nil {
return nil, err return nil, err