From 83ad7f24ce5dc33f814fdcfffa321383c50c5020 Mon Sep 17 00:00:00 2001 From: Seth Davis Date: Fri, 18 Oct 2024 15:52:30 -0400 Subject: [PATCH] Attempt to puplate tags from log data when sending to Sentry --- logger/sentry.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/logger/sentry.go b/logger/sentry.go index c0105ff..43b09a1 100644 --- a/logger/sentry.go +++ b/logger/sentry.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log/slog" + "strconv" "time" "github.com/getsentry/sentry-go" @@ -96,6 +97,10 @@ func (sl *SentryLogger) send(level sentry.Level, ctx *LogContext) { scope.SetContext("data", ctx.Data) } + // attempt to populate tags from Data + tags := convertMapAnyToString(ctx.Data) + scope.SetTags(tags) + scope.AddEventProcessor(skipBackFrames(sl.Skip())) scope.SetLevel(level) @@ -128,3 +133,25 @@ func skipBackFrames(skip int) func(*sentry.Event, *sentry.EventHint) *sentry.Eve return event } } + +// convertMapAnyToString converts supported map[string]any values to a map[string]string +func convertMapAnyToString(in map[string]any) map[string]string { + out := make(map[string]string) + + for k, v := range in { + switch val := v.(type) { + case string: + out[k] = val + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: + out[k] = fmt.Sprintf("%d", val) + case float32, float64: + out[k] = fmt.Sprintf("%f", val) + case bool: + out[k] = strconv.FormatBool(val) + default: + // Silently skip unsupported types + } + } + + return out +}