Skip to content

Commit 885e1e9

Browse files
authored
Merge pull request kubernetes#90501 from tanjunchen/component-base/metrics-improve-code
k8s.io/component-base/metrics/testutil/:improve code
2 parents fabe9cd + 807a7e2 commit 885e1e9

File tree

1 file changed

+16
-16
lines changed
  • staging/src/k8s.io/component-base/metrics/testutil

1 file changed

+16
-16
lines changed

staging/src/k8s.io/component-base/metrics/testutil/metrics.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type Metrics map[string]model.Samples
4343

4444
// Equal returns true if all metrics are the same as the arguments.
4545
func (m *Metrics) Equal(o Metrics) bool {
46-
leftKeySet := []string{}
47-
rightKeySet := []string{}
46+
var leftKeySet []string
47+
var rightKeySet []string
4848
for k := range *m {
4949
leftKeySet = append(leftKeySet, k)
5050
}
@@ -207,22 +207,22 @@ func GetHistogramFromGatherer(gatherer metrics.Gatherer, metricName string) (His
207207
return Histogram{}, err
208208
}
209209
for _, mFamily := range m {
210-
if mFamily.Name != nil && *mFamily.Name == metricName {
210+
if mFamily.GetName() == metricName {
211211
metricFamily = mFamily
212212
break
213213
}
214214
}
215215

216216
if metricFamily == nil {
217-
return Histogram{}, fmt.Errorf("Metric %q not found", metricName)
217+
return Histogram{}, fmt.Errorf("metric %q not found", metricName)
218218
}
219219

220220
if metricFamily.GetMetric() == nil {
221-
return Histogram{}, fmt.Errorf("Metric %q is empty", metricName)
221+
return Histogram{}, fmt.Errorf("metric %q is empty", metricName)
222222
}
223223

224224
if len(metricFamily.GetMetric()) == 0 {
225-
return Histogram{}, fmt.Errorf("Metric %q is empty", metricName)
225+
return Histogram{}, fmt.Errorf("metric %q is empty", metricName)
226226
}
227227

228228
return Histogram{
@@ -277,12 +277,12 @@ func bucketQuantile(q float64, buckets []bucket) float64 {
277277
// Quantile computes q-th quantile of a cumulative histogram.
278278
// It's expected the histogram is valid (by calling Validate)
279279
func (hist *Histogram) Quantile(q float64) float64 {
280-
buckets := []bucket{}
280+
var buckets []bucket
281281

282282
for _, bckt := range hist.Bucket {
283283
buckets = append(buckets, bucket{
284-
count: float64(*bckt.CumulativeCount),
285-
upperBound: *bckt.UpperBound,
284+
count: float64(bckt.GetCumulativeCount()),
285+
upperBound: bckt.GetUpperBound(),
286286
})
287287
}
288288

@@ -294,7 +294,7 @@ func (hist *Histogram) Quantile(q float64) float64 {
294294

295295
// Average computes histogram's average value
296296
func (hist *Histogram) Average() float64 {
297-
return *hist.SampleSum / float64(*hist.SampleCount)
297+
return hist.GetSampleSum() / float64(hist.GetSampleCount())
298298
}
299299

300300
// Clear clears all fields of the wrapped histogram
@@ -314,19 +314,19 @@ func (hist *Histogram) Clear() {
314314

315315
// Validate makes sure the wrapped histogram has all necessary fields set and with valid values.
316316
func (hist *Histogram) Validate() error {
317-
if hist.SampleCount == nil || *hist.SampleCount == 0 {
317+
if hist.SampleCount == nil || hist.GetSampleCount() == 0 {
318318
return fmt.Errorf("nil or empty histogram SampleCount")
319319
}
320320

321-
if hist.SampleSum == nil || *hist.SampleSum == 0 {
321+
if hist.SampleSum == nil || hist.GetSampleSum() == 0 {
322322
return fmt.Errorf("nil or empty histogram SampleSum")
323323
}
324324

325325
for _, bckt := range hist.Bucket {
326326
if bckt == nil {
327327
return fmt.Errorf("empty histogram bucket")
328328
}
329-
if bckt.UpperBound == nil || *bckt.UpperBound < 0 {
329+
if bckt.UpperBound == nil || bckt.GetUpperBound() < 0 {
330330
return fmt.Errorf("nil or negative histogram bucket UpperBound")
331331
}
332332
}
@@ -338,7 +338,7 @@ func (hist *Histogram) Validate() error {
338338
func GetGaugeMetricValue(m metrics.GaugeMetric) (float64, error) {
339339
metricProto := &dto.Metric{}
340340
if err := m.Write(metricProto); err != nil {
341-
return 0, fmt.Errorf("Error writing m: %v", err)
341+
return 0, fmt.Errorf("error writing m: %v", err)
342342
}
343343
return metricProto.Gauge.GetValue(), nil
344344
}
@@ -347,7 +347,7 @@ func GetGaugeMetricValue(m metrics.GaugeMetric) (float64, error) {
347347
func GetCounterMetricValue(m metrics.CounterMetric) (float64, error) {
348348
metricProto := &dto.Metric{}
349349
if err := m.(metrics.Metric).Write(metricProto); err != nil {
350-
return 0, fmt.Errorf("Error writing m: %v", err)
350+
return 0, fmt.Errorf("error writing m: %v", err)
351351
}
352352
return metricProto.Counter.GetValue(), nil
353353
}
@@ -356,7 +356,7 @@ func GetCounterMetricValue(m metrics.CounterMetric) (float64, error) {
356356
func GetHistogramMetricValue(m metrics.ObserverMetric) (float64, error) {
357357
metricProto := &dto.Metric{}
358358
if err := m.(metrics.Metric).Write(metricProto); err != nil {
359-
return 0, fmt.Errorf("Error writing m: %v", err)
359+
return 0, fmt.Errorf("error writing m: %v", err)
360360
}
361361
return metricProto.Histogram.GetSampleSum(), nil
362362
}

0 commit comments

Comments
 (0)