Skip to content

Commit

Permalink
Instrument backend handler response count
Browse files Browse the repository at this point in the history
Every time the backend handler responds with a status code, we should
count that response

Signed-off-by: Toby Lorne <toby.lornewelch-richards@digital.cabinet-office.gov.uk>
  • Loading branch information
Toby Lorne committed Mar 2, 2020
1 parent ee1b938 commit b443d3d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions handlers/backend_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ func (bt *backendTransport) RoundTrip(req *http.Request) (resp *http.Response, e
"backend_id": bt.backendID,
"response_code": fmt.Sprintf("%d", responseCode),
}).Add(durationSeconds)

BackendHandlerResponseCountMetric.With(prometheus.Labels{
"backend_id": bt.backendID,
"response_code": fmt.Sprintf("%d", responseCode),
}).Inc()
}()

resp, err = bt.wrapped.RoundTrip(req)
Expand Down
24 changes: 24 additions & 0 deletions handlers/backend_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ var _ = Describe("Backend handler", func() {
Context("metrics", func() {
var (
beforeRequestCountMetric float64
beforeResponseCountMetric float64
beforeResponseDurationSecondsMetric float64
)

Expand All @@ -152,6 +153,15 @@ var _ = Describe("Backend handler", func() {
)
}

var measureResponseCount = func(responseCode string) float64 {
return promtest.ToFloat64(
handlers.BackendHandlerResponseCountMetric.With(prometheus.Labels{
"backend_id": "backend-metrics",
"response_code": responseCode,
}),
)
}

var measureResponseDurationSeconds = func(responseCode string) float64 {
return promtest.ToFloat64(
handlers.BackendHandlerResponseDurationSecondsMetric.With(prometheus.Labels{
Expand Down Expand Up @@ -179,6 +189,7 @@ var _ = Describe("Backend handler", func() {
rw.WriteHeader(http.StatusOK)
})

beforeResponseCountMetric = measureResponseCount("200")
beforeResponseDurationSecondsMetric = measureResponseDurationSeconds("200")

router.ServeHTTP(
Expand All @@ -193,6 +204,12 @@ var _ = Describe("Backend handler", func() {
).To(Equal(float64(1)))
})

It("should count the number of proxied responses", func() {
Expect(
measureResponseCount("200") - beforeResponseCountMetric,
).To(Equal(float64(1)))
})

It("should record the duration of proxied responses", func() {
Expect(
measureResponseDurationSeconds("200") - beforeResponseDurationSecondsMetric,
Expand All @@ -207,6 +224,7 @@ var _ = Describe("Backend handler", func() {
rw.WriteHeader(http.StatusOK)
})

beforeResponseCountMetric = measureResponseCount("504")
beforeResponseDurationSecondsMetric = measureResponseDurationSeconds("504")

router.ServeHTTP(
Expand All @@ -221,6 +239,12 @@ var _ = Describe("Backend handler", func() {
).To(Equal(float64(1)))
})

It("should count the number of proxied responses", func() {
Expect(
measureResponseCount("504") - beforeResponseCountMetric,
).To(Equal(float64(1)))
})

It("should record the duration of proxied responses", func() {
Expect(
measureResponseDurationSeconds("504") - beforeResponseDurationSecondsMetric,
Expand Down
12 changes: 12 additions & 0 deletions handlers/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ var (
},
)

BackendHandlerResponseCountMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "router_backend_handler_response_count",
Help: "Number of responses returned by router backend handlers",
},
[]string{
"backend_id",
"response_code",
},
)

BackendHandlerResponseDurationSecondsMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "router_backend_handler_response_duration_seconds",
Expand All @@ -43,5 +54,6 @@ func initMetrics() {
prometheus.MustRegister(RedirectHandlerRedirectCountMetric)

prometheus.MustRegister(BackendHandlerRequestCountMetric)
prometheus.MustRegister(BackendHandlerResponseCountMetric)
prometheus.MustRegister(BackendHandlerResponseDurationSecondsMetric)
}

0 comments on commit b443d3d

Please sign in to comment.