-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
converter.go
123 lines (109 loc) · 3.6 KB
/
converter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package slogsentry
import (
"encoding"
"fmt"
"net/http"
"log/slog"
"github.com/getsentry/sentry-go"
slogcommon "github.com/samber/slog-common"
)
var SourceKey = "source"
var ContextKey = "extra"
var ErrorKeys = []string{"error", "err"}
type Converter func(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record, hub *sentry.Hub) *sentry.Event
func DefaultConverter(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record, hub *sentry.Hub) *sentry.Event {
// aggregate all attributes
attrs := slogcommon.AppendRecordAttrsToAttrs(loggerAttr, groups, record)
// developer formatters
if addSource {
attrs = append(attrs, slogcommon.Source(SourceKey, record))
}
attrs = slogcommon.ReplaceAttrs(replaceAttr, []string{}, attrs...)
attrs = slogcommon.RemoveEmptyAttrs(attrs)
// Removes err attribute from `attrs`
attrs, err := slogcommon.ExtractError(attrs, ErrorKeys...)
// handler formatter
event := sentry.NewEvent()
event.Timestamp = record.Time.UTC()
event.Level = LogLevels[record.Level]
event.Message = record.Message
event.Logger = name
event.SetException(err, 10)
for i := range attrs {
attrToSentryEvent(attrs[i], event)
}
return event
}
func attrToSentryEvent(attr slog.Attr, event *sentry.Event) {
k := attr.Key
v := attr.Value
kind := v.Kind()
switch {
case k == "dist" && kind == slog.KindString:
event.Dist = v.String()
case k == "environment" && kind == slog.KindString:
event.Environment = v.String()
case k == "event_id" && kind == slog.KindString:
event.EventID = sentry.EventID(v.String())
case k == "platform" && kind == slog.KindString:
event.Platform = v.String()
case k == "release" && kind == slog.KindString:
event.Release = v.String()
case k == "server_name" && kind == slog.KindString:
event.ServerName = v.String()
case k == "tags" && kind == slog.KindGroup:
event.Tags = slogcommon.AttrsToString(v.Group()...)
case k == "transaction" && kind == slog.KindString:
event.Transaction = v.String()
case k == "user" && kind == slog.KindGroup:
data := slogcommon.AttrsToString(v.Group()...)
if id, ok := data["id"]; ok {
event.User.ID = id
delete(data, "id")
}
if email, ok := data["email"]; ok {
event.User.Email = email
delete(data, "email")
}
if ipAddress, ok := data["ip_address"]; ok {
event.User.IPAddress = ipAddress
delete(data, "ip_address")
}
if username, ok := data["username"]; ok {
event.User.Username = username
delete(data, "username")
}
if name, ok := data["name"]; ok {
event.User.Name = name
delete(data, "name")
}
if segment, ok := data["segment"]; ok {
event.User.Segment = segment
delete(data, "segment")
}
event.User.Data = data
case k == "request" && kind == slog.KindAny:
if req, ok := v.Any().(http.Request); ok {
event.Request = sentry.NewRequest(&req)
} else if req, ok := v.Any().(*http.Request); ok {
event.Request = sentry.NewRequest(req)
} else {
if tm, ok := v.Any().(encoding.TextMarshaler); ok {
data, err := tm.MarshalText()
if err == nil {
event.User.Data["request"] = string(data)
} else {
event.User.Data["request"] = fmt.Sprintf("%v", v.Any())
}
}
}
case kind == slog.KindGroup:
event.Contexts[k] = slogcommon.AttrsToMap(v.Group()...)
default:
// "context" should not be added to underlying context layers (see slog.KindGroup case).
if _, ok := event.Contexts[ContextKey]; !ok {
event.Contexts[ContextKey] = make(map[string]any, 0)
}
event.Contexts[ContextKey][k] = v.Any()
}
}