Skip to content

Commit

Permalink
Add HTTP instrumentation for requests in flight
Browse files Browse the repository at this point in the history
Note that the api/v2 calls are currently not instrumented at all. This
adds a TODO note.

Signed-off-by: beorn7 <beorn@soundcloud.com>
  • Loading branch information
beorn7 committed Feb 7, 2019
1 parent b15a8c4 commit 0318737
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func (api *API) Register(
if routePrefix != "/" {
apiPrefix = routePrefix
}
// TODO(beorn7): HTTP instrumentation is only in place for Router. Since
// /api/v2 works on the Handler level, it is currently not instrumented
// at all!
mux.Handle(
apiPrefix+"/api/v2/",
limiter(http.StripPrefix(apiPrefix+"/api/v2", api.v2.Handler)),
Expand Down
17 changes: 14 additions & 3 deletions cmd/alertmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ var (
},
[]string{"handler", "method"},
)
requestsInFlight = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "alertmanager_http_requests_in_flight",
Help: "Current number of HTTP requests being processed.",
},
[]string{"handler", "method"},
)
responseSize = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "alertmanager_http_response_size_bytes",
Expand All @@ -98,11 +105,15 @@ func init() {
}

func instrumentHandler(handlerName string, handler http.HandlerFunc) http.HandlerFunc {
handlerLabel := prometheus.Labels{"handler": handlerName}
return promhttp.InstrumentHandlerDuration(
requestDuration.MustCurryWith(prometheus.Labels{"handler": handlerName}),
requestDuration.MustCurryWith(handlerLabel),
promhttp.InstrumentHandlerResponseSize(
responseSize.MustCurryWith(prometheus.Labels{"handler": handlerName}),
handler,
responseSize.MustCurryWith(handlerLabel),
promhttp.InstrumentHandlerInFlight(
requestsInFlight.MustCurryWith(handlerLabel),
handler,
),
),
)
}
Expand Down

0 comments on commit 0318737

Please sign in to comment.