Files
Zach LeslieandGitHub 7d57b262b0 Add native histograms for internal metrics (#3870)
* Use native histogram for blocklist_poll_duration_seconds

* Keep default buckets

* Add native histogram support to remaining histograms

* Update changelog
2024-07-19 17:49:57 +00:00

42 lines
1.2 KiB
Go

package instrumentation
import (
"net/http"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var requestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "tempodb",
Name: "backend_request_duration_seconds",
Help: "Time spent doing backend storage requests.",
Buckets: prometheus.ExponentialBuckets(0.005, 4, 6),
NativeHistogramBucketFactor: 1.1,
NativeHistogramMaxBucketNumber: 100,
NativeHistogramMinResetDuration: 1 * time.Hour,
}, []string{"operation", "status_code"})
type instrumentedTransport struct {
observer prometheus.ObserverVec
next http.RoundTripper
}
func NewTransport(next http.RoundTripper) http.RoundTripper {
return instrumentedTransport{
next: next,
observer: requestDuration,
}
}
func (i instrumentedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
start := time.Now()
resp, err := i.next.RoundTrip(req)
if err == nil {
i.observer.WithLabelValues(req.Method, strconv.Itoa(resp.StatusCode)).Observe(time.Since(start).Seconds())
}
return resp, err
}