Skip to content

Commit

Permalink
Improved how we implement the sentry integration
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Aug 29, 2023
1 parent 0797752 commit 46e82f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ services:
restart: always
build:
context: server/
args:
version: dev # compiled with the git sha in prod
ports:
- 50051:50051
environment:
Expand Down
36 changes: 15 additions & 21 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/textproto"
"os"
"strings"
"time"

pb "github.com/TUM-Dev/Campus-Backend/server/api"
"github.com/TUM-Dev/Campus-Backend/server/backend"
Expand All @@ -32,15 +33,18 @@ import (
"gorm.io/gorm"
)

const (
httpPort = ":50051"
)
const httpPort = ":50051"

// Version is injected at build time by the compiler with the correct git-commit-sha or "dev" in development
var Version = "dev"

//go:embed swagger
var swagfs embed.FS

func main() {
setupTelemetry()
defer sentry.Flush(2 * time.Second) // make sure that sentry handles shutdowns gracefully

// Connect to DB
var conn gorm.Dialector
shouldAutoMigrate := false
Expand Down Expand Up @@ -156,33 +160,23 @@ func main() {
// - sentry to be connected with log
// - logrus to
func setupTelemetry() {
environment := "unknown"
environment := "development"
if env.IsProd() {
environment = "production"
}
if env.IsDev() {
environment = "development"
}

if environment == "production" {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&log.TextFormatter{})
log.SetFormatter(&log.JSONFormatter{}) // simpler to query but harder to parse in the console
}

if sentryDSN := os.Getenv("SENTRY_DSN"); sentryDSN != "" {
if err := sentry.Init(sentry.ClientOptions{
Dsn: os.Getenv("SENTRY_DSN"),
Release: env.GetEnvironment(),
Environment: environment,
Dsn: sentryDSN,
AttachStacktrace: true,
Release: Version,
Dist: Version, // see https://github.com/getsentry/sentry-react-native/issues/516 why this is equal
Environment: environment,
}); err != nil {
log.WithError(err).Error("Sentry initialization failed")
}
log.AddHook(sentryhook.New([]log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
}))
log.AddHook(sentryhook.New([]log.Level{log.PanicLevel, log.FatalLevel, log.ErrorLevel}))
} else {
log.Info("continuing without sentry")
}
Expand Down

0 comments on commit 46e82f2

Please sign in to comment.