rentease/internal/server/tracing.go
2025-11-02 21:45:37 +01:00

35 lines
776 B
Go

package server
import (
"fmt"
"net/http"
"github.com/getsentry/sentry-go"
)
func SentryTracingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(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)
r = r.WithContext(ctx)
}
options := []sentry.SpanOption{
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(r),
sentry.WithTransactionSource(sentry.SourceURL),
}
transaction := sentry.StartTransaction(
ctx,
fmt.Sprintf("%s %s", r.Method, r.URL.Path),
options...,
)
defer transaction.Finish()
next.ServeHTTP(w, r.WithContext(transaction.Context()))
})
}