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 7d65c23
Show file tree
Hide file tree
Showing 6 changed files with 2,413 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
Loading

0 comments on commit 7d65c23

Please sign in to comment.