feat: add sentry spans to evaluate proxy performances
Cadoles/bouncer/pipeline/head This commit looks good Details

This commit is contained in:
wpetit 2024-09-27 10:46:18 +02:00
parent 867e7c549f
commit 590505e17a
3 changed files with 42 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import (
"forge.cadoles.com/Cadoles/go-proxy"
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
"forge.cadoles.com/cadoles/bouncer/internal/cache"
bouncersentry "forge.cadoles.com/cadoles/bouncer/internal/sentry"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
@ -257,6 +258,7 @@ func (d *Director) Middleware() proxy.Middleware {
}
handler := createMiddlewareChain(next, httpMiddlewares)
handler = bouncersentry.SpanHandler(handler, "director.proxy")
handler.ServeHTTP(w, r)
}

View File

@ -30,6 +30,8 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gitlab.com/wpetit/goweb/logger"
bouncersentry "forge.cadoles.com/cadoles/bouncer/internal/sentry"
)
type Server struct {
@ -189,7 +191,7 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
proxy.WithDefaultHandler(http.HandlerFunc(s.handleDefault)),
)
r.Handle("/*", handler)
r.Handle("/*", bouncersentry.SpanHandler(handler, "http.proxy"))
})
if err := http.Serve(listener, router); err != nil && !errors.Is(err, net.ErrClosed) {

37
internal/sentry/sentry.go Normal file
View File

@ -0,0 +1,37 @@
package sentry
import (
"net/http"
"github.com/getsentry/sentry-go"
)
func SpanHandler(handler http.Handler, name string) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
hub = sentry.CurrentHub().Clone()
ctx = sentry.SetHubOnContext(ctx, hub)
}
options := []sentry.SpanOption{
sentry.WithOpName(name),
sentry.ContinueFromRequest(r),
sentry.WithTransactionSource(sentry.SourceURL),
}
span := sentry.StartSpan(ctx,
name,
options...,
)
defer span.Finish()
r = r.WithContext(span.Context())
handler.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}