feat: add proxy information in sentry context
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
This commit is contained in:
parent
6a4a144c97
commit
8b132dddd4
@ -6,6 +6,7 @@ import (
|
||||
"net/url"
|
||||
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
)
|
||||
@ -17,6 +18,7 @@ const (
|
||||
contextKeyLayers contextKey = "layers"
|
||||
contextKeyOriginalURL contextKey = "originalURL"
|
||||
contextKeyHandleError contextKey = "handleError"
|
||||
contextKeySentryScope contextKey = "sentryScope"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -82,3 +84,16 @@ func HandleError(ctx context.Context, w http.ResponseWriter, r *http.Request, st
|
||||
|
||||
fn(w, r, status, err)
|
||||
}
|
||||
|
||||
func withSentryScope(ctx context.Context, scope *sentry.Scope) context.Context {
|
||||
return context.WithValue(ctx, contextKeySentryScope, scope)
|
||||
}
|
||||
|
||||
func SentryScope(ctx context.Context) (*sentry.Scope, error) {
|
||||
scope, err := ctxValue[*sentry.Scope](ctx, contextKeySentryScope)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return scope, nil
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/cache"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
@ -76,6 +77,13 @@ func (d *Director) rewriteRequest(r *http.Request) (*http.Request, error) {
|
||||
proxyCtx = withLayers(proxyCtx, layers)
|
||||
r = r.WithContext(proxyCtx)
|
||||
|
||||
if sentryScope, _ := SentryScope(ctx); sentryScope != nil {
|
||||
sentryScope.SetContext("bouncer", sentry.Context{
|
||||
"proxy_name": p.Name,
|
||||
"proxy_target": r.URL.String(),
|
||||
})
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
}
|
||||
@ -216,40 +224,43 @@ func (d *Director) ResponseTransformer() proxy.ResponseTransformer {
|
||||
func (d *Director) Middleware() proxy.Middleware {
|
||||
return func(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := withHandleError(r.Context(), d.handleError)
|
||||
r = r.WithContext(ctx)
|
||||
sentry.ConfigureScope(func(scope *sentry.Scope) {
|
||||
ctx := withHandleError(r.Context(), d.handleError)
|
||||
ctx = withSentryScope(ctx, scope)
|
||||
r = r.WithContext(ctx)
|
||||
|
||||
r, err := d.rewriteRequest(r)
|
||||
if err != nil {
|
||||
HandleError(ctx, w, r, http.StatusInternalServerError, errors.Wrap(err, "could not rewrite request"))
|
||||
return
|
||||
}
|
||||
|
||||
ctx = r.Context()
|
||||
|
||||
layers, err := ctxLayers(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, errContextKeyNotFound) {
|
||||
r, err := d.rewriteRequest(r)
|
||||
if err != nil {
|
||||
HandleError(ctx, w, r, http.StatusInternalServerError, errors.Wrap(err, "could not rewrite request"))
|
||||
return
|
||||
}
|
||||
|
||||
HandleError(ctx, w, r, http.StatusInternalServerError, errors.Wrap(err, "could not retrieve proxy and layers from context"))
|
||||
return
|
||||
}
|
||||
ctx = r.Context()
|
||||
|
||||
httpMiddlewares := make([]proxy.Middleware, 0)
|
||||
for _, layer := range layers {
|
||||
middleware, ok := d.layerRegistry.GetMiddleware(layer.Type)
|
||||
if !ok {
|
||||
continue
|
||||
layers, err := ctxLayers(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, errContextKeyNotFound) {
|
||||
return
|
||||
}
|
||||
|
||||
HandleError(ctx, w, r, http.StatusInternalServerError, errors.Wrap(err, "could not retrieve proxy and layers from context"))
|
||||
return
|
||||
}
|
||||
|
||||
httpMiddlewares = append(httpMiddlewares, middleware.Middleware(layer))
|
||||
}
|
||||
httpMiddlewares := make([]proxy.Middleware, 0)
|
||||
for _, layer := range layers {
|
||||
middleware, ok := d.layerRegistry.GetMiddleware(layer.Type)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
handler := createMiddlewareChain(next, httpMiddlewares)
|
||||
httpMiddlewares = append(httpMiddlewares, middleware.Middleware(layer))
|
||||
}
|
||||
|
||||
handler.ServeHTTP(w, r)
|
||||
handler := createMiddlewareChain(next, httpMiddlewares)
|
||||
|
||||
handler.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
return http.HandlerFunc(fn)
|
||||
|
Loading…
x
Reference in New Issue
Block a user