2023-01-28 12:25:58 +01:00
|
|
|
package sentrysetup
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SentryConfig struct {
|
|
|
|
|
DSN string `name:"sentry-dsn" env:"SENTRY_DSN" help:"Sentry dsn" default:""`
|
|
|
|
|
Environment string `name:"sentry-environment" env:"SENTRY_ENVIRONMENT" help:"Sentry environment" default:"development"`
|
|
|
|
|
Disabled bool `name:"sentry-disabled" env:"SENTRY_DISABLED" help:"Disable Sentry" default:"false"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sentryInit = sentry.Init
|
|
|
|
|
|
|
|
|
|
func SetupSentry(logger log.Interface, releaseName string, args SentryConfig) error {
|
|
|
|
|
if args.Disabled {
|
2023-09-05 12:26:49 +00:00
|
|
|
logger.Info("Sentry disabled, setup using empty options")
|
|
|
|
|
return sentryInit(sentry.ClientOptions{})
|
2023-01-28 12:25:58 +01:00
|
|
|
}
|
|
|
|
|
if args.Environment == "" {
|
|
|
|
|
return fmt.Errorf("no Sentry environment supplied, exiting")
|
|
|
|
|
}
|
|
|
|
|
cfg := sentry.ClientOptions{
|
|
|
|
|
Dsn: args.DSN,
|
|
|
|
|
Environment: args.Environment,
|
|
|
|
|
Release: releaseName,
|
|
|
|
|
}
|
|
|
|
|
switch args.Environment {
|
|
|
|
|
case "development":
|
|
|
|
|
cfg.Debug = true
|
|
|
|
|
cfg.EnableTracing = false
|
|
|
|
|
cfg.TracesSampleRate = 0.0
|
|
|
|
|
case "staging", "production":
|
|
|
|
|
if args.DSN == "" {
|
|
|
|
|
return fmt.Errorf("no DSN supplied for non-dev environment, exiting")
|
|
|
|
|
}
|
|
|
|
|
cfg.Debug = false
|
|
|
|
|
cfg.EnableTracing = true
|
|
|
|
|
cfg.TracesSampleRate = 1.0
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("illegal environment %s", args.Environment)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := sentryInit(cfg); err != nil {
|
|
|
|
|
return fmt.Errorf("sentry setup: %w", err)
|
|
|
|
|
}
|
|
|
|
|
logger.Infof("configured Sentry for env: %s", args.Environment)
|
|
|
|
|
return nil
|
|
|
|
|
}
|