mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-11-14 05:57:25 +01:00
Use the MetricAggregator for all calculations in the MetricRouter
This commit is contained in:
parent
64a12b80bb
commit
a4bd141786
@ -3,6 +3,7 @@ package metricRouter
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -84,7 +85,7 @@ func (c *metricAggregator) Init(output chan lp.CCMetric) error {
|
|||||||
c.constants["smtWidth"] = cinfo.SMTWidth
|
c.constants["smtWidth"] = cinfo.SMTWidth
|
||||||
|
|
||||||
c.language = gval.NewLanguage(
|
c.language = gval.NewLanguage(
|
||||||
gval.Base(),
|
gval.Full(),
|
||||||
metricCacheLanguage,
|
metricCacheLanguage,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -281,6 +282,85 @@ func (c *metricAggregator) AddFunction(name string, function func(args ...interf
|
|||||||
c.language = gval.NewLanguage(c.language, gval.Function(name, function))
|
c.language = gval.NewLanguage(c.language, gval.Function(name, function))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EvalBoolCondition(condition string, params map[string]interface{}) (bool, error) {
|
||||||
|
newcond := strings.ReplaceAll(condition, "'", "\"")
|
||||||
|
newcond = strings.ReplaceAll(newcond, "%", "\\")
|
||||||
|
language := gval.NewLanguage(
|
||||||
|
gval.Full(),
|
||||||
|
metricCacheLanguage,
|
||||||
|
)
|
||||||
|
value, err := gval.Evaluate(newcond, params, language)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
var endResult bool = false
|
||||||
|
err = nil
|
||||||
|
switch r := value.(type) {
|
||||||
|
case bool:
|
||||||
|
endResult = r
|
||||||
|
case float64:
|
||||||
|
if r != 0.0 {
|
||||||
|
endResult = true
|
||||||
|
}
|
||||||
|
case float32:
|
||||||
|
if r != 0.0 {
|
||||||
|
endResult = true
|
||||||
|
}
|
||||||
|
case int:
|
||||||
|
if r != 0 {
|
||||||
|
endResult = true
|
||||||
|
}
|
||||||
|
case int64:
|
||||||
|
if r != 0 {
|
||||||
|
endResult = true
|
||||||
|
}
|
||||||
|
case int32:
|
||||||
|
if r != 0 {
|
||||||
|
endResult = true
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("cannot evaluate '%s' to bool", newcond)
|
||||||
|
}
|
||||||
|
return endResult, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func EvalFloat64Condition(condition string, params map[string]interface{}) (float64, error) {
|
||||||
|
var endResult float64 = math.NaN()
|
||||||
|
newcond := strings.ReplaceAll(condition, "'", "\"")
|
||||||
|
newcond = strings.ReplaceAll(newcond, "%", "\\")
|
||||||
|
language := gval.NewLanguage(
|
||||||
|
gval.Full(),
|
||||||
|
metricCacheLanguage,
|
||||||
|
)
|
||||||
|
value, err := gval.Evaluate(newcond, params, language)
|
||||||
|
if err != nil {
|
||||||
|
cclog.ComponentDebug("MetricRouter", condition, " = ", err.Error())
|
||||||
|
return endResult, err
|
||||||
|
}
|
||||||
|
err = nil
|
||||||
|
switch r := value.(type) {
|
||||||
|
case bool:
|
||||||
|
if r {
|
||||||
|
endResult = 1.0
|
||||||
|
} else {
|
||||||
|
endResult = 0.0
|
||||||
|
}
|
||||||
|
case float64:
|
||||||
|
endResult = r
|
||||||
|
case float32:
|
||||||
|
endResult = float64(r)
|
||||||
|
case int:
|
||||||
|
endResult = float64(r)
|
||||||
|
case int64:
|
||||||
|
endResult = float64(r)
|
||||||
|
case int32:
|
||||||
|
endResult = float64(r)
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("cannot evaluate '%s' to float64", newcond)
|
||||||
|
}
|
||||||
|
return endResult, err
|
||||||
|
}
|
||||||
|
|
||||||
func NewAggregator(output chan lp.CCMetric) (MetricAggregator, error) {
|
func NewAggregator(output chan lp.CCMetric) (MetricAggregator, error) {
|
||||||
a := new(metricAggregator)
|
a := new(metricAggregator)
|
||||||
err := a.Init(output)
|
err := a.Init(output)
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
|
|
||||||
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
||||||
mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker"
|
mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker"
|
||||||
"gopkg.in/Knetic/govaluate.v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Metric router tag configuration
|
// Metric router tag configuration
|
||||||
@ -26,8 +25,12 @@ type metricRouterConfig struct {
|
|||||||
AddTags []metricRouterTagConfig `json:"add_tags"` // List of tags that are added when the condition is met
|
AddTags []metricRouterTagConfig `json:"add_tags"` // List of tags that are added when the condition is met
|
||||||
DelTags []metricRouterTagConfig `json:"delete_tags"` // List of tags that are removed when the condition is met
|
DelTags []metricRouterTagConfig `json:"delete_tags"` // List of tags that are removed when the condition is met
|
||||||
IntervalAgg []metricAggregatorIntervalConfig `json:"interval_aggregates"` // List of aggregation function processed at the end of an interval
|
IntervalAgg []metricAggregatorIntervalConfig `json:"interval_aggregates"` // List of aggregation function processed at the end of an interval
|
||||||
|
DropMetrics []string `json:"drop_metrics"` // List of metric names to drop. For fine-grained dropping use drop_metrics_if
|
||||||
|
DropMetricsIf []string `json:"drop_metrics_if"` // List of evaluatable terms to drop metrics
|
||||||
|
RenameMetrics map[string]string `json:"rename_metrics"` // Map to rename metric name from key to value
|
||||||
IntervalStamp bool `json:"interval_timestamp"` // Update timestamp periodically by ticker each interval?
|
IntervalStamp bool `json:"interval_timestamp"` // Update timestamp periodically by ticker each interval?
|
||||||
NumCacheIntervals int `json:"num_cache_intervals"` // Number of intervals of cached metrics for evaluation
|
NumCacheIntervals int `json:"num_cache_intervals"` // Number of intervals of cached metrics for evaluation
|
||||||
|
dropMetrics map[string]bool // Internal map for O(1) lookup
|
||||||
}
|
}
|
||||||
|
|
||||||
// Metric router data structure
|
// Metric router data structure
|
||||||
@ -104,6 +107,10 @@ func (r *metricRouter) Init(ticker mct.MultiChanTicker, wg *sync.WaitGroup, rout
|
|||||||
for _, agg := range r.config.IntervalAgg {
|
for _, agg := range r.config.IntervalAgg {
|
||||||
r.cache.AddAggregation(agg.Name, agg.Function, agg.Condition, agg.Tags, agg.Meta)
|
r.cache.AddAggregation(agg.Name, agg.Function, agg.Condition, agg.Tags, agg.Meta)
|
||||||
}
|
}
|
||||||
|
r.config.dropMetrics = make(map[string]bool)
|
||||||
|
for _, mname := range r.config.DropMetrics {
|
||||||
|
r.config.dropMetrics[mname] = true
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,16 +137,9 @@ func (r *metricRouter) StartTimer() {
|
|||||||
cclog.ComponentDebug("MetricRouter", "TIMER START")
|
cclog.ComponentDebug("MetricRouter", "TIMER START")
|
||||||
}
|
}
|
||||||
|
|
||||||
// EvalCondition evaluates condition cond for metric data from point
|
func getParamMap(point lp.CCMetric) map[string]interface{} {
|
||||||
func (r *metricRouter) EvalCondition(cond string, point lp.CCMetric) (bool, error) {
|
|
||||||
expression, err := govaluate.NewEvaluableExpression(cond)
|
|
||||||
if err != nil {
|
|
||||||
cclog.ComponentDebug("MetricRouter", cond, " = ", err.Error())
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add metric name, tags, meta data, fields and timestamp to the parameter list
|
|
||||||
params := make(map[string]interface{})
|
params := make(map[string]interface{})
|
||||||
|
params["metric"] = point
|
||||||
params["name"] = point.Name()
|
params["name"] = point.Name()
|
||||||
for key, value := range point.Tags() {
|
for key, value := range point.Tags() {
|
||||||
params[key] = value
|
params[key] = value
|
||||||
@ -151,26 +151,19 @@ func (r *metricRouter) EvalCondition(cond string, point lp.CCMetric) (bool, erro
|
|||||||
params[f.Key] = f.Value
|
params[f.Key] = f.Value
|
||||||
}
|
}
|
||||||
params["timestamp"] = point.Time()
|
params["timestamp"] = point.Time()
|
||||||
|
return params
|
||||||
// evaluate condition
|
|
||||||
result, err := expression.Evaluate(params)
|
|
||||||
if err != nil {
|
|
||||||
cclog.ComponentDebug("MetricRouter", cond, " = ", err.Error())
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return bool(result.(bool)), err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoAddTags adds a tag when condition is fullfiled
|
// DoAddTags adds a tag when condition is fullfiled
|
||||||
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 conditionMatches bool
|
var conditionMatches bool = false
|
||||||
|
|
||||||
if m.Condition == "*" {
|
if m.Condition == "*" {
|
||||||
conditionMatches = true
|
conditionMatches = true
|
||||||
} else {
|
} else {
|
||||||
var err error
|
var err error
|
||||||
conditionMatches, err = r.EvalCondition(m.Condition, point)
|
conditionMatches, err = EvalBoolCondition(m.Condition, getParamMap(point))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cclog.ComponentError("MetricRouter", err.Error())
|
cclog.ComponentError("MetricRouter", err.Error())
|
||||||
conditionMatches = false
|
conditionMatches = false
|
||||||
@ -185,13 +178,13 @@ func (r *metricRouter) DoAddTags(point lp.CCMetric) {
|
|||||||
// DoDelTags removes a tag when condition is fullfiled
|
// DoDelTags removes a tag when condition is fullfiled
|
||||||
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 conditionMatches bool
|
var conditionMatches bool = false
|
||||||
|
|
||||||
if m.Condition == "*" {
|
if m.Condition == "*" {
|
||||||
conditionMatches = true
|
conditionMatches = true
|
||||||
} else {
|
} else {
|
||||||
var err error
|
var err error
|
||||||
conditionMatches, err = r.EvalCondition(m.Condition, point)
|
conditionMatches, err = EvalBoolCondition(m.Condition, getParamMap(point))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cclog.ComponentError("MetricRouter", err.Error())
|
cclog.ComponentError("MetricRouter", err.Error())
|
||||||
conditionMatches = false
|
conditionMatches = false
|
||||||
@ -203,9 +196,24 @@ func (r *metricRouter) DoDelTags(point lp.CCMetric) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Conditional test whether a metric should be dropped
|
||||||
|
func (r *metricRouter) dropMetric(point lp.CCMetric) bool {
|
||||||
|
// Simple drop check
|
||||||
|
if _, ok := r.config.dropMetrics[point.Name()]; ok {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// Checking the dropping conditions
|
||||||
|
for _, m := range r.config.DropMetricsIf {
|
||||||
|
conditionMatches, err := EvalBoolCondition(m, getParamMap(point))
|
||||||
|
if conditionMatches || err != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// Start starts the metric router
|
// Start starts the metric router
|
||||||
func (r *metricRouter) Start() {
|
func (r *metricRouter) Start() {
|
||||||
|
|
||||||
// start timer if configured
|
// start timer if configured
|
||||||
r.timestamp = time.Now()
|
r.timestamp = time.Now()
|
||||||
if r.config.IntervalStamp {
|
if r.config.IntervalStamp {
|
||||||
@ -224,6 +232,12 @@ func (r *metricRouter) Start() {
|
|||||||
cclog.ComponentDebug("MetricRouter", "FORWARD", point)
|
cclog.ComponentDebug("MetricRouter", "FORWARD", point)
|
||||||
r.DoAddTags(point)
|
r.DoAddTags(point)
|
||||||
r.DoDelTags(point)
|
r.DoDelTags(point)
|
||||||
|
if new, ok := r.config.RenameMetrics[point.Name()]; ok {
|
||||||
|
point.SetName(new)
|
||||||
|
}
|
||||||
|
r.DoAddTags(point)
|
||||||
|
r.DoDelTags(point)
|
||||||
|
|
||||||
for _, o := range r.outputs {
|
for _, o := range r.outputs {
|
||||||
o <- point
|
o <- point
|
||||||
}
|
}
|
||||||
@ -247,7 +261,11 @@ func (r *metricRouter) Start() {
|
|||||||
if r.config.IntervalStamp {
|
if r.config.IntervalStamp {
|
||||||
p.SetTime(r.timestamp)
|
p.SetTime(r.timestamp)
|
||||||
}
|
}
|
||||||
|
if !r.dropMetric(p) {
|
||||||
forward(p)
|
forward(p)
|
||||||
|
}
|
||||||
|
// even if the metric is dropped, it is stored in the cache for
|
||||||
|
// aggregations
|
||||||
r.cache.Add(p)
|
r.cache.Add(p)
|
||||||
|
|
||||||
case p := <-r.recv_input:
|
case p := <-r.recv_input:
|
||||||
@ -255,14 +273,18 @@ func (r *metricRouter) Start() {
|
|||||||
if r.config.IntervalStamp {
|
if r.config.IntervalStamp {
|
||||||
p.SetTime(r.timestamp)
|
p.SetTime(r.timestamp)
|
||||||
}
|
}
|
||||||
|
if !r.dropMetric(p) {
|
||||||
forward(p)
|
forward(p)
|
||||||
|
}
|
||||||
|
|
||||||
case p := <-r.cache_input:
|
case p := <-r.cache_input:
|
||||||
// receive from metric collector
|
// receive from metric collector
|
||||||
|
if !r.dropMetric(p) {
|
||||||
p.AddTag("hostname", r.hostname)
|
p.AddTag("hostname", r.hostname)
|
||||||
forward(p)
|
forward(p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
cclog.ComponentDebug("MetricRouter", "STARTED")
|
cclog.ComponentDebug("MetricRouter", "STARTED")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user