Skip to content

Commit b443d3d

Browse files
author
Toby Lorne
committed
Instrument backend handler response count
Every time the backend handler responds with a status code, we should count that response Signed-off-by: Toby Lorne <[email protected]>
1 parent ee1b938 commit b443d3d

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

handlers/backend_handler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ func (bt *backendTransport) RoundTrip(req *http.Request) (resp *http.Response, e
144144
"backend_id": bt.backendID,
145145
"response_code": fmt.Sprintf("%d", responseCode),
146146
}).Add(durationSeconds)
147+
148+
BackendHandlerResponseCountMetric.With(prometheus.Labels{
149+
"backend_id": bt.backendID,
150+
"response_code": fmt.Sprintf("%d", responseCode),
151+
}).Inc()
147152
}()
148153

149154
resp, err = bt.wrapped.RoundTrip(req)

handlers/backend_handler_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ var _ = Describe("Backend handler", func() {
141141
Context("metrics", func() {
142142
var (
143143
beforeRequestCountMetric float64
144+
beforeResponseCountMetric float64
144145
beforeResponseDurationSecondsMetric float64
145146
)
146147

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

156+
var measureResponseCount = func(responseCode string) float64 {
157+
return promtest.ToFloat64(
158+
handlers.BackendHandlerResponseCountMetric.With(prometheus.Labels{
159+
"backend_id": "backend-metrics",
160+
"response_code": responseCode,
161+
}),
162+
)
163+
}
164+
155165
var measureResponseDurationSeconds = func(responseCode string) float64 {
156166
return promtest.ToFloat64(
157167
handlers.BackendHandlerResponseDurationSecondsMetric.With(prometheus.Labels{
@@ -179,6 +189,7 @@ var _ = Describe("Backend handler", func() {
179189
rw.WriteHeader(http.StatusOK)
180190
})
181191

192+
beforeResponseCountMetric = measureResponseCount("200")
182193
beforeResponseDurationSecondsMetric = measureResponseDurationSeconds("200")
183194

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

207+
It("should count the number of proxied responses", func() {
208+
Expect(
209+
measureResponseCount("200") - beforeResponseCountMetric,
210+
).To(Equal(float64(1)))
211+
})
212+
196213
It("should record the duration of proxied responses", func() {
197214
Expect(
198215
measureResponseDurationSeconds("200") - beforeResponseDurationSecondsMetric,
@@ -207,6 +224,7 @@ var _ = Describe("Backend handler", func() {
207224
rw.WriteHeader(http.StatusOK)
208225
})
209226

227+
beforeResponseCountMetric = measureResponseCount("504")
210228
beforeResponseDurationSecondsMetric = measureResponseDurationSeconds("504")
211229

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

242+
It("should count the number of proxied responses", func() {
243+
Expect(
244+
measureResponseCount("504") - beforeResponseCountMetric,
245+
).To(Equal(float64(1)))
246+
})
247+
224248
It("should record the duration of proxied responses", func() {
225249
Expect(
226250
measureResponseDurationSeconds("504") - beforeResponseDurationSecondsMetric,

handlers/metrics.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ var (
2727
},
2828
)
2929

30+
BackendHandlerResponseCountMetric = prometheus.NewCounterVec(
31+
prometheus.CounterOpts{
32+
Name: "router_backend_handler_response_count",
33+
Help: "Number of responses returned by router backend handlers",
34+
},
35+
[]string{
36+
"backend_id",
37+
"response_code",
38+
},
39+
)
40+
3041
BackendHandlerResponseDurationSecondsMetric = prometheus.NewCounterVec(
3142
prometheus.CounterOpts{
3243
Name: "router_backend_handler_response_duration_seconds",
@@ -43,5 +54,6 @@ func initMetrics() {
4354
prometheus.MustRegister(RedirectHandlerRedirectCountMetric)
4455

4556
prometheus.MustRegister(BackendHandlerRequestCountMetric)
57+
prometheus.MustRegister(BackendHandlerResponseCountMetric)
4658
prometheus.MustRegister(BackendHandlerResponseDurationSecondsMetric)
4759
}

0 commit comments

Comments
 (0)