add status code to request duration metric

This commit is contained in:
Marco Jantke 2017-06-15 16:06:02 +02:00 committed by Ludovic Fernandez
parent 34e60a8404
commit 0055965295
3 changed files with 9 additions and 5 deletions

View file

@ -42,9 +42,12 @@ func (m *MetricsWrapper) ServeHTTP(rw http.ResponseWriter, r *http.Request, next
start := time.Now()
prw := &responseRecorder{rw, http.StatusOK}
next(prw, r)
labels := []string{"code", strconv.Itoa(prw.statusCode), "method", r.Method}
m.Impl.getReqsCounter().With(labels...).Add(1)
m.Impl.getReqDurationHistogram().Observe(float64(time.Since(start).Seconds()))
reqLabels := []string{"code", strconv.Itoa(prw.statusCode), "method", r.Method}
m.Impl.getReqsCounter().With(reqLabels...).Add(1)
reqDurationLabels := []string{"code", strconv.Itoa(prw.statusCode)}
m.Impl.getReqDurationHistogram().With(reqDurationLabels...).Observe(float64(time.Since(start).Seconds()))
}
// MetricsRetryListener is an implementation of the RetryListener interface to

View file

@ -17,7 +17,7 @@ const (
// Prometheus is an Implementation for Metrics that exposes the following Prometheus metrics:
// - number of requests partitioned by status code and method
// - request durations
// - request durations partitioned by status code
// - amount of retries happened
type Prometheus struct {
reqsCounter metrics.Counter
@ -73,7 +73,7 @@ func NewPrometheus(name string, config *types.Prometheus) (*Prometheus, []stdpro
ConstLabels: stdprometheus.Labels{"service": name},
Buckets: buckets,
},
[]string{},
[]string{"code"},
)
hv, err = registerHistogramVec(hv)
if err != nil {

View file

@ -75,6 +75,7 @@ func TestPrometheus(t *testing.T) {
name: reqDurationName,
labels: map[string]string{
"service": "test",
"code": "200",
},
assert: func(family *dto.MetricFamily) {
sc := family.Metric[0].Histogram.GetSampleCount()