Skip to content

Commit

Permalink
Reduce the amount of info being sent to Sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
af-afk committed Jun 25, 2024
1 parent 9436d19 commit 9723a22
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions lib/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
package setup

import (
"fmt"
"log"
"log/slog"
"os"
"context"
"runtime/debug"
"strings"
"fmt"
"time"

"github.com/getsentry/sentry-go"
Expand All @@ -24,6 +25,43 @@ const (
EnvSentryDsn = "SPN_SENTRY_DSN"
)

type Multihandler struct {
sentry, json slog.Handler
}

func (h Multihandler) Enabled(ctx context.Context, record slog.Level) bool {
// The JSON handler should be the furthest we're going.
return h.sentry.Enabled(ctx, record)
}
func (h Multihandler) WithAttrs(attrs []slog.Attr) slog.Handler {
var s slog.Handler
if h.sentry != nil {
s = h.sentry.WithAttrs(attrs)
}
return Multihandler{
sentry: s,
json: h.json.WithAttrs(attrs),
}
}
func (h Multihandler) WithGroup(name string) slog.Handler {
var s slog.Handler
if h.sentry != nil {
s = h.sentry.WithGroup(name)
}
return Multihandler{
sentry: s,
json: h.json.WithGroup(name),
}
}
func (h Multihandler) Handle(ctx context.Context, record slog.Record) error {
if s := h.sentry; s != nil {
if err := s.Handle(ctx, record); err != nil {
return err
}
}
return h.json.Handle(ctx, record)
}

func init() {
// Set up the logging to print JSON blobs.
logLevel := slog.LevelInfo
Expand All @@ -41,15 +79,18 @@ func init() {
panic(fmt.Sprintf("failed to set up sentry: %v", err))
}
}
var logger *slog.Logger
var multihandler Multihandler
if dsn != "" { // DSN being set means we're using Sentry.
slogSentryOpts := slogsentry.Option{Level: logLevel}.NewSentryHandler()
logger = slog.New(slogSentryOpts)
} else {
logger = slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: logLevel,
}))
// We want to only track errors with Sentry.
multihandler.sentry = slogsentry.Option{
Level: slog.LevelError,
}.
NewSentryHandler()
}
multihandler.json = slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: logLevel,
})
logger := slog.New(multihandler)
// Find the commit hash (taken straight from
// https:icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/)
var revision string
Expand Down

0 comments on commit 9723a22

Please sign in to comment.