Skip to content

Commit

Permalink
add status code to request duration metric
Browse files Browse the repository at this point in the history
  • Loading branch information
m3co-code authored and ldez committed Jun 26, 2017
1 parent 34e60a8 commit 0055965
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
9 changes: 6 additions & 3 deletions middlewares/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions middlewares/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions middlewares/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0055965

Please sign in to comment.