Skip to content

Commit

Permalink
Attempt to puplate tags from log data when sending to Sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
seedifferently committed Oct 18, 2024
1 parent 83a2512 commit 83ad7f2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions logger/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"strconv"
"time"

"github.com/getsentry/sentry-go"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}

0 comments on commit 83ad7f2

Please sign in to comment.