1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// Copyright 2017 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package monitoring
import (
"fmt"
"strings"
"sync"
"k8s.io/klog/v2"
)
// InertMetricFactory creates inert metrics for testing.
type InertMetricFactory struct{}
// NewCounter creates a new inert Counter.
func (imf InertMetricFactory) NewCounter(name, help string, labelNames ...string) Counter {
return &InertFloat{
labelCount: len(labelNames),
vals: make(map[string]float64),
}
}
// NewGauge creates a new inert Gauge.
func (imf InertMetricFactory) NewGauge(name, help string, labelNames ...string) Gauge {
return &InertFloat{
labelCount: len(labelNames),
vals: make(map[string]float64),
}
}
// NewHistogram creates a new inert Histogram.
func (imf InertMetricFactory) NewHistogram(name, help string, labelNames ...string) Histogram {
return &InertDistribution{
labelCount: len(labelNames),
counts: make(map[string]uint64),
sums: make(map[string]float64),
}
}
// NewHistogramWithBuckets creates a new inert Histogram with supplied buckets.
// The buckets are not actually used.
func (imf InertMetricFactory) NewHistogramWithBuckets(name, help string, _ []float64, labelNames ...string) Histogram {
return imf.NewHistogram(name, help, labelNames...)
}
// InertFloat is an internal-only implementation of both the Counter and Gauge interfaces.
type InertFloat struct {
labelCount int
mu sync.Mutex
vals map[string]float64
}
// Inc adds 1 to the value.
func (m *InertFloat) Inc(labelVals ...string) {
m.Add(1.0, labelVals...)
}
// Dec subtracts 1 from the value.
func (m *InertFloat) Dec(labelVals ...string) {
m.Add(-1.0, labelVals...)
}
// Add adds the given amount to the value.
func (m *InertFloat) Add(val float64, labelVals ...string) {
m.mu.Lock()
defer m.mu.Unlock()
key, err := keyForLabels(labelVals, m.labelCount)
if err != nil {
klog.Error(err.Error())
return
}
m.vals[key] += val
}
// Set sets the value.
func (m *InertFloat) Set(val float64, labelVals ...string) {
m.mu.Lock()
defer m.mu.Unlock()
key, err := keyForLabels(labelVals, m.labelCount)
if err != nil {
klog.Error(err.Error())
return
}
m.vals[key] = val
}
// Value returns the current value.
func (m *InertFloat) Value(labelVals ...string) float64 {
m.mu.Lock()
defer m.mu.Unlock()
key, err := keyForLabels(labelVals, m.labelCount)
if err != nil {
klog.Error(err.Error())
return 0.0
}
return m.vals[key]
}
// InertDistribution is an internal-only implementation of the Distribution interface.
type InertDistribution struct {
labelCount int
mu sync.Mutex
counts map[string]uint64
sums map[string]float64
}
// Observe adds a single observation to the distribution.
func (m *InertDistribution) Observe(val float64, labelVals ...string) {
m.mu.Lock()
defer m.mu.Unlock()
key, err := keyForLabels(labelVals, m.labelCount)
if err != nil {
klog.Error(err.Error())
return
}
m.counts[key]++
m.sums[key] += val
}
// Info returns count, sum for the distribution.
func (m *InertDistribution) Info(labelVals ...string) (uint64, float64) {
m.mu.Lock()
defer m.mu.Unlock()
key, err := keyForLabels(labelVals, m.labelCount)
if err != nil {
klog.Error(err.Error())
return 0, 0.0
}
return m.counts[key], m.sums[key]
}
func keyForLabels(labelVals []string, count int) (string, error) {
if len(labelVals) != count {
return "", fmt.Errorf("invalid label count %d; want %d", len(labelVals), count)
}
return strings.Join(labelVals, "|"), nil
}
|