Skip to content

Commit 4c41dfb

Browse files
Clarify exemplar(Add|Observe) by renaming to (add|observe)WithExemplar (#1122)
* Clarify exemplarAdd by renaming to addWithExemplar Signed-off-by: Dave Henderson <[email protected]> * Documenting addWithExemplar Signed-off-by: Dave Henderson <[email protected]> * Also rename exemplarObserve to follow the same pattern Signed-off-by: Dave Henderson <[email protected]> Signed-off-by: Dave Henderson <[email protected]>
1 parent f73e3cc commit 4c41dfb

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

prometheus/promhttp/instrument_client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func InstrumentRoundTripperCounter(counter *prometheus.CounterVec, next http.Rou
7373
return func(r *http.Request) (*http.Response, error) {
7474
resp, err := next.RoundTrip(r)
7575
if err == nil {
76-
exemplarAdd(
76+
addWithExemplar(
7777
counter.With(labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...)),
7878
1,
7979
rtOpts.getExemplarFn(r.Context()),
@@ -116,7 +116,7 @@ func InstrumentRoundTripperDuration(obs prometheus.ObserverVec, next http.RoundT
116116
start := time.Now()
117117
resp, err := next.RoundTrip(r)
118118
if err == nil {
119-
exemplarObserve(
119+
observeWithExemplar(
120120
obs.With(labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...)),
121121
time.Since(start).Seconds(),
122122
rtOpts.getExemplarFn(r.Context()),

prometheus/promhttp/instrument_server.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ import (
2828
// magicString is used for the hacky label test in checkLabels. Remove once fixed.
2929
const magicString = "zZgWfBxLqvG8kc8IMv3POi2Bb0tZI3vAnBx+gBaFi9FyPzB/CzKUer1yufDa"
3030

31-
func exemplarObserve(obs prometheus.Observer, val float64, labels map[string]string) {
31+
// observeWithExemplar is a wrapper for [prometheus.ExemplarAdder.ExemplarObserver],
32+
// which falls back to [prometheus.Observer.Observe] if no labels are provided.
33+
func observeWithExemplar(obs prometheus.Observer, val float64, labels map[string]string) {
3234
if labels == nil {
3335
obs.Observe(val)
3436
return
3537
}
3638
obs.(prometheus.ExemplarObserver).ObserveWithExemplar(val, labels)
3739
}
3840

39-
func exemplarAdd(obs prometheus.Counter, val float64, labels map[string]string) {
41+
// addWithExemplar is a wrapper for [prometheus.ExemplarAdder.AddWithExemplar],
42+
// which falls back to [prometheus.Counter.Add] if no labels are provided.
43+
func addWithExemplar(obs prometheus.Counter, val float64, labels map[string]string) {
4044
if labels == nil {
4145
obs.Add(val)
4246
return
@@ -91,7 +95,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
9195
d := newDelegator(w, nil)
9296
next.ServeHTTP(d, r)
9397

94-
exemplarObserve(
98+
observeWithExemplar(
9599
obs.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)),
96100
time.Since(now).Seconds(),
97101
hOpts.getExemplarFn(r.Context()),
@@ -103,7 +107,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
103107
now := time.Now()
104108
next.ServeHTTP(w, r)
105109

106-
exemplarObserve(
110+
observeWithExemplar(
107111
obs.With(labels(code, method, r.Method, 0, hOpts.extraMethods...)),
108112
time.Since(now).Seconds(),
109113
hOpts.getExemplarFn(r.Context()),
@@ -141,7 +145,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
141145
d := newDelegator(w, nil)
142146
next.ServeHTTP(d, r)
143147

144-
exemplarAdd(
148+
addWithExemplar(
145149
counter.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)),
146150
1,
147151
hOpts.getExemplarFn(r.Context()),
@@ -151,7 +155,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
151155

152156
return func(w http.ResponseWriter, r *http.Request) {
153157
next.ServeHTTP(w, r)
154-
exemplarAdd(
158+
addWithExemplar(
155159
counter.With(labels(code, method, r.Method, 0, hOpts.extraMethods...)),
156160
1,
157161
hOpts.getExemplarFn(r.Context()),
@@ -192,7 +196,7 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
192196
return func(w http.ResponseWriter, r *http.Request) {
193197
now := time.Now()
194198
d := newDelegator(w, func(status int) {
195-
exemplarObserve(
199+
observeWithExemplar(
196200
obs.With(labels(code, method, r.Method, status, hOpts.extraMethods...)),
197201
time.Since(now).Seconds(),
198202
hOpts.getExemplarFn(r.Context()),
@@ -233,7 +237,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
233237
d := newDelegator(w, nil)
234238
next.ServeHTTP(d, r)
235239
size := computeApproximateRequestSize(r)
236-
exemplarObserve(
240+
observeWithExemplar(
237241
obs.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)),
238242
float64(size),
239243
hOpts.getExemplarFn(r.Context()),
@@ -244,7 +248,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
244248
return func(w http.ResponseWriter, r *http.Request) {
245249
next.ServeHTTP(w, r)
246250
size := computeApproximateRequestSize(r)
247-
exemplarObserve(
251+
observeWithExemplar(
248252
obs.With(labels(code, method, r.Method, 0, hOpts.extraMethods...)),
249253
float64(size),
250254
hOpts.getExemplarFn(r.Context()),
@@ -282,7 +286,7 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
282286
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
283287
d := newDelegator(w, nil)
284288
next.ServeHTTP(d, r)
285-
exemplarObserve(
289+
observeWithExemplar(
286290
obs.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)),
287291
float64(d.Written()),
288292
hOpts.getExemplarFn(r.Context()),

0 commit comments

Comments
 (0)