8000 WIP · coder/coder@4698231 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4698231

Browse files
committed
WIP
1 parent 0431e12 commit 4698231

File tree

3 files changed

+104
-32
lines changed

3 files changed

+104
-32
lines changed

coderd/httpmw/prometheus.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
102102

103103
func getRoutePattern(r *http.Request) string {
104104
rctx := chi.RouteContext(r.Context())
105+
if rctx == nil {
106+
return ""
107+
}
108+
105109
if pattern := rctx.RoutePattern(); pattern != "" {
106110
// Pattern is already available
107111
return pattern
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package httpmw
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/go-chi/chi/v5"
10+
"github.com/prometheus/client_golang/prometheus"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/coder/coder/v2/coderd/tracing"
14+
)
15+
16+
func TestPrometheus(t *testing.T) {
17+
t.Parallel()
18+
t.Run("All", func(t *testing.T) {
19+
t.Parallel()
20+
req := httptest.NewRequest("GET", "/", nil)
21+
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chi.NewRouteContext()))
22+
res := &tracing.StatusWriter{ResponseWriter: httptest.NewRecorder()}
23+
reg := prometheus.NewRegistry()
24+
Prometheus(reg)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
25+
w.WriteHeader(http.StatusOK)
26+
})).ServeHTTP(res, req)
27+
metrics, err := reg.Gather()
28+
require.NoError(t, err)
29+
require.Greater(t, len(metrics), 0)
30+
})
31+
}
32+
33+
func TestGetRoutePattern(t *testing.T) {
34+
t.Parallel()
35+
reg := prometheus.NewRegistry()
36+
promMW := Prometheus(reg)
37+
// Create a test router with some routes
38+
r := chi.NewRouter()
39+
r.With(promMW).Get("/api/v2/workspaces/{workspace}", func(w http.ResponseWriter, r *http.Request) {})
40+
r.With(promMW).Get("/api/v2/users/{user}", func(w http.ResponseWriter, r *http.Request) {})
41+
r.With(promMW).Get("/static/*", func(w http.ResponseWriter, r *http.Request) {})
42+
43+
tests := []struct {
44+
name string
45+
method string
46+
path string
47+
expected string
48+
}{
49+
{
50+
name: "PatternAlreadyAvailable",
51+
method: "GET",
52+
path: "/api/v2/workspaces/test",
53+
expected: "/api/v2/workspaces/{workspace}",
54+
},
55+
{
56+
name: "UserRoute",
57+
method: "GET",
58+
path: "/api/v2/users/john",
59+
expected: "/api/v2/users/{user}",
60+
},
61+
{
62+
name: "StaticRoute",
63+
method: "GET",
64+
path: "/static/css/style.css",
65+
expected: "/static/*",
66+
},
67+
{
68+
name: "NoMatchingRoute",
69+
method: "GET",
70+
path: "/nonexistent",
71+
expected: "",
72+
},
73+
{
74+
name: "FrontendRoute",
75+
method: "GET",
76+
path: "/",
77+
expected: "",
78+
},
79+
}
80+
81+
for _, tt := range tests {
82+
tt := tt
83+
t.Run(tt.name, func(t *testing.T) {
84+
t.Parallel()
85+
86+
req := httptest.NewRequest(tt.method, tt.path, nil)
87+
88+
sw := &tracing.StatusWriter{ResponseWriter: httptest.NewRecorder()}
89+
90+
r.ServeHTTP(sw, req)
91+
92+
metrics, err := reg.Gather()
93+
require.NoError(t, err)
94+
require.Greater(t, len(metrics), 0)
95+
96+
// Verify the result
97+
// require.Equal(t, tt.expected, pattern, "unexpected route pattern")
98+
})
99+
}
100+
}

coderd/httpmw/prometheus_test.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0