Files
Mike Cohen 45d4819fdc Implemented cancellation for GUI (#789)
Instrumenting api calls can be used to inject latency. This is
required to see what the GUI does when api calls are very
latent. Started to implement API call cancellations to ensure that
usage under latent conditions is possible.
2020-12-03 10:01:36 +10:00

35 lines
729 B
Go

package api
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
apiHistorgram = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "gui_api_latency",
Help: "Latency to server API calls.",
Buckets: prometheus.LinearBuckets(0.01, 0.05, 10),
},
[]string{"api"},
)
inject_time = 0
)
func Instrument(api string) func() time.Duration {
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
apiHistorgram.WithLabelValues(api).Observe(v)
}))
// Instrument a delay in API calls.
if inject_time > 0 {
time.Sleep(time.Duration(inject_time) * time.Millisecond)
}
return timer.ObserveDuration
}