accounts-profiles-endpoint/internal/logging/sentry/sentry.go

61 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-09-22 18:46:25 +05:30
package sentry
import (
"strings"
2024-09-22 18:46:25 +05:30
"github.com/getsentry/sentry-go"
sentryGin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
2024-10-01 05:19:23 +05:30
"ely.by/accounts-profiles-endpoint/internal/version"
2024-09-22 18:46:25 +05:30
)
func InitWithConfig(config *viper.Viper) error {
config.SetDefault("sentry.enable_tracing", false)
config.SetDefault("sentry.traces_sample_rate", 1.0)
config.SetDefault("sentry.profiles_sample_rate", 1.0)
2024-09-22 18:46:25 +05:30
sampleRate := config.GetFloat64("sentry.traces_sample_rate")
return sentry.Init(sentry.ClientOptions{
Dsn: viper.GetString("sentry.dsn"),
EnableTracing: viper.GetBool("sentry.enable_tracing"),
TracesSampler: func(ctx sentry.SamplingContext) float64 {
if !strings.Contains(ctx.Span.Name, "/api") {
2024-09-22 18:46:25 +05:30
return 0
}
return sampleRate
},
ProfilesSampleRate: config.GetFloat64("sentry.profiles_sample_rate"),
Release: version.Version(),
Environment: config.GetString("sentry.environment"),
2024-09-22 18:46:25 +05:30
Integrations: func(integrations []sentry.Integration) []sentry.Integration {
nDeleted := 0
for i, integration := range integrations {
if integration.Name() == "Modules" {
integrations[i] = integrations[len(integrations)-(nDeleted+1)]
nDeleted++
}
}
return integrations[:len(integrations)-nDeleted]
},
})
}
// It seems like this must be a part of the sentrygin package, but it is not, so implement it ourselves
func ErrorMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
sentryHub := sentryGin.GetHubFromContext(c)
if sentryHub != nil {
return
}
for _, err := range c.Errors {
sentryHub.CaptureException(err)
}
}
}