mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-01-24 05:26:55 +02:00
a7c8a233ba
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
68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
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",
|
|
}),
|
|
)
|
|
})
|