mirror of
https://github.com/hanzoai/traces.git
synced 2026-11-02 12:18:48 +00:00
* Use native histogram for blocklist_poll_duration_seconds * Keep default buckets * Add native histogram support to remaining histograms * Update changelog
42 lines
1.2 KiB
Go
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
|
|
}
|