Skip to content

Commit 52ca48d

Browse files
committed
Include responseWriterWrapper definition directly in metrics.go
1 parent cb87e55 commit 52ca48d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pkg/middleware/metrics.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,31 @@ import (
88
"github.com/prometheus/client_golang/prometheus"
99
)
1010

11+
// responseWriterWrapper is a custom response writer that captures the status code
12+
type responseWriterWrapper struct {
13+
w http.ResponseWriter
14+
statusCode int
15+
}
16+
17+
func (rww *responseWriterWrapper) Header() http.Header {
18+
return rww.w.Header()
19+
}
20+
21+
func (rww *responseWriterWrapper) Write(bytes []byte) (int, error) {
22+
return rww.w.Write(bytes)
23+
}
24+
25+
func (rww *responseWriterWrapper) WriteHeader(statusCode int) {
26+
rww.statusCode = statusCode
27+
rww.w.WriteHeader(statusCode)
28+
}
29+
30+
func (rww *responseWriterWrapper) Flush() {
31+
if f, ok := rww.w.(http.Flusher); ok {
32+
f.Flush()
33+
}
34+
}
35+
1136
// MetricsMiddleware adds Prometheus metrics to HTTP requests
1237
func MetricsMiddleware(requestCounter *prometheus.CounterVec, requestDuration *prometheus.HistogramVec, activeRequests prometheus.Gauge) func(http.Handler) http.Handler {
1338
return func(next http.Handler) http.Handler {

0 commit comments

Comments
 (0)