Skip to content

Commit

Permalink
feat: plugins can use raw or display metric in calculations (#1567)
Browse files Browse the repository at this point in the history
* feat: plugins can use raw or display metric in calculations

feat: add metric_agent percent operation
docs: add metric_agent percent operation to docs
fix: Histograms and arrays don't support display metrics yet
  • Loading branch information
cgrinds committed Dec 1, 2022
1 parent e4115eb commit 6b01afc
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 104 deletions.
3 changes: 1 addition & 2 deletions cmd/collectors/rest/plugins/qtree/qtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,12 @@ func (my *Qtree) Init() error {
for _, obj := range quotaMetric {
metricName, display, _, _ := util.ParseMetric(obj)

metric, err := my.data.NewMetricFloat64(metricName)
metric, err := my.data.NewMetricFloat64(metricName, display)
if err != nil {
my.Logger.Error().Stack().Err(err).Msg("add metric")
return err
}

metric.SetName(display)
my.Logger.Trace().Msgf("added metric: (%s) [%s] %s", metricName, display, metric)
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/collectors/rest/plugins/shelf/shelf.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,11 @@ func (my *Shelf) Init() error {
instanceLabels.NewChildS("", display)
my.Logger.Debug().Msgf("added instance label: (%s) [%s]", attribute, display)
case "float":
metric, err := my.data[attribute].NewMetricFloat64(metricName)
_, err := my.data[attribute].NewMetricFloat64(metricName, display)
if err != nil {
my.Logger.Error().Stack().Err(err).Msg("add metric")
return err
}
metric.SetName(display)
my.Logger.Debug().Msgf("added metric: (%s) [%s]", attribute, display)
}
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/collectors/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,14 @@ func (r *Rest) HandleResults(result []gjson.Result, prop *prop, isEndPoint bool)
for _, metric := range prop.Metrics {
metr, ok := mat.GetMetrics()[metric.Name]
if !ok {
if metr, err = mat.NewMetricFloat64(metric.Name); err != nil {
if metr, err = mat.NewMetricFloat64(metric.Name, metric.Label); err != nil {
r.Logger.Error().Err(err).
Str("name", metric.Name).
Msg("NewMetricFloat64")
}
}
f := instanceData.Get(metric.Name)
if f.Exists() {
metr.SetName(metric.Label)

var floatValue float64
switch metric.MetricType {
case "duration":
Expand Down
15 changes: 5 additions & 10 deletions cmd/collectors/restperf/restperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,12 @@ func (r *RestPerf) processWorkLoadCounter() (map[string]*matrix.Matrix, error) {
for name, metric := range r.Prop.Metrics {
metr, ok := mat.GetMetrics()[name]
if !ok {
if metr, err = mat.NewMetricFloat64(name); err != nil {
if metr, err = mat.NewMetricFloat64(name, metric.Label); err != nil {
r.Logger.Error().Err(err).
Str("name", name).
Msg("NewMetricFloat64")
}
}
metr.SetName(metric.Label)
metr.SetExportable(metric.Exportable)
}

Expand Down Expand Up @@ -445,7 +444,7 @@ func (r *RestPerf) processWorkLoadCounter() (map[string]*matrix.Matrix, error) {
if m := mat.GetMetric(name); m != nil {
continue
}
if m, err := mat.NewMetricFloat64(name); err != nil {
if m, err := mat.NewMetricFloat64(name, "resource_latency"); err != nil {
return nil, err
} else {
r.perfProp.counterInfo[name] = &counter{
Expand All @@ -455,7 +454,6 @@ func (r *RestPerf) processWorkLoadCounter() (map[string]*matrix.Matrix, error) {
unit: r.perfProp.counterInfo[service.GetName()].unit,
denominator: "ops",
}
m.SetName("resource_latency")
m.SetLabel("resource", resource)

r.Logger.Debug().Str("name", name).Str("resource", resource).Msg("added workload latency metric")
Expand Down Expand Up @@ -688,13 +686,12 @@ func (r *RestPerf) PollData() (map[string]*matrix.Matrix, error) {
if histogramMetric != nil {
r.Logger.Trace().Str("metric", key).Msg("Updating array metric attributes")
} else {
histogramMetric, err = curMat.NewMetricFloat64(key)
histogramMetric, err = curMat.NewMetricFloat64(key, metric.Label)
if err != nil {
r.Logger.Error().Err(err).Str("key", key).Msg("unable to create histogram metric")
continue
}
}
histogramMetric.SetName(metric.Label)
histogramMetric.SetArray(true)
histogramMetric.SetExportable(metric.Exportable)
histogramMetric.SetBuckets(&labels)
Expand All @@ -705,13 +702,12 @@ func (r *RestPerf) PollData() (map[string]*matrix.Matrix, error) {
k := name + arrayKeyToken + label
metr, ok := curMat.GetMetrics()[k]
if !ok {
if metr, err = curMat.NewMetricFloat64(k); err != nil {
if metr, err = curMat.NewMetricFloat64(k, metric.Label); err != nil {
r.Logger.Error().Err(err).
Str("name", k).
Msg("NewMetricFloat64")
continue
}
metr.SetName(metric.Label)
if x := strings.Split(label, arrayKeyToken); len(x) == 2 {
metr.SetLabel("metric", x[0])
metr.SetLabel("submetric", x[1])
Expand Down Expand Up @@ -751,14 +747,13 @@ func (r *RestPerf) PollData() (map[string]*matrix.Matrix, error) {
} else {
metr, ok := curMat.GetMetrics()[name]
if !ok {
if metr, err = curMat.NewMetricFloat64(name); err != nil {
if metr, err = curMat.NewMetricFloat64(name, metric.Label); err != nil {
r.Logger.Error().Err(err).
Str("name", name).
Int("instIndex", instIndex).
Msg("NewMetricFloat64")
}
}
metr.SetName(metric.Label)
metr.SetExportable(metric.Exportable)
if c, err := strconv.ParseFloat(f.value, 64); err == nil {
if err = metr.SetValueFloat64(instance, c); err != nil {
Expand Down
6 changes: 2 additions & 4 deletions cmd/collectors/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func (n *NodeMon) Init(a *collector.AbstractCollector) error {

func (n *NodeMon) loadMetrics(counters *node.Node) error {
var (
metric matrix.Metric
err error
err error
)

n.Logger.Debug().Msg("initializing metric cache")
Expand All @@ -70,10 +69,9 @@ func (n *NodeMon) loadMetrics(counters *node.Node) error {
dtype := "int64"
n.Logger.Trace().Msgf("handling (%s) (%s) dtype=%s", name, display, dtype)

if metric, err = mat.NewMetricType(name, dtype); err != nil {
if _, err = mat.NewMetricType(name, dtype, display); err != nil {
return err
}
metric.SetName(display)
n.Logger.Debug().Msgf("(%s) added metric (%s)", name, display)
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/collectors/storagegrid/storagegrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,14 @@ func (s *StorageGrid) handleResults(result []gjson.Result) uint64 {
for _, metric := range s.Props.Metrics {
metr, ok := mat.GetMetrics()[metric.Name]
if !ok {
if metr, err = mat.NewMetricFloat64(metric.Name); err != nil {
if metr, err = mat.NewMetricFloat64(metric.Name, metric.Label); err != nil {
s.Logger.Error().Err(err).
Str("name", metric.Name).
Msg("NewMetricFloat64")
}
}
f := instanceData.Get(metric.Name)
if f.Exists() {
metr.SetName(metric.Label)

var floatValue float64
switch metric.MetricType {
case "":
Expand Down
6 changes: 2 additions & 4 deletions cmd/collectors/unix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ func (u *Unix) loadMetrics(counters *node.Node) error {
// counter is scalar metric
if _, has := _Metrics[name]; has {

if metric, err = mat.NewMetricType(name, dtype); err != nil {
if _, err = mat.NewMetricType(name, dtype, display); err != nil {
return err
}
metric.SetName(display)
u.Logger.Debug().Msgf("(%s) added metric (%s)", name, display)

// counter is histogram
Expand Down Expand Up @@ -251,10 +250,9 @@ func (u *Unix) loadMetrics(counters *node.Node) error {
continue
}

if metric, err = mat.NewMetricType(name+"."+label, dtype); err != nil {
if metric, err = mat.NewMetricType(name+"."+label, dtype, name); err != nil {
return err
}
metric.SetName(name)
metric.SetLabel("metric", ldisplay)
u.histogramLabels[name] = append(u.histogramLabels[name], label)
}
Expand Down
12 changes: 5 additions & 7 deletions cmd/collectors/zapi/collector/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (z *Zapi) HandleCounter(path []string, content string) string {
var (
name, display, key string
splitValues, fullPath []string
metric matrix.Metric
err error
)

Expand All @@ -94,7 +93,6 @@ func (z *Zapi) HandleCounter(path []string, content string) string {

name = strings.TrimSpace(strings.TrimLeft(name, "^"))

//full_path = append(path[1:], name)
fullPath = append(path, name)
key = strings.Join(fullPath, ".")

Expand All @@ -115,16 +113,16 @@ func (z *Zapi) HandleCounter(path []string, content string) string {
} else {
// use user-defined metric type
if t := z.Params.GetChildContentS("metric_type"); t != "" {
metric, err = mat.NewMetricType(key, t)
_, err = mat.NewMetricType(key, t, display)
// use uint64 as default, since nearly all ZAPI counters are unsigned
} else {
metric, err = mat.NewMetricUint64(key)
_, err = mat.NewMetricUint64(key, display)
}
if err != nil {
z.Logger.Error().Stack().Err(err).Msgf("add as metric [%s]: %v", key, display)
z.Logger.Error().Err(err).Str("key", key).Str("display", display).Msg("Failed to add metric")
} else {
metric.SetName(display)
z.Logger.Trace().Msgf("%sadd as metric (%s) [%s]%s => %v", color.Blue, key, display, color.End, fullPath)
z.Logger.Trace().Str("key", key).Str("display", display).Strs("fullPath", fullPath).
Msg("Add metric")
}
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/collectors/zapi/plugins/qtree/qtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,12 @@ func (my *Qtree) Init() error {

metricName, display, _, _ := util.ParseMetric(obj)

metric, err := my.data.NewMetricFloat64(metricName)
metric, err := my.data.NewMetricFloat64(metricName, display)
if err != nil {
my.Logger.Error().Stack().Err(err).Msg("add metric")
return err
}

metric.SetName(display)
my.Logger.Debug().Msgf("added metric: (%s) [%s] %s", metricName, display, metric)
}

Expand Down
1 change: 1 addition & 0 deletions cmd/collectors/zapi/plugins/sensor/sensor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright NetApp Inc, 2021 All rights reserved
*/

package sensor

import (
Expand Down
3 changes: 1 addition & 2 deletions cmd/collectors/zapi/plugins/shelf/shelf.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ func (my *Shelf) Init() error {
instanceLabels.NewChildS("", display)
my.Logger.Debug().Msgf("added instance label: (%s) (%s) [%s]", attribute, x.GetNameS(), display)
case "float":
metric, err := my.data[attribute].NewMetricFloat64(metricName)
_, err := my.data[attribute].NewMetricFloat64(metricName, display)
if err != nil {
my.Logger.Error().Stack().Err(err).Msg("add metric")
return err
}
metric.SetName(display)
my.Logger.Debug().Msgf("added metric: (%s) (%s) [%s]", attribute, x.GetNameS(), display)
}
}
Expand Down
12 changes: 4 additions & 8 deletions cmd/collectors/zapiperf/zapiperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,9 @@ func (z *ZapiPerf) PollCounter() (map[string]*matrix.Matrix, error) {
oldMetrics.Remove(name)
continue
}
if m, err := mat.NewMetricFloat64(name); err != nil {
if m, err := mat.NewMetricFloat64(name, "resource_latency"); err != nil {
return nil, err
} else {
m.SetName("resource_latency")
m.SetLabel("resource", resource)
m.SetProperty(service.GetProperty())
// base counter is the ops of the same resource
Expand Down Expand Up @@ -1175,13 +1174,12 @@ func (z *ZapiPerf) addCounter(counter *node.Node, name, display string, enabled
if histogramMetric != nil {
z.Logger.Trace().Str("metric", key).Msg("Updating array metric attributes")
} else {
histogramMetric, err = mat.NewMetricFloat64(key)
histogramMetric, err = mat.NewMetricFloat64(key, display)
if err != nil {
z.Logger.Error().Err(err).Str("key", key).Msg("unable to create histogram metric")
return ""
}
}
histogramMetric.SetName(display)
histogramMetric.SetProperty(property)
histogramMetric.SetComment(baseKey)
histogramMetric.SetExportable(enabled)
Expand All @@ -1197,14 +1195,13 @@ func (z *ZapiPerf) addCounter(counter *node.Node, name, display string, enabled

if m = mat.GetMetric(key); m != nil {
z.Logger.Trace().Msgf("updating array metric [%s] attributes", key)
} else if m, err = mat.NewMetricFloat64(key); err == nil {
} else if m, err = mat.NewMetricFloat64(key, display); err == nil {
z.Logger.Trace().Msgf("%s+[%s] added array metric (%s), element with label (%s)%s", color.Pink, name, display, label, color.End)
} else {
z.Logger.Error().Err(err).Msgf("add array metric element [%s]: ", key)
return ""
}

m.SetName(display)
m.SetProperty(property)
m.SetComment(baseKey)
m.SetExportable(enabled)
Expand All @@ -1231,15 +1228,14 @@ func (z *ZapiPerf) addCounter(counter *node.Node, name, display string, enabled
var m matrix.Metric
if m = mat.GetMetric(name); m != nil {
z.Logger.Trace().Msgf("updating scalar metric [%s] attributes", name)
} else if m, err = mat.NewMetricFloat64(name); err == nil {
} else if m, err = mat.NewMetricFloat64(name, display); err == nil {
z.Logger.Trace().Msgf("%s+[%s] added scalar metric (%s)%s", color.Cyan, name, display, color.End)
} else {
z.Logger.Error().Err(err).Msgf("add scalar metric [%s]", name)
return ""
}

z.scalarCounters = append(z.scalarCounters, name)
m.SetName(display)
m.SetProperty(property)
m.SetComment(baseCounter)
m.SetExportable(enabled)
Expand Down
32 changes: 20 additions & 12 deletions cmd/poller/plugin/metricagent/metric_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ func (a *MetricAgent) computeMetrics(m *matrix.Matrix) error {

// map values for compute_metric mapping rules
for _, r := range a.computeMetricRules {

if metric = m.GetMetric(r.metric); metric == nil {
if metric = a.getMetric(m, r.metric); metric == nil {
if metric, err = m.NewMetricFloat64(r.metric); err != nil {
a.Logger.Error().Stack().Err(err).Str("new metric", r.metric).Msg("computeMetrics: failed to create metric")
a.Logger.Error().Err(err).Str("metric", r.metric).Msg("Failed to create metric")
return err
} else {
metric.SetProperty("compute_metric mapping")
Expand All @@ -76,7 +75,7 @@ func (a *MetricAgent) computeMetrics(m *matrix.Matrix) error {
var result float64

// Parse first operand and store in result for further processing
if firstMetricVal = m.GetMetric(r.metricNames[0]); firstMetricVal != nil {
if firstMetricVal = a.getMetric(m, r.metricNames[0]); firstMetricVal != nil {
if val, ok := firstMetricVal.GetValueFloat64(instance); ok {
result = val
} else {
Expand All @@ -92,7 +91,7 @@ func (a *MetricAgent) computeMetrics(m *matrix.Matrix) error {
if value, err := strconv.Atoi(r.metricNames[i]); err == nil {
v = float64(value)
} else {
metricVal = m.GetMetric(r.metricNames[i])
metricVal = a.getMetric(m, r.metricNames[i])
if metricVal != nil {
v, _ = metricVal.GetValueFloat64(instance)
} else {
Expand All @@ -112,16 +111,17 @@ func (a *MetricAgent) computeMetrics(m *matrix.Matrix) error {
if v != 0 {
result /= v
} else {
a.Logger.Error().
Str("operation", r.operation).
Msg("Division by zero operation")
a.Logger.Error().Str("operation", r.operation).Msg("Division by zero operation")
}
case "PERCENT":
if v != 0 {
result = (result / v) * 100
} else {
a.Logger.Error().Str("operation", r.operation).Msg("Division by zero operation")
}
default:
a.Logger.Warn().
Str("operation", r.operation).
Msg("Unknown operation")
a.Logger.Warn().Str("operation", r.operation).Msg("Unknown operation")
}

}

_ = metric.SetValueFloat64(instance, result)
Expand All @@ -130,3 +130,11 @@ func (a *MetricAgent) computeMetrics(m *matrix.Matrix) error {
}
return nil
}

func (a *MetricAgent) getMetric(m *matrix.Matrix, name string) matrix.Metric {
metric := m.DisplayMetric(name)
if metric != nil {
return metric
}
return m.GetMetric(name)
}
Loading

0 comments on commit 6b01afc

Please sign in to comment.