Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refractor matrix #1595

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cmd/collectors/restperf/restperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ func (r *RestPerf) PollData() (map[string]*matrix.Matrix, error) {

// calculate timestamp delta first since many counters require it for postprocessing.
// Timestamp has "raw" property, so it isn't post-processed automatically
if _, err = timestamp.Delta(prevMat.GetMetric("timestamp"), prevMat, curMat, r.Logger); err != nil {
if _, err = curMat.Delta("timestamp", prevMat, r.Logger); err != nil {
r.Logger.Error().Err(err).Msg("(timestamp) calculate delta:")
}

Expand All @@ -878,7 +878,7 @@ func (r *RestPerf) PollData() (map[string]*matrix.Matrix, error) {
}

// all other properties - first calculate delta
if skips, err = metric.Delta(prevMat.GetMetric(key), prevMat, curMat, r.Logger); err != nil {
if skips, err = curMat.Delta(key, prevMat, r.Logger); err != nil {
r.Logger.Error().Err(err).Str("key", key).Msg("Calculate delta")
continue
}
Expand Down Expand Up @@ -919,9 +919,9 @@ func (r *RestPerf) PollData() (map[string]*matrix.Matrix, error) {
if property == "average" || property == "percent" {

if strings.HasSuffix(metric.GetName(), "latency") {
skips, err = metric.DivideWithThreshold(base, r.perfProp.latencyIoReqd, r.Logger)
skips, err = curMat.DivideWithThreshold(key, counter.denominator, r.perfProp.latencyIoReqd, r.Logger)
} else {
skips, err = metric.Divide(base, r.Logger)
skips, err = curMat.Divide(key, counter.denominator, r.Logger)
}

if err != nil {
Expand All @@ -936,7 +936,7 @@ func (r *RestPerf) PollData() (map[string]*matrix.Matrix, error) {
}

if property == "percent" {
if skips, err = metric.MultiplyByScalar(100, r.Logger); err != nil {
if skips, err = curMat.MultiplyByScalar(key, 100, r.Logger); err != nil {
r.Logger.Error().Err(err).Str("key", key).Msg("Multiply by scalar")
} else {
totalSkips += skips
Expand All @@ -958,7 +958,7 @@ func (r *RestPerf) PollData() (map[string]*matrix.Matrix, error) {
if counter != nil {
property := counter.counterType
if property == "rate" {
if skips, err = metric.Divide(timestamp, r.Logger); err != nil {
if skips, err = curMat.Divide(orderedKeys[i], "timestamp", r.Logger); err != nil {
r.Logger.Error().Err(err).
Int("i", i).
Str("metric", metric.GetName()).
Expand Down
12 changes: 6 additions & 6 deletions cmd/collectors/zapiperf/zapiperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ func (z *ZapiPerf) PollData() (map[string]*matrix.Matrix, error) {

// calculate timestamp delta first since many counters require it for postprocessing.
// Timestamp has "raw" property, so it isn't post-processed automatically
if _, err = timestamp.Delta(prevMat.GetMetric("timestamp"), prevMat, curMat, z.Logger); err != nil {
if _, err = curMat.Delta("timestamp", prevMat, z.Logger); err != nil {
z.Logger.Error().Err(err).Msg("(timestamp) calculate delta:")
// @TODO terminate since other counters will be incorrect
}
Expand All @@ -590,7 +590,7 @@ func (z *ZapiPerf) PollData() (map[string]*matrix.Matrix, error) {
}

// all other properties - first calculate delta
if skips, err = metric.Delta(prevMat.GetMetric(key), prevMat, curMat, z.Logger); err != nil {
if skips, err = curMat.Delta(key, prevMat, z.Logger); err != nil {
z.Logger.Error().Err(err).Str("key", key).Msg("Calculate delta")
continue
}
Expand Down Expand Up @@ -631,9 +631,9 @@ func (z *ZapiPerf) PollData() (map[string]*matrix.Matrix, error) {
if property == "average" || property == "percent" {

if strings.HasSuffix(metric.GetName(), "latency") {
skips, err = metric.DivideWithThreshold(base, z.latencyIoReqd, z.Logger)
skips, err = curMat.DivideWithThreshold(key, metric.GetComment(), z.latencyIoReqd, z.Logger)
} else {
skips, err = metric.Divide(base, z.Logger)
skips, err = curMat.Divide(key, metric.GetComment(), z.Logger)
}

if err != nil {
Expand All @@ -647,7 +647,7 @@ func (z *ZapiPerf) PollData() (map[string]*matrix.Matrix, error) {
}

if property == "percent" {
if skips, err = metric.MultiplyByScalar(100, z.Logger); err != nil {
if skips, err = curMat.MultiplyByScalar(key, 100, z.Logger); err != nil {
z.Logger.Error().Err(err).Str("key", key).Msg("Multiply by scalar")
} else {
totalSkips += skips
Expand All @@ -663,7 +663,7 @@ func (z *ZapiPerf) PollData() (map[string]*matrix.Matrix, error) {
// calculate rates (which we deferred to calculate averages/percents first)
for i, metric := range orderedMetrics {
if metric.GetProperty() == "rate" {
if skips, err = metric.Divide(timestamp, z.Logger); err != nil {
if skips, err = curMat.Divide(orderedKeys[i], "timestamp", z.Logger); err != nil {
z.Logger.Error().Err(err).
Int("i", i).
Str("key", orderedKeys[i]).
Expand Down
167 changes: 167 additions & 0 deletions pkg/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"
"github.com/netapp/harvest/v2/pkg/dict"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/logging"
"github.com/netapp/harvest/v2/pkg/tree/node"
"strings"
)
Expand Down Expand Up @@ -303,3 +304,169 @@ func CreateMetric(key string, data *Matrix) error {
}
return nil
}

// Delta vector arithmetics
func (m *Matrix) Delta(metricKey string, prevMat *Matrix, logger *logging.Logger) (int, error) {
var skips int
prevMetric := prevMat.GetMetric(metricKey)
curMetric := m.GetMetric(metricKey)
prevRaw := prevMetric.values
prevRecord := prevMetric.GetRecords()
for key, currInstance := range m.GetInstances() {
// check if this instance key exists in previous matrix
prevInstance := prevMat.GetInstance(key)
currIndex := currInstance.index
curRaw := curMetric.values[currIndex]
if prevInstance != nil {
prevIndex := prevInstance.index
if curMetric.record[currIndex] && prevRecord[prevIndex] {
curMetric.values[currIndex] -= prevRaw[prevIndex]
// Sometimes ONTAP sends spurious zeroes or values less than the previous poll.
// Detect and don't publish negative deltas or the subsequent poll will show a large spike.
isInvalidZero := (curRaw == 0 || prevRaw[prevIndex] == 0) && curMetric.values[prevIndex] != 0
isNegative := curMetric.values[currIndex] < 0
if isInvalidZero || isNegative {
curMetric.record[currIndex] = false
skips++
logger.Trace().
Str("metric", curMetric.GetName()).
Float64("currentRaw", curRaw).
Float64("previousRaw", prevRaw[prevIndex]).
Str("instKey", key).
Msg("Negative cooked value")
}
} else {
curMetric.record[currIndex] = false
skips++
logger.Trace().
Str("metric", curMetric.GetName()).
Float64("currentRaw", curRaw).
Float64("previousRaw", prevRaw[prevIndex]).
Str("instKey", key).
Msg("Delta calculation skipped")
}
} else {
curMetric.record[currIndex] = false
skips++
logger.Trace().
Str("metric", curMetric.GetName()).
Float64("currentRaw", curRaw).
Str("instKey", key).
Msg("New instance added")
}
}
return skips, nil
}

func (m *Matrix) Divide(metricKey string, baseKey string, logger *logging.Logger) (int, error) {
var skips int
metric := m.GetMetric(metricKey)
base := m.GetMetric(baseKey)
sValues := base.values
sRecord := base.GetRecords()
if len(metric.values) != len(sValues) {
return 0, errs.New(ErrUnequalVectors, fmt.Sprintf("numerator=%d, denominator=%d", len(metric.values), len(sValues)))
}
for i := 0; i < len(metric.values); i++ {
if metric.record[i] && sRecord[i] {
if sValues[i] != 0 {
// Don't pass along the value if the numerator or denominator is < 0
// A denominator of zero is fine
if metric.values[i] < 0 || sValues[i] < 0 {
metric.record[i] = false
skips++
logger.Trace().
Str("metric", metric.GetName()).
Float64("numerator", metric.values[i]).
Float64("denominator", sValues[i]).
Msg("Divide calculation skipped")
}
metric.values[i] /= sValues[i]
} else {
metric.values[i] = 0
}
} else {
metric.record[i] = false
skips++
logger.Trace().
Str("metric", metric.GetName()).
Float64("numerator", metric.values[i]).
Float64("denominator", sValues[i]).
Msg("Divide calculation skipped")
}
}
return skips, nil
}

func (m *Matrix) DivideWithThreshold(metricKey string, baseKey string, threshold int, logger *logging.Logger) (int, error) {
var skips int
x := float64(threshold)
metric := m.GetMetric(metricKey)
base := m.GetMetric(baseKey)
sValues := base.values
sRecord := base.GetRecords()
if len(metric.values) != len(sValues) {
return 0, errs.New(ErrUnequalVectors, fmt.Sprintf("numerator=%d, denominator=%d", len(metric.values), len(sValues)))
}
for i := 0; i < len(metric.values); i++ {
v := metric.values[i]
// Don't pass along the value if the numerator or denominator is < 0
// It is important to check sValues[i] < 0 and allow a zero so pass=true and m.values[i] remains unchanged
if metric.values[i] < 0 || sValues[i] < 0 {
metric.record[i] = false
skips++
logger.Trace().
Str("metric", metric.GetName()).
Float64("numerator", v).
Float64("denominator", sValues[i]).
Msg("Negative values")
return skips, nil
}
if metric.record[i] && sRecord[i] {
if sValues[i] >= x {
metric.values[i] /= sValues[i]
} else {
metric.values[i] = 0
}
} else {
metric.record[i] = false
skips++
logger.Trace().
Str("metric", metric.GetName()).
Float64("numerator", metric.values[i]).
Float64("denominator", sValues[i]).
Msg("Divide threshold calculation skipped")
}
}
return skips, nil
}

func (m *Matrix) MultiplyByScalar(metricKey string, s uint, logger *logging.Logger) (int, error) {
var skips int
x := float64(s)
metric := m.GetMetric(metricKey)
for i := 0; i < len(metric.values); i++ {
if metric.record[i] {
// if current is <= 0
if metric.values[i] < 0 {
metric.record[i] = false
skips++
logger.Trace().
Str("metric", metric.GetName()).
Float64("currentRaw", metric.values[i]).
Uint("scalar", s).
Msg("Negative value")
}
metric.values[i] *= x
} else {
metric.record[i] = false
skips++
logger.Trace().
Str("metric", metric.GetName()).
Float64("currentRaw", metric.values[i]).
Uint("scalar", s).
Msg("Scalar multiplication skipped")
}
}
return skips, nil
}
Loading