...
1
16
17 package metrics
18
19 import (
20 "context"
21
22 "github.com/blang/semver/v4"
23 "github.com/prometheus/client_golang/prometheus"
24 )
25
26
27
28 type Histogram struct {
29 ObserverMetric
30 *HistogramOpts
31 lazyMetric
32 selfCollector
33 }
34
35
36
37 func NewHistogram(opts *HistogramOpts) *Histogram {
38 opts.StabilityLevel.setDefaults()
39
40 h := &Histogram{
41 HistogramOpts: opts,
42 lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
43 }
44 h.setPrometheusHistogram(noopMetric{})
45 h.lazyInit(h, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
46 return h
47 }
48
49
50 func (h *Histogram) setPrometheusHistogram(histogram prometheus.Histogram) {
51 h.ObserverMetric = histogram
52 h.initSelfCollection(histogram)
53 }
54
55
56 func (h *Histogram) DeprecatedVersion() *semver.Version {
57 return parseSemver(h.HistogramOpts.DeprecatedVersion)
58 }
59
60
61
62 func (h *Histogram) initializeMetric() {
63 h.HistogramOpts.annotateStabilityLevel()
64
65 h.setPrometheusHistogram(prometheus.NewHistogram(h.HistogramOpts.toPromHistogramOpts()))
66 }
67
68
69
70 func (h *Histogram) initializeDeprecatedMetric() {
71 h.HistogramOpts.markDeprecated()
72 h.initializeMetric()
73 }
74
75
76 func (h *Histogram) WithContext(ctx context.Context) ObserverMetric {
77 return h.ObserverMetric
78 }
79
80
81
82 type HistogramVec struct {
83 *prometheus.HistogramVec
84 *HistogramOpts
85 lazyMetric
86 originalLabels []string
87 }
88
89
90
91
92
93
94
95 func NewHistogramVec(opts *HistogramOpts, labels []string) *HistogramVec {
96 opts.StabilityLevel.setDefaults()
97
98 fqName := BuildFQName(opts.Namespace, opts.Subsystem, opts.Name)
99 allowListLock.RLock()
100 if allowList, ok := labelValueAllowLists[fqName]; ok {
101 opts.LabelValueAllowLists = allowList
102 }
103 allowListLock.RUnlock()
104
105 v := &HistogramVec{
106 HistogramVec: noopHistogramVec,
107 HistogramOpts: opts,
108 originalLabels: labels,
109 lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
110 }
111 v.lazyInit(v, fqName)
112 return v
113 }
114
115
116 func (v *HistogramVec) DeprecatedVersion() *semver.Version {
117 return parseSemver(v.HistogramOpts.DeprecatedVersion)
118 }
119
120 func (v *HistogramVec) initializeMetric() {
121 v.HistogramOpts.annotateStabilityLevel()
122 v.HistogramVec = prometheus.NewHistogramVec(v.HistogramOpts.toPromHistogramOpts(), v.originalLabels)
123 }
124
125 func (v *HistogramVec) initializeDeprecatedMetric() {
126 v.HistogramOpts.markDeprecated()
127 v.initializeMetric()
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 func (v *HistogramVec) WithLabelValues(lvs ...string) ObserverMetric {
146 if !v.IsCreated() {
147 return noop
148 }
149 if v.LabelValueAllowLists != nil {
150 v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
151 }
152 return v.HistogramVec.WithLabelValues(lvs...)
153 }
154
155
156
157
158
159 func (v *HistogramVec) With(labels map[string]string) ObserverMetric {
160 if !v.IsCreated() {
161 return noop
162 }
163 if v.LabelValueAllowLists != nil {
164 v.LabelValueAllowLists.ConstrainLabelMap(labels)
165 }
166 return v.HistogramVec.With(labels)
167 }
168
169
170
171
172
173
174
175
176 func (v *HistogramVec) Delete(labels map[string]string) bool {
177 if !v.IsCreated() {
178 return false
179 }
180 return v.HistogramVec.Delete(labels)
181 }
182
183
184 func (v *HistogramVec) Reset() {
185 if !v.IsCreated() {
186 return
187 }
188
189 v.HistogramVec.Reset()
190 }
191
192
193 func (v *HistogramVec) WithContext(ctx context.Context) *HistogramVecWithContext {
194 return &HistogramVecWithContext{
195 ctx: ctx,
196 HistogramVec: v,
197 }
198 }
199
200
201 type HistogramVecWithContext struct {
202 *HistogramVec
203 ctx context.Context
204 }
205
206
207 func (vc *HistogramVecWithContext) WithLabelValues(lvs ...string) ObserverMetric {
208 return vc.HistogramVec.WithLabelValues(lvs...)
209 }
210
211
212 func (vc *HistogramVecWithContext) With(labels map[string]string) ObserverMetric {
213 return vc.HistogramVec.With(labels)
214 }
215
View as plain text