2022-06-10 20:19:48 +00:00
|
|
|
package sentry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type contextSentryNoSample struct{}
|
|
|
|
|
|
|
|
func SentryNoSample(handler func(rw http.ResponseWriter, r *http.Request)) func(http.ResponseWriter, *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := context.WithValue(r.Context(), contextSentryNoSample{}, true)
|
|
|
|
handler(w, r.WithContext(ctx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SentryNoSampleMiddleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := context.WithValue(r.Context(), contextSentryNoSample{}, true)
|
|
|
|
h.ServeHTTP(rw, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:20:41 +00:00
|
|
|
func SamplerFunc(defaultRate float64) sentry.TracesSampler {
|
|
|
|
return func(ctx sentry.SamplingContext) float64 {
|
2022-06-10 20:19:48 +00:00
|
|
|
data, ok := ctx.Span.Context().Value(contextSentryNoSample{}).(bool)
|
|
|
|
if data && ok {
|
2022-12-09 11:20:41 +00:00
|
|
|
return 0
|
2022-06-10 20:19:48 +00:00
|
|
|
}
|
2022-12-09 11:20:41 +00:00
|
|
|
return defaultRate
|
|
|
|
}
|
2022-06-10 20:19:48 +00:00
|
|
|
}
|