mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
k8s.io/component-base/metrics/testutil/:improve code
This commit is contained in:
parent
e7de96e635
commit
807a7e2426
@ -43,8 +43,8 @@ type Metrics map[string]model.Samples
|
|||||||
|
|
||||||
// Equal returns true if all metrics are the same as the arguments.
|
// Equal returns true if all metrics are the same as the arguments.
|
||||||
func (m *Metrics) Equal(o Metrics) bool {
|
func (m *Metrics) Equal(o Metrics) bool {
|
||||||
leftKeySet := []string{}
|
var leftKeySet []string
|
||||||
rightKeySet := []string{}
|
var rightKeySet []string
|
||||||
for k := range *m {
|
for k := range *m {
|
||||||
leftKeySet = append(leftKeySet, k)
|
leftKeySet = append(leftKeySet, k)
|
||||||
}
|
}
|
||||||
@ -207,22 +207,22 @@ func GetHistogramFromGatherer(gatherer metrics.Gatherer, metricName string) (His
|
|||||||
return Histogram{}, err
|
return Histogram{}, err
|
||||||
}
|
}
|
||||||
for _, mFamily := range m {
|
for _, mFamily := range m {
|
||||||
if mFamily.Name != nil && *mFamily.Name == metricName {
|
if mFamily.GetName() == metricName {
|
||||||
metricFamily = mFamily
|
metricFamily = mFamily
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if metricFamily == nil {
|
if metricFamily == nil {
|
||||||
return Histogram{}, fmt.Errorf("Metric %q not found", metricName)
|
return Histogram{}, fmt.Errorf("metric %q not found", metricName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if metricFamily.GetMetric() == nil {
|
if metricFamily.GetMetric() == nil {
|
||||||
return Histogram{}, fmt.Errorf("Metric %q is empty", metricName)
|
return Histogram{}, fmt.Errorf("metric %q is empty", metricName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(metricFamily.GetMetric()) == 0 {
|
if len(metricFamily.GetMetric()) == 0 {
|
||||||
return Histogram{}, fmt.Errorf("Metric %q is empty", metricName)
|
return Histogram{}, fmt.Errorf("metric %q is empty", metricName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Histogram{
|
return Histogram{
|
||||||
@ -277,12 +277,12 @@ func bucketQuantile(q float64, buckets []bucket) float64 {
|
|||||||
// Quantile computes q-th quantile of a cumulative histogram.
|
// Quantile computes q-th quantile of a cumulative histogram.
|
||||||
// It's expected the histogram is valid (by calling Validate)
|
// It's expected the histogram is valid (by calling Validate)
|
||||||
func (hist *Histogram) Quantile(q float64) float64 {
|
func (hist *Histogram) Quantile(q float64) float64 {
|
||||||
buckets := []bucket{}
|
var buckets []bucket
|
||||||
|
|
||||||
for _, bckt := range hist.Bucket {
|
for _, bckt := range hist.Bucket {
|
||||||
buckets = append(buckets, bucket{
|
buckets = append(buckets, bucket{
|
||||||
count: float64(*bckt.CumulativeCount),
|
count: float64(bckt.GetCumulativeCount()),
|
||||||
upperBound: *bckt.UpperBound,
|
upperBound: bckt.GetUpperBound(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,7 +294,7 @@ func (hist *Histogram) Quantile(q float64) float64 {
|
|||||||
|
|
||||||
// Average computes histogram's average value
|
// Average computes histogram's average value
|
||||||
func (hist *Histogram) Average() float64 {
|
func (hist *Histogram) Average() float64 {
|
||||||
return *hist.SampleSum / float64(*hist.SampleCount)
|
return hist.GetSampleSum() / float64(hist.GetSampleCount())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear clears all fields of the wrapped histogram
|
// Clear clears all fields of the wrapped histogram
|
||||||
@ -314,11 +314,11 @@ func (hist *Histogram) Clear() {
|
|||||||
|
|
||||||
// Validate makes sure the wrapped histogram has all necessary fields set and with valid values.
|
// Validate makes sure the wrapped histogram has all necessary fields set and with valid values.
|
||||||
func (hist *Histogram) Validate() error {
|
func (hist *Histogram) Validate() error {
|
||||||
if hist.SampleCount == nil || *hist.SampleCount == 0 {
|
if hist.SampleCount == nil || hist.GetSampleCount() == 0 {
|
||||||
return fmt.Errorf("nil or empty histogram SampleCount")
|
return fmt.Errorf("nil or empty histogram SampleCount")
|
||||||
}
|
}
|
||||||
|
|
||||||
if hist.SampleSum == nil || *hist.SampleSum == 0 {
|
if hist.SampleSum == nil || hist.GetSampleSum() == 0 {
|
||||||
return fmt.Errorf("nil or empty histogram SampleSum")
|
return fmt.Errorf("nil or empty histogram SampleSum")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -326,7 +326,7 @@ func (hist *Histogram) Validate() error {
|
|||||||
if bckt == nil {
|
if bckt == nil {
|
||||||
return fmt.Errorf("empty histogram bucket")
|
return fmt.Errorf("empty histogram bucket")
|
||||||
}
|
}
|
||||||
if bckt.UpperBound == nil || *bckt.UpperBound < 0 {
|
if bckt.UpperBound == nil || bckt.GetUpperBound() < 0 {
|
||||||
return fmt.Errorf("nil or negative histogram bucket UpperBound")
|
return fmt.Errorf("nil or negative histogram bucket UpperBound")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -338,7 +338,7 @@ func (hist *Histogram) Validate() error {
|
|||||||
func GetGaugeMetricValue(m metrics.GaugeMetric) (float64, error) {
|
func GetGaugeMetricValue(m metrics.GaugeMetric) (float64, error) {
|
||||||
metricProto := &dto.Metric{}
|
metricProto := &dto.Metric{}
|
||||||
if err := m.Write(metricProto); err != nil {
|
if err := m.Write(metricProto); err != nil {
|
||||||
return 0, fmt.Errorf("Error writing m: %v", err)
|
return 0, fmt.Errorf("error writing m: %v", err)
|
||||||
}
|
}
|
||||||
return metricProto.Gauge.GetValue(), nil
|
return metricProto.Gauge.GetValue(), nil
|
||||||
}
|
}
|
||||||
@ -347,7 +347,7 @@ func GetGaugeMetricValue(m metrics.GaugeMetric) (float64, error) {
|
|||||||
func GetCounterMetricValue(m metrics.CounterMetric) (float64, error) {
|
func GetCounterMetricValue(m metrics.CounterMetric) (float64, error) {
|
||||||
metricProto := &dto.Metric{}
|
metricProto := &dto.Metric{}
|
||||||
if err := m.(metrics.Metric).Write(metricProto); err != nil {
|
if err := m.(metrics.Metric).Write(metricProto); err != nil {
|
||||||
return 0, fmt.Errorf("Error writing m: %v", err)
|
return 0, fmt.Errorf("error writing m: %v", err)
|
||||||
}
|
}
|
||||||
return metricProto.Counter.GetValue(), nil
|
return metricProto.Counter.GetValue(), nil
|
||||||
}
|
}
|
||||||
@ -356,7 +356,7 @@ func GetCounterMetricValue(m metrics.CounterMetric) (float64, error) {
|
|||||||
func GetHistogramMetricValue(m metrics.ObserverMetric) (float64, error) {
|
func GetHistogramMetricValue(m metrics.ObserverMetric) (float64, error) {
|
||||||
metricProto := &dto.Metric{}
|
metricProto := &dto.Metric{}
|
||||||
if err := m.(metrics.Metric).Write(metricProto); err != nil {
|
if err := m.(metrics.Metric).Write(metricProto); err != nil {
|
||||||
return 0, fmt.Errorf("Error writing m: %v", err)
|
return 0, fmt.Errorf("error writing m: %v", err)
|
||||||
}
|
}
|
||||||
return metricProto.Histogram.GetSampleSum(), nil
|
return metricProto.Histogram.GetSampleSum(), nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user