mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2026-09-30 03:31:27 +02:00
Add Prometheus metrics endpoint
Add the Prometheus http.Handler to serve metrics at MetricsPath ("/metrics"
by default). This allows Prometheus to scrape metrics from OAuth2 Proxy.
Add a new middleware NewRequestMetrics and attach it to the preAuth
chain. This will collect metrics on all requests made to OAuth2 Proxy
Collapse some calls to Prinf() and os.Exit(1) to Fatalf as they are
equivalent. main() has a strict 50 lines limit so brevity in these
calls appreciated
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/justinas/alice"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
// DefaultMetricsHandler is the default http.Handler for serving metrics from
|
||||
// the default prometheus.Registry
|
||||
var DefaultMetricsHandler = NewMetricsHandlerWithDefaultRegistry()
|
||||
|
||||
// NewMetricsHandlerWithDefaultRegistry creates a new http.Handler for serving
|
||||
// metrics from the default prometheus.Registry.
|
||||
func NewMetricsHandlerWithDefaultRegistry() http.Handler {
|
||||
return NewMetricsHandler(prometheus.DefaultRegisterer, prometheus.DefaultGatherer)
|
||||
}
|
||||
|
||||
// NewMetricsHandler creates a new http.Handler for serving metrics from the
|
||||
// provided prometheus.Registerer and prometheus.Gatherer
|
||||
func NewMetricsHandler(registerer prometheus.Registerer, gatherer prometheus.Gatherer) http.Handler {
|
||||
return promhttp.InstrumentMetricHandler(
|
||||
registerer, promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{}),
|
||||
)
|
||||
}
|
||||
|
||||
// NewRequestMetricsWithDefaultRegistry returns a middleware that will record
|
||||
// metrics for HTTP requests to the default prometheus.Registry
|
||||
func NewRequestMetricsWithDefaultRegistry() alice.Constructor {
|
||||
return NewRequestMetrics(prometheus.DefaultRegisterer)
|
||||
}
|
||||
|
||||
// NewRequestMetrics returns a middleware that will record metrics for HTTP
|
||||
// requests to the provided prometheus.Registerer
|
||||
func NewRequestMetrics(registerer prometheus.Registerer) alice.Constructor {
|
||||
return func(next http.Handler) http.Handler {
|
||||
// Counter for all requests
|
||||
// This is bucketed based on the response code we set
|
||||
counterHandler := func(next http.Handler) http.Handler {
|
||||
return promhttp.InstrumentHandlerCounter(registerRequestsCounter(registerer), next)
|
||||
}
|
||||
|
||||
// Gauge to all requests currently being handled
|
||||
inFlightHandler := func(next http.Handler) http.Handler {
|
||||
return promhttp.InstrumentHandlerInFlight(registerInflightRequestsGauge(registerer), next)
|
||||
}
|
||||
|
||||
// The latency of all requests bucketed by HTTP method
|
||||
durationHandler := func(next http.Handler) http.Handler {
|
||||
return promhttp.InstrumentHandlerDuration(registerRequestsLatencyHistogram(registerer), next)
|
||||
}
|
||||
|
||||
return alice.New(counterHandler, inFlightHandler, durationHandler).Then(next)
|
||||
}
|
||||
}
|
||||
|
||||
// registerRequestsCounter registers the 'oauth2_proxy_requests_total' metric
|
||||
// This keeps a tally of all received requests bucket by their HTTP response
|
||||
// status code
|
||||
func registerRequestsCounter(registerer prometheus.Registerer) *prometheus.CounterVec {
|
||||
counter := prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "oauth2_proxy_requests_total",
|
||||
Help: "Total number of requests by HTTP status code.",
|
||||
},
|
||||
[]string{"code"},
|
||||
)
|
||||
|
||||
if err := registerer.Register(counter); err != nil {
|
||||
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
|
||||
counter = are.ExistingCollector.(*prometheus.CounterVec)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return counter
|
||||
}
|
||||
|
||||
// registerInflightRequestsGauge registers 'oauth2_proxy_requests_in_flight'
|
||||
// This only keeps the count of currently in progress HTTP requests
|
||||
func registerInflightRequestsGauge(registerer prometheus.Registerer) prometheus.Gauge {
|
||||
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "oauth2_proxy_requests_in_flight",
|
||||
Help: "Current number of requests being served.",
|
||||
})
|
||||
|
||||
if err := registerer.Register(gauge); err != nil {
|
||||
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
|
||||
gauge = are.ExistingCollector.(prometheus.Gauge)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return gauge
|
||||
}
|
||||
|
||||
// registerRequestsLatencyHistogram registers 'oauth2_proxy_response_duration_seconds'
|
||||
// This keeps tally of the requests bucketed by the time taken to process the request
|
||||
func registerRequestsLatencyHistogram(registerer prometheus.Registerer) *prometheus.HistogramVec {
|
||||
histogram := prometheus.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Name: "oauth2_proxy_response_duration_seconds",
|
||||
Help: "A histogram of request latencies.",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
},
|
||||
[]string{"method"},
|
||||
)
|
||||
|
||||
if err := registerer.Register(histogram); err != nil {
|
||||
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
|
||||
histogram = are.ExistingCollector.(*prometheus.HistogramVec)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return histogram
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/ginkgo/extensions/table"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/testutil"
|
||||
)
|
||||
|
||||
var _ = Describe("Instrumentation suite", func() {
|
||||
type requestTableInput struct {
|
||||
registry *prometheus.Registry
|
||||
requestString string
|
||||
expectedHandler http.Handler
|
||||
expectedMetrics []string
|
||||
expectedStatus int
|
||||
// Prometheus output is large so is stored in testdata
|
||||
expectedResultsFile string
|
||||
}
|
||||
|
||||
DescribeTable("when serving a request",
|
||||
func(in *requestTableInput) {
|
||||
req := httptest.NewRequest("", in.requestString, nil)
|
||||
|
||||
rw := httptest.NewRecorder()
|
||||
|
||||
handler := NewRequestMetrics(in.registry)(in.expectedHandler)
|
||||
handler.ServeHTTP(rw, req)
|
||||
|
||||
Expect(rw.Code).To(Equal(in.expectedStatus))
|
||||
|
||||
expectedPrometheusText, err := os.Open(in.expectedResultsFile)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = testutil.GatherAndCompare(in.registry, expectedPrometheusText, in.expectedMetrics...)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
},
|
||||
Entry("successfully", func() *requestTableInput {
|
||||
in := &requestTableInput{
|
||||
registry: prometheus.NewRegistry(),
|
||||
requestString: "http://example.com/metrics",
|
||||
expectedMetrics: []string{
|
||||
"oauth2_proxy_requests_total",
|
||||
},
|
||||
expectedStatus: 200,
|
||||
expectedResultsFile: "testdata/metrics/successfulrequest.txt",
|
||||
}
|
||||
in.expectedHandler = NewMetricsHandler(in.registry, in.registry)
|
||||
|
||||
return in
|
||||
}()),
|
||||
Entry("with not found", &requestTableInput{
|
||||
registry: prometheus.NewRegistry(),
|
||||
requestString: "http://example.com/",
|
||||
expectedHandler: http.NotFoundHandler(),
|
||||
expectedMetrics: []string{"oauth2_proxy_requests_total"},
|
||||
expectedStatus: 404,
|
||||
expectedResultsFile: "testdata/metrics/notfoundrequest.txt",
|
||||
}),
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,3 @@
|
||||
# HELP oauth2_proxy_requests_total Total number of requests by HTTP status code.
|
||||
# TYPE oauth2_proxy_requests_total counter
|
||||
oauth2_proxy_requests_total{code="404"} 1
|
||||
@@ -0,0 +1,3 @@
|
||||
# HELP oauth2_proxy_requests_total Total number of requests by HTTP status code.
|
||||
# TYPE oauth2_proxy_requests_total counter
|
||||
oauth2_proxy_requests_total{code="200"} 1
|
||||
Reference in New Issue
Block a user