/
gauge.go
104 lines (93 loc) · 2.88 KB
/
gauge.go
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
// Copyright Istio Authors
//
// 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 (
"context"
"sync"
"go.opentelemetry.io/otel/attribute"
api "go.opentelemetry.io/otel/metric"
"istio.io/istio/pkg/log"
)
type gauge struct {
baseMetric
g api.Float64ObservableGauge
// attributeSets stores a map of attributes -> values, for gauges.
attributeSetsMutex *sync.RWMutex
attributeSets map[attribute.Set]*gaugeValues
currentGaugeSet *gaugeValues
}
var _ Metric = &gauge{}
func newGauge(o options) *gauge {
r := &gauge{
attributeSetsMutex: &sync.RWMutex{},
}
r.attributeSets = map[attribute.Set]*gaugeValues{}
g, err := meter().Float64ObservableGauge(o.name,
api.WithFloat64Callback(func(ctx context.Context, observer api.Float64Observer) error {
r.attributeSetsMutex.Lock()
defer r.attributeSetsMutex.Unlock()
for _, gv := range r.attributeSets {
observer.Observe(gv.val, gv.opt...)
}
return nil
}),
api.WithDescription(o.description),
api.WithUnit(string(o.unit)))
if err != nil {
log.Fatalf("failed to create gauge: %v", err)
}
r.g = g
r.baseMetric = baseMetric{
name: o.name,
rest: r,
}
return r
}
func (f *gauge) Record(value float64) {
f.runRecordHook(value)
// TODO: https://github.com/open-telemetry/opentelemetry-specification/issues/2318 use synchronous gauge so we don't need to deal with this
f.attributeSetsMutex.Lock()
// Special case: we lazy-load the non-labeled value. This ensures that metrics which should always have labels do not end up with a un-labeled zero-value
// If a metric really requires `metric{} 0`, they can explicitly call .Record(0).
if f.currentGaugeSet == nil {
f.currentGaugeSet = &gaugeValues{}
f.attributeSets[attribute.NewSet()] = f.currentGaugeSet
}
f.currentGaugeSet.val = value
f.attributeSetsMutex.Unlock()
}
func (f *gauge) With(labelValues ...LabelValue) Metric {
attrs, set := rebuildAttributes(f.baseMetric, labelValues)
nm := &gauge{
g: f.g,
attributeSetsMutex: f.attributeSetsMutex,
attributeSets: f.attributeSets,
}
if _, f := nm.attributeSets[set]; !f {
nm.attributeSets[set] = &gaugeValues{
opt: []api.ObserveOption{api.WithAttributeSet(set)},
}
}
nm.currentGaugeSet = nm.attributeSets[set]
nm.baseMetric = baseMetric{
name: f.name,
attrs: attrs,
rest: nm,
}
return nm
}
type gaugeValues struct {
val float64
opt []api.ObserveOption
}