forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.*: update prometheus client, remove deprecated InstrumentHandler and…
… InstrumentHandlerFunc methods (thanos-io#1314) * Update prometheus client, remove deprecated InstrumentHandler method * Remove package level variables. Rename package * Do not use global default registry * Update CHANGELOG * Update CHANGELOG * Simplify API for handler * Add missing comment periods * Rename instrumentation handler package
- Loading branch information
Showing
14 changed files
with
170 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
// InstrumentationMiddleware holds necessary metrics to instrument an http.Server | ||
// and provides necessary behaviors. | ||
type InstrumentationMiddleware interface { | ||
// NewHandler wraps the given HTTP handler for instrumentation. | ||
NewHandler(handlerName string, handler http.Handler) http.HandlerFunc | ||
} | ||
|
||
type nopInstrumentationMiddleware struct{} | ||
|
||
func (ins nopInstrumentationMiddleware) NewHandler(handlerName string, handler http.Handler) http.HandlerFunc { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
handler.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
// NewNopInstrumentationMiddleware provides a InstrumentationMiddleware which does nothing. | ||
func NewNopInstrumentationMiddleware() InstrumentationMiddleware { | ||
return nopInstrumentationMiddleware{} | ||
} | ||
|
||
type defaultInstrumentationMiddleware struct { | ||
requestDuration *prometheus.HistogramVec | ||
requestSize *prometheus.SummaryVec | ||
requestsTotal *prometheus.CounterVec | ||
responseSize *prometheus.SummaryVec | ||
} | ||
|
||
// NewInstrumentationMiddleware provides default InstrumentationMiddleware. | ||
func NewInstrumentationMiddleware(reg *prometheus.Registry) InstrumentationMiddleware { | ||
ins := defaultInstrumentationMiddleware{ | ||
requestDuration: prometheus.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Name: "http_request_duration_seconds", | ||
Help: "Tracks the latencies for HTTP requests.", | ||
}, | ||
[]string{"code", "handler", "method"}, | ||
), | ||
|
||
requestSize: prometheus.NewSummaryVec( | ||
prometheus.SummaryOpts{ | ||
Name: "http_request_size_bytes", | ||
Help: "Tracks the size of HTTP requests.", | ||
}, | ||
[]string{"code", "handler", "method"}, | ||
), | ||
|
||
requestsTotal: prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "http_requests_total", | ||
Help: "Tracks the number of HTTP requests.", | ||
}, []string{"code", "handler", "method"}, | ||
), | ||
|
||
responseSize: prometheus.NewSummaryVec( | ||
prometheus.SummaryOpts{ | ||
Name: "http_response_size_bytes", | ||
Help: "Tracks the size of HTTP responses.", | ||
}, | ||
[]string{"code", "handler", "method"}, | ||
), | ||
} | ||
reg.MustRegister(ins.requestDuration, ins.requestSize, ins.requestsTotal, ins.responseSize) | ||
return &ins | ||
} | ||
|
||
// NewHandler wraps the given HTTP handler for instrumentation. It | ||
// registers four metric collectors (if not already done) and reports HTTP | ||
// metrics to the (newly or already) registered collectors: http_requests_total | ||
// (CounterVec), http_request_duration_seconds (Histogram), | ||
// http_request_size_bytes (Summary), http_response_size_bytes (Summary). Each | ||
// has a constant label named "handler" with the provided handlerName as | ||
// value. http_requests_total is a metric vector partitioned by HTTP method | ||
// (label name "method") and HTTP status code (label name "code"). | ||
func (ins *defaultInstrumentationMiddleware) NewHandler(handlerName string, handler http.Handler) http.HandlerFunc { | ||
return promhttp.InstrumentHandlerDuration( | ||
ins.requestDuration.MustCurryWith(prometheus.Labels{"handler": handlerName}), | ||
promhttp.InstrumentHandlerRequestSize( | ||
ins.requestSize.MustCurryWith(prometheus.Labels{"handler": handlerName}), | ||
promhttp.InstrumentHandlerCounter( | ||
ins.requestsTotal.MustCurryWith(prometheus.Labels{"handler": handlerName}), | ||
promhttp.InstrumentHandlerResponseSize( | ||
ins.responseSize.MustCurryWith(prometheus.Labels{"handler": handlerName}), | ||
handler, | ||
), | ||
), | ||
), | ||
) | ||
} |
Oops, something went wrong.