53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
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 {
|
|
logger.Info("Sentry disabled, skipping setup")
|
|
return nil
|
|
}
|
|
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
|
|
}
|