Skip to content

Commit

Permalink
instrument http handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Dec 31, 2024
1 parent ea48bf8 commit 6efc244
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions internal/app/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func Mux(
if useLoggingMW {
commonMiddlewares = append(commonMiddlewares, middleware.LogRequest)
}
if cfg.Prometheus.Enabled && cfg.Prometheus.InstrumentHTTPHandlers {
commonMiddlewares = append(commonMiddlewares, middleware.HTTPServerInstrumentation)
}

basicMiddlewares := append([]alice.Constructor{}, commonMiddlewares...)
basicChain := alice.New(basicMiddlewares...)
Expand Down
5 changes: 3 additions & 2 deletions internal/configtypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ type UsageStats struct {
}

type Prometheus struct {
Enabled bool `mapstructure:"enabled" json:"enabled" envconfig:"enabled" yaml:"enabled" toml:"enabled"`
HandlerPrefix string `mapstructure:"handler_prefix" json:"handler_prefix" envconfig:"handler_prefix" default:"/metrics" yaml:"handler_prefix" toml:"handler_prefix"`
Enabled bool `mapstructure:"enabled" json:"enabled" envconfig:"enabled" yaml:"enabled" toml:"enabled"`
HandlerPrefix string `mapstructure:"handler_prefix" json:"handler_prefix" envconfig:"handler_prefix" default:"/metrics" yaml:"handler_prefix" toml:"handler_prefix"`
InstrumentHTTPHandlers bool `mapstructure:"instrument_http_handlers" json:"instrument_http_handlers" envconfig:"instrument_http_handlers" yaml:"instrument_http_handlers" toml:"instrument_http_handlers"`
}

type Health struct {
Expand Down
38 changes: 38 additions & 0 deletions internal/middleware/http_instrumentation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package middleware

import (
"net/http"
"strconv"

"github.com/prometheus/client_golang/prometheus"
)

var (
httpRequestsTotal *prometheus.CounterVec
)

func init() {
httpRequestsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "centrifugo",
Subsystem: "node",
Name: "incoming_http_requests_total",
Help: "Number of incoming HTTP requests",
},
[]string{"path", "method", "status"},
)
_ = prometheus.DefaultRegisterer.Register(httpRequestsTotal)
}

// HTTPServerInstrumentation is a middleware to instrument HTTP handlers.
// Since it adds and extra layer of response wrapping Centrifugo doesn't use it by default.
// Note, we can not simply collect durations here because we have handlers with long-lived
// connections which require special care. So for now we just count requests.
func HTTPServerInstrumentation(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rw := &statusResponseWriter{w, http.StatusOK}
next.ServeHTTP(rw, r)
status := strconv.Itoa(rw.status)
httpRequestsTotal.WithLabelValues(r.URL.Path, r.Method, status).Inc()
})
}

0 comments on commit 6efc244

Please sign in to comment.