Skip to content

Commit

Permalink
fix: sentry request logging
Browse files Browse the repository at this point in the history
chore: attach release version info to sentry events
  • Loading branch information
muety committed Sep 7, 2024
1 parent ddffd46 commit 0580492
Show file tree
Hide file tree
Showing 7 changed files with 2,414 additions and 2,427 deletions.
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,14 @@ func Get() *Config {

func Load(configFlag string, version string) *Config {
config := &Config{}

if err := configor.New(&configor.Config{}).Load(config, configFlag); err != nil {
Log().Fatal("failed to read config", "error", err)
}

env = config.Env

InitLogger(config.IsDev())

config.Version = strings.TrimSpace(version)
tagVersionMatch, _ := regexp.MatchString(`\d+\.\d+\.\d+`, version)
if tagVersionMatch {
Expand Down Expand Up @@ -514,7 +515,7 @@ func Load(configFlag string, version string) *Config {
config.Sentry.Environment = config.Env
}
slog.Info("enabling sentry integration", "environment", config.Sentry.Environment)
initSentry(config.Sentry, config.IsDev())
initSentry(config.Sentry, config.IsDev(), config.Version)
}

if config.App.DataRetentionMonths <= 0 {
Expand Down
16 changes: 16 additions & 0 deletions config/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package config

import (
"log/slog"
"os"
)

func InitLogger(isDev bool) {
var handler slog.Handler
if isDev {
handler = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})
} else {
handler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})
}
slog.SetDefault(slog.New(handler))
}
16 changes: 8 additions & 8 deletions config/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func (l *SentryLogger) Fatal(msg string, args ...any) {
}

func (l *SentryLogger) Request(r *http.Request) *slog.Logger {
return l.Logger.With(slog.Any("http_request", r))
l.Logger = l.Logger.With("request", r)
if uid := getPrincipal(r); uid != "" {
l.Logger = l.Logger.With(slog.Group("user", slog.String("id", uid)))
}
return l.Logger
}

var excludedRoutes = []string{
Expand All @@ -55,11 +59,12 @@ var excludedRoutes = []string{
"GET /docs",
}

func initSentry(config sentryConfig, debug bool) {
func initSentry(config sentryConfig, debug bool, releaseVersion string) {
if err := sentry.Init(sentry.ClientOptions{
Dsn: config.Dsn,
Debug: debug,
Environment: config.Environment,
Release: releaseVersion,
AttachStacktrace: true,
EnableTracing: config.EnableTracing,
TracesSampler: func(ctx sentry.SamplingContext) float64 {
Expand All @@ -75,12 +80,7 @@ func initSentry(config sentryConfig, debug bool) {
return float64(config.SampleRate)
},
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
r, ok := event.Contexts["extra"]["http_request"]
if ok {
if uid := getPrincipal(r.(*http.Request)); uid != "" {
event.User.ID = uid
}
}
// optional pre-processing before sending the event off
return event
},
}); err != nil {
Expand Down
4,783 changes: 2,384 additions & 2,399 deletions coverage/coverage.out

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ require (
gorm.io/gorm v1.25.11
)

require (
github.com/samber/lo v1.44.0 // indirect
github.com/samber/slog-common v0.17.0 // indirect
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
Expand Down Expand Up @@ -79,6 +74,8 @@ require (
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/samber/lo v1.44.0 // indirect
github.com/samber/slog-common v0.17.0 // indirect
github.com/samber/slog-sentry/v2 v2.8.0
github.com/stretchr/objx v0.5.2 // indirect
github.com/swaggo/files v1.0.1 // indirect
Expand Down
13 changes: 0 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ func main() {
// Configure Swagger docs
docs.SwaggerInfo.BasePath = config.Server.BasePath + "/api"

initLogger()

slog.Info("Wakapi", "version", version)

// Set up GORM
Expand Down Expand Up @@ -440,14 +438,3 @@ func listen(handler http.Handler) {

<-make(chan interface{}, 1)
}

func initLogger() {
var handler slog.Handler
if config.IsDev() {
handler = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})
} else {
handler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})
}
l := slog.New(handler)
slog.SetDefault(l)
}
1 change: 1 addition & 0 deletions middlewares/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middlewares

// Borrowed from https://gist.github.com/elithrar/887d162dfd0c539b700ab4049c76e22b
// Alternatively, we could use https://github.com/samber/slog-chi, however, it pulls in another bunch of dependencies and log messages are more verbose and feel almost little bloated

import (
"io"
Expand Down

0 comments on commit 0580492

Please sign in to comment.