Skip to content

Commit

Permalink
Merge pull request kubernetes#2298 from tosi3k/generic
Browse files Browse the repository at this point in the history
Enable treating threshold as a lower bound in generic measurement
  • Loading branch information
k8s-ci-robot authored Jul 14, 2023
2 parents 1ba68e4 + 6f7a882 commit edb4057
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
30 changes: 23 additions & 7 deletions clusterloader2/pkg/measurement/common/generic_query_measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type GenericQuery struct {
Name string
Query string
Threshold *float64
LowerBound bool
RequireSamples bool
}

Expand Down Expand Up @@ -132,6 +133,26 @@ func getOrCreate(dataItems map[string]*measurementutil.DataItem, key, unit strin
return dataItem
}

func (g *genericQueryGatherer) validateSample(q GenericQuery, val float64) error {
thresholdMsg := "none"
if q.Threshold != nil {
thresholdMsg = fmt.Sprintf("%v (upper bound)", *q.Threshold)
if q.LowerBound {
thresholdMsg = fmt.Sprintf("%v (lower bound)", *q.Threshold)
}
}
klog.V(2).Infof("metric: %v: %v, value: %v, threshold: %v", g.MetricName, q.Name, val, thresholdMsg)
if q.Threshold != nil {
if q.LowerBound && val < *q.Threshold {
return errors.NewMetricViolationError(q.Name, fmt.Sprintf("sample below threshold: want: greater or equal than %v, got: %v", *q.Threshold, val))
}
if !q.LowerBound && val > *q.Threshold {
return errors.NewMetricViolationError(q.Name, fmt.Sprintf("sample above threshold: want: less or equal than %v, got: %v", *q.Threshold, val))
}
}
return nil
}

func (g *genericQueryGatherer) Gather(executor QueryExecutor, startTime, endTime time.Time, config *measurement.Config) ([]measurement.Summary, error) {
var errs []error
dataItems := map[string]*measurementutil.DataItem{}
Expand Down Expand Up @@ -161,13 +182,8 @@ func (g *genericQueryGatherer) Gather(executor QueryExecutor, startTime, endTime
dataItem.Data[q.Name] = val
}

thresholdMsg := "none"
if q.Threshold != nil {
thresholdMsg = fmt.Sprintf("%v", *q.Threshold)
}
klog.V(2).Infof("metric: %v: %v, value: %v, threshold: %v", g.MetricName, q.Name, val, thresholdMsg)
if q.Threshold != nil && val > *q.Threshold {
errs = append(errs, errors.NewMetricViolationError(q.Name, fmt.Sprintf("sample above threshold: want: less or equal than %v, got: %v", *q.Threshold, val)))
if err := g.validateSample(q, val); err != nil {
errs = append(errs, err)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,61 @@ func TestGather(t *testing.T) {
},
},
},
{
desc: "sample above lower bound",
params: map[string]interface{}{
"metricName": "below-threshold",
"metricVersion": "v1",
"unit": "ms",
"queries": []map[string]interface{}{
{
"name": "above-threshold",
"query": "above-threshold-query[%v]",
"threshold": 60,
"lowerBound": true,
},
},
},
samples: map[string][]*model.Sample{
"above-threshold-query[60s]": {{Value: model.SampleValue(74)}},
},
wantDataItems: []measurementutil.DataItem{
{
Unit: "ms",
Data: map[string]float64{
"above-threshold": 74.0,
},
},
},
},
{
desc: "sample below lower bound",
params: map[string]interface{}{
"metricName": "below-threshold",
"metricVersion": "v1",
"unit": "ms",
"queries": []map[string]interface{}{
{
"name": "below-threshold",
"query": "below-threshold-query[%v]",
"threshold": 60,
"lowerBound": true,
},
},
},
samples: map[string][]*model.Sample{
"below-threshold-query[60s]": {{Value: model.SampleValue(42)}},
},
wantErr: "sample below threshold: want: greater or equal than 60, got: 42",
wantDataItems: []measurementutil.DataItem{
{
Unit: "ms",
Data: map[string]float64{
"below-threshold": 42.0,
},
},
},
},
{
desc: "missing field metricName",
params: map[string]interface{}{
Expand Down

0 comments on commit edb4057

Please sign in to comment.