// timeRequestStatus runs 'f' and records how long it took in the given // Prometheus metric. // // toStatusCode is a function that translates errors returned by 'f' into // HTTP-like status codes. func timeRequestStatus(method string, metric *prometheus.SummaryVec, toStatusCode func(error) string, f func() error) error { if toStatusCode == nil { toStatusCode = errorCode } startTime := time.Now() err := f() duration := time.Now().Sub(startTime) metric.WithLabelValues(method, toStatusCode(err)).Observe(duration.Seconds()) return err }
func measureCommandDuration(m *prometheus.SummaryVec, fn func() error, labels ...string) error { n := time.Now() err := fn() duration := time.Now().Sub(n) statusCode, ok := exitCodeForCommandError(err) if !ok { statusCode = -1 } m.WithLabelValues(append(labels, strconv.Itoa(statusCode))...).Observe(float64(duration / time.Millisecond)) return err }
// TODO: Pending prometheus/client_golang#58 // read metric helpers needs rework once testability is improved! func readSummaryVec(m *prometheus.SummaryVec, l prometheus.Labels) []*dto.Quantile { pb := &dto.Metric{} s := m.With(l) s.Write(pb) return pb.GetSummary().GetQuantile() }