func()

in clusterloader2/pkg/measurement/common/generic_query_measurement.go [135:182]


func (g *genericQueryGatherer) Gather(executor QueryExecutor, startTime, endTime time.Time, config *measurement.Config) ([]measurement.Summary, error) {
	var errs []error
	dataItems := map[string]*measurementutil.DataItem{}
	for _, q := range g.Queries {
		samples, err := g.query(q, executor, startTime, endTime)
		if err != nil {
			return nil, err
		}

		if len(samples) == 0 {
			if q.RequireSamples {
				errs = append(errs, errors.NewMetricViolationError(q.Name, fmt.Sprintf("query returned no samples for %v", g.MetricName)))
			}
			klog.Warningf("query returned no samples for %v: %v", g.MetricName, q.Name)
			continue
		}

		for _, sample := range samples {
			k, labels := key(sample.Metric, g.Dimensions)
			dataItem := getOrCreate(dataItems, k, g.Unit, labels)

			val := float64(sample.Value)
			prevVal, ok := dataItem.Data[q.Name]
			if ok {
				errs = append(errs, errors.NewMetricViolationError(q.Name, fmt.Sprintf("too many samples for %s: query returned %v and %v, expected single value.", k, val, prevVal)))
			} else {
				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)))
			}
		}
	}
	summary, err := g.createSummary(g.MetricName, dataItems)
	if err != nil {
		return nil, err
	}
	if len(errs) > 0 {
		err = errors.NewMetricViolationError(g.MetricName, fmt.Sprintf("%v", errs))
	}
	return []measurement.Summary{summary}, err
}