Clarify exemplar(Add|Observe) by renaming to (add|observe)WithExemplar (#1122)

* Clarify exemplarAdd by renaming to addWithExemplar

Signed-off-by: Dave Henderson <dhenderson@gmail.com>

* Documenting addWithExemplar

Signed-off-by: Dave Henderson <dhenderson@gmail.com>

* Also rename exemplarObserve to follow the same pattern

Signed-off-by: Dave Henderson <dhenderson@gmail.com>

Signed-off-by: Dave Henderson <dhenderson@gmail.com>
This commit is contained in:
Dave Henderson 2022-08-22 11:31:08 -04:00 committed by GitHub
parent f73e3cc0e2
commit 4c41dfbcd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 12 deletions

View File

@ -73,7 +73,7 @@ func InstrumentRoundTripperCounter(counter *prometheus.CounterVec, next http.Rou
return func(r *http.Request) (*http.Response, error) { return func(r *http.Request) (*http.Response, error) {
resp, err := next.RoundTrip(r) resp, err := next.RoundTrip(r)
if err == nil { if err == nil {
exemplarAdd( addWithExemplar(
counter.With(labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...)), counter.With(labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...)),
1, 1,
rtOpts.getExemplarFn(r.Context()), rtOpts.getExemplarFn(r.Context()),
@ -116,7 +116,7 @@ func InstrumentRoundTripperDuration(obs prometheus.ObserverVec, next http.RoundT
start := time.Now() start := time.Now()
resp, err := next.RoundTrip(r) resp, err := next.RoundTrip(r)
if err == nil { if err == nil {
exemplarObserve( observeWithExemplar(
obs.With(labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...)), obs.With(labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...)),
time.Since(start).Seconds(), time.Since(start).Seconds(),
rtOpts.getExemplarFn(r.Context()), rtOpts.getExemplarFn(r.Context()),

View File

@ -28,7 +28,9 @@ import (
// magicString is used for the hacky label test in checkLabels. Remove once fixed. // magicString is used for the hacky label test in checkLabels. Remove once fixed.
const magicString = "zZgWfBxLqvG8kc8IMv3POi2Bb0tZI3vAnBx+gBaFi9FyPzB/CzKUer1yufDa" const magicString = "zZgWfBxLqvG8kc8IMv3POi2Bb0tZI3vAnBx+gBaFi9FyPzB/CzKUer1yufDa"
func exemplarObserve(obs prometheus.Observer, val float64, labels map[string]string) { // observeWithExemplar is a wrapper for [prometheus.ExemplarAdder.ExemplarObserver],
// which falls back to [prometheus.Observer.Observe] if no labels are provided.
func observeWithExemplar(obs prometheus.Observer, val float64, labels map[string]string) {
if labels == nil { if labels == nil {
obs.Observe(val) obs.Observe(val)
return return
@ -36,7 +38,9 @@ func exemplarObserve(obs prometheus.Observer, val float64, labels map[string]str
obs.(prometheus.ExemplarObserver).ObserveWithExemplar(val, labels) obs.(prometheus.ExemplarObserver).ObserveWithExemplar(val, labels)
} }
func exemplarAdd(obs prometheus.Counter, val float64, labels map[string]string) { // addWithExemplar is a wrapper for [prometheus.ExemplarAdder.AddWithExemplar],
// which falls back to [prometheus.Counter.Add] if no labels are provided.
func addWithExemplar(obs prometheus.Counter, val float64, labels map[string]string) {
if labels == nil { if labels == nil {
obs.Add(val) obs.Add(val)
return return
@ -91,7 +95,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
d := newDelegator(w, nil) d := newDelegator(w, nil)
next.ServeHTTP(d, r) next.ServeHTTP(d, r)
exemplarObserve( observeWithExemplar(
obs.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)), obs.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)),
time.Since(now).Seconds(), time.Since(now).Seconds(),
hOpts.getExemplarFn(r.Context()), hOpts.getExemplarFn(r.Context()),
@ -103,7 +107,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
now := time.Now() now := time.Now()
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
exemplarObserve( observeWithExemplar(
obs.With(labels(code, method, r.Method, 0, hOpts.extraMethods...)), obs.With(labels(code, method, r.Method, 0, hOpts.extraMethods...)),
time.Since(now).Seconds(), time.Since(now).Seconds(),
hOpts.getExemplarFn(r.Context()), hOpts.getExemplarFn(r.Context()),
@ -141,7 +145,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
d := newDelegator(w, nil) d := newDelegator(w, nil)
next.ServeHTTP(d, r) next.ServeHTTP(d, r)
exemplarAdd( addWithExemplar(
counter.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)), counter.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)),
1, 1,
hOpts.getExemplarFn(r.Context()), hOpts.getExemplarFn(r.Context()),
@ -151,7 +155,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
exemplarAdd( addWithExemplar(
counter.With(labels(code, method, r.Method, 0, hOpts.extraMethods...)), counter.With(labels(code, method, r.Method, 0, hOpts.extraMethods...)),
1, 1,
hOpts.getExemplarFn(r.Context()), hOpts.getExemplarFn(r.Context()),
@ -192,7 +196,7 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
now := time.Now() now := time.Now()
d := newDelegator(w, func(status int) { d := newDelegator(w, func(status int) {
exemplarObserve( observeWithExemplar(
obs.With(labels(code, method, r.Method, status, hOpts.extraMethods...)), obs.With(labels(code, method, r.Method, status, hOpts.extraMethods...)),
time.Since(now).Seconds(), time.Since(now).Seconds(),
hOpts.getExemplarFn(r.Context()), hOpts.getExemplarFn(r.Context()),
@ -233,7 +237,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
d := newDelegator(w, nil) d := newDelegator(w, nil)
next.ServeHTTP(d, r) next.ServeHTTP(d, r)
size := computeApproximateRequestSize(r) size := computeApproximateRequestSize(r)
exemplarObserve( observeWithExemplar(
obs.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)), obs.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)),
float64(size), float64(size),
hOpts.getExemplarFn(r.Context()), hOpts.getExemplarFn(r.Context()),
@ -244,7 +248,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
size := computeApproximateRequestSize(r) size := computeApproximateRequestSize(r)
exemplarObserve( observeWithExemplar(
obs.With(labels(code, method, r.Method, 0, hOpts.extraMethods...)), obs.With(labels(code, method, r.Method, 0, hOpts.extraMethods...)),
float64(size), float64(size),
hOpts.getExemplarFn(r.Context()), hOpts.getExemplarFn(r.Context()),
@ -282,7 +286,7 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
d := newDelegator(w, nil) d := newDelegator(w, nil)
next.ServeHTTP(d, r) next.ServeHTTP(d, r)
exemplarObserve( observeWithExemplar(
obs.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)), obs.With(labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)),
float64(d.Written()), float64(d.Written()),
hOpts.getExemplarFn(r.Context()), hOpts.getExemplarFn(r.Context()),