Skip to content

Commit

Permalink
track response time for http endpoints (#1412)
Browse files Browse the repository at this point in the history
* track response time for http endpoints
  • Loading branch information
SimoneDutto authored Oct 23, 2024
1 parent 818feff commit 1ef9fb8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/jimmsrv/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ func NewService(ctx context.Context, p Params) (*Service, error) {
s := new(Service)
s.mux = chi.NewRouter()

s.mux.Use(middleware.MeasureHTTPResponseTime)

// Setup all dependency services

if p.ControllerUUID == "" {
Expand Down
44 changes: 44 additions & 0 deletions internal/middleware/responsetime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 Canonical.

package middleware

import (
"net/http"
"strconv"
"time"

"github.com/go-chi/chi/v5"

"github.com/canonical/jimm/v3/internal/servermon"
)

// statusRecorder to record the status code from the ResponseWriter
type statusRecorder struct {
http.ResponseWriter
statusCode int
}

func (rec *statusRecorder) WriteHeader(statusCode int) {
rec.statusCode = statusCode
rec.ResponseWriter.WriteHeader(statusCode)
}

// MeasureHTTPResponseTime tracks response time of HTTP requests.
// We don't track websocket requests.
func MeasureHTTPResponseTime(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check the upgrade header because we only track http endpoints
if r.Header.Get("Upgrade") == "websocket" {
next.ServeHTTP(w, r)
return
}
rec := statusRecorder{w, 200}
start := time.Now()
defer func() {
route := chi.RouteContext(r.Context()).RoutePattern()
statusCode := strconv.Itoa(rec.statusCode)
servermon.ResponseTimeHistogram.WithLabelValues(route, r.Method, statusCode).Observe(time.Since(start).Seconds())
}()
next.ServeHTTP(&rec, r)
})
}
7 changes: 7 additions & 0 deletions internal/servermon/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ var (
Name: "controller",
Help: "The number of controllers managed by JIMM.",
})
ResponseTimeHistogram = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "jimm",
Subsystem: "http",
Name: "request_duration_seconds",
Help: "The duration of handling an HTTP request in seconds.",
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10},
}, []string{"route", "method", "status_code"})
)

// DurationObserver returns a function that, when run with `defer` will
Expand Down

0 comments on commit 1ef9fb8

Please sign in to comment.