mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
feat: add rbac tracing (#4093)
This commit is contained in:
@ -6,13 +6,14 @@ import (
|
||||
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
"github.com/coder/coder/coderd/tracing"
|
||||
)
|
||||
|
||||
func Logger(log slog.Logger) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
sw := &httpapi.StatusWriter{ResponseWriter: w}
|
||||
sw := &tracing.StatusWriter{ResponseWriter: w}
|
||||
|
||||
httplog := log.With(
|
||||
slog.F("host", httpapi.RequestHost(r)),
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
"github.com/coder/coder/coderd/tracing"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
@ -67,9 +68,9 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
|
||||
rctx = chi.RouteContext(r.Context())
|
||||
)
|
||||
|
||||
sw, ok := w.(*httpapi.StatusWriter)
|
||||
sw, ok := w.(*tracing.StatusWriter)
|
||||
if !ok {
|
||||
panic("dev error: http.ResponseWriter is not *httpapi.StatusWriter")
|
||||
panic("dev error: http.ResponseWriter is not *tracing.StatusWriter")
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -10,8 +10,8 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
"github.com/coder/coder/coderd/httpmw"
|
||||
"github.com/coder/coder/coderd/tracing"
|
||||
)
|
||||
|
||||
func TestPrometheus(t *testing.T) {
|
||||
@ -20,7 +20,7 @@ func TestPrometheus(t *testing.T) {
|
||||
t.Parallel()
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chi.NewRouteContext()))
|
||||
res := &httpapi.StatusWriter{ResponseWriter: httptest.NewRecorder()}
|
||||
res := &tracing.StatusWriter{ResponseWriter: httptest.NewRecorder()}
|
||||
reg := prometheus.NewRegistry()
|
||||
httpmw.Prometheus(reg)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
"github.com/coder/coder/coderd/tracing"
|
||||
)
|
||||
|
||||
func Recover(log slog.Logger) func(h http.Handler) http.Handler {
|
||||
@ -22,7 +23,7 @@ func Recover(log slog.Logger) func(h http.Handler) http.Handler {
|
||||
)
|
||||
|
||||
var hijacked bool
|
||||
if sw, ok := w.(*httpapi.StatusWriter); ok {
|
||||
if sw, ok := w.(*tracing.StatusWriter); ok {
|
||||
hijacked = sw.Hijacked
|
||||
}
|
||||
|
||||
|
@ -8,8 +8,8 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cdr.dev/slog/sloggers/slogtest"
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
"github.com/coder/coder/coderd/httpmw"
|
||||
"github.com/coder/coder/coderd/tracing"
|
||||
)
|
||||
|
||||
func TestRecover(t *testing.T) {
|
||||
@ -60,7 +60,7 @@ func TestRecover(t *testing.T) {
|
||||
var (
|
||||
log = slogtest.Make(t, nil)
|
||||
r = httptest.NewRequest("GET", "/", nil)
|
||||
w = &httpapi.StatusWriter{
|
||||
w = &tracing.StatusWriter{
|
||||
ResponseWriter: httptest.NewRecorder(),
|
||||
Hijacked: c.Hijack,
|
||||
}
|
||||
|
Reference in New Issue
Block a user