-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vivek Singh <[email protected]>
- Loading branch information
1 parent
7e71174
commit 8ed733e
Showing
4 changed files
with
160 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,5 @@ linters: | |
- goerr113 | ||
- exhaustivestruct | ||
- lll | ||
|
||
service: | ||
golangci-lint-version: 1.45.x |
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,66 @@ | ||
// Copyright 2022 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
func init() { | ||
_ = prometheus.Register(totalRequests) | ||
_ = prometheus.Register(httpDuration) | ||
} | ||
|
||
type httpResponseWriter struct { | ||
http.ResponseWriter | ||
statusCode int | ||
} | ||
|
||
func newHTTPResponseWriter(w http.ResponseWriter) *httpResponseWriter { | ||
return &httpResponseWriter{ | ||
w, | ||
http.StatusOK, | ||
} | ||
} | ||
|
||
func (h *httpResponseWriter) WriteHeader(statusCode int) { | ||
h.statusCode = statusCode | ||
h.ResponseWriter.WriteHeader(statusCode) | ||
} | ||
|
||
var totalRequests = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "capsule_proxy_requests_total", | ||
Help: "Number of requests", | ||
}, | ||
[]string{"path", "status"}, | ||
) | ||
|
||
var httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ | ||
Name: "capsule_proxy_response_time_seconds", | ||
Help: "Duration of capsule proxy requests.", | ||
}, []string{"path"}) | ||
|
||
func MetricsMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
route := mux.CurrentRoute(r) | ||
path, _ := route.GetPathTemplate() | ||
|
||
timer := prometheus.NewTimer(httpDuration.WithLabelValues(path)) | ||
|
||
rw := newHTTPResponseWriter(w) | ||
next.ServeHTTP(rw, r) | ||
|
||
statusCode := rw.statusCode | ||
|
||
totalRequests.WithLabelValues(path, strconv.Itoa(statusCode)).Inc() | ||
|
||
timer.ObserveDuration() | ||
}) | ||
} |
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,90 @@ | ||
// Copyright 2022 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/prometheus/client_golang/prometheus" | ||
model "github.com/prometheus/client_model/go" | ||
) | ||
|
||
func dummyHandler(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte("hello")) | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func newRequest(method, url string) (*http.Request, error) { | ||
req, err := http.NewRequest(method, url, nil) | ||
return req, err | ||
} | ||
|
||
func Test_MetricsMiddleware_RequestCount(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
requestCount int | ||
path string | ||
output float64 | ||
}{ | ||
{ | ||
|
||
name: "single request count", | ||
requestCount: 1, | ||
path: "/test", | ||
output: 1, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
router := mux.NewRouter() | ||
router.HandleFunc(test.path, dummyHandler).Methods("GET") | ||
router.Use(MetricsMiddleware) | ||
|
||
rw := httptest.NewRecorder() | ||
|
||
for i := 0; i < test.requestCount; i++ { | ||
req, err := newRequest("GET", test.path) | ||
if err != nil { | ||
t.Errorf("failed to create HTTP request object") | ||
} | ||
router.ServeHTTP(rw, req) | ||
} | ||
|
||
t.Run("regular middleware call", func(t *testing.T) { | ||
ch := make(chan prometheus.Metric) | ||
go totalRequests.Collect(ch) | ||
g := (<-ch).(prometheus.Counter) | ||
result := readVector(g) | ||
if test.output != result.value { | ||
t.Errorf("testcase %s failed. expected: %f, got: %f", test.name, test.output, result.value) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type metricResult struct { | ||
value float64 | ||
labels map[string]string | ||
} | ||
|
||
func labels2Map(labels []*model.LabelPair) map[string]string { | ||
res := map[string]string{} | ||
for _, l := range labels { | ||
res[l.GetName()] = l.GetValue() | ||
} | ||
return res | ||
} | ||
|
||
func readVector(g prometheus.Metric) metricResult { | ||
m := &model.Metric{} | ||
_ = g.Write(m) | ||
|
||
return metricResult{ | ||
value: m.GetCounter().GetValue(), | ||
labels: labels2Map(m.GetLabel()), | ||
} | ||
} |
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