Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert errors as sentry exception #10

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 3 additions & 38 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package slogsentry

import (
"net/http"
"reflect"

"log/slog"

Expand All @@ -21,18 +20,20 @@ func DefaultConverter(addSource bool, replaceAttr func(groups []string, a slog.A
attrs := slogcommon.AppendRecordAttrsToAttrs(loggerAttr, groups, record)

// developer formatters
attrs = slogcommon.ReplaceError(attrs, ErrorKeys...)
if addSource {
attrs = append(attrs, slogcommon.Source(SourceKey, record))
}
attrs = slogcommon.ReplaceAttrs(replaceAttr, []string{}, 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)
Expand All @@ -46,19 +47,6 @@ func attrToSentryEvent(attr slog.Attr, event *sentry.Event) {
v := attr.Value
kind := attr.Value.Kind()

for _, errorKey := range ErrorKeys {
if attr.Key == errorKey {
if err, ok := attr.Value.Any().(error); ok {
event.Exception = buildExceptions(err)
} else {
if event.User.Data == nil {
event.User.Data = map[string]string{}
}
event.User.Data[errorKey] = slogcommon.AnyValueToString(v)
}
}
}

if k == "dist" && kind == slog.KindString {
event.Dist = v.String()
} else if k == "environment" && kind == slog.KindString {
Expand Down Expand Up @@ -117,26 +105,3 @@ func attrToSentryEvent(attr slog.Attr, event *sentry.Event) {
event.Contexts[ContextKey][attr.Key] = attr.Value.Any()
}
}

func buildExceptions(err error) []sentry.Exception {
exceptions := []sentry.Exception{}

for i := 0; i < 10 && err != nil; i++ {
exceptions = append(exceptions, sentry.Exception{
Value: err.Error(),
Type: reflect.TypeOf(err).String(),
Stacktrace: sentry.ExtractStacktrace(err), // @TODO: use record.pc
})

switch previous := err.(type) {
case interface{ Unwrap() error }:
err = previous.Unwrap()
case interface{ Cause() error }:
err = previous.Cause()
default:
err = nil
}
}

return exceptions
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/getsentry/sentry-go v0.22.0
github.com/samber/slog-common v0.14.0
github.com/samber/slog-common v0.15.0
go.uber.org/goleak v1.2.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/samber/slog-common v0.14.0 h1:g4TGALXmqogbJu3epRhjIWUJKSpgqB9VyN2OEnS8Wyg=
github.com/samber/slog-common v0.14.0/go.mod h1:Qjrfhwk79XiCIhBj8+jTq1Cr0u9rlWbjawh3dWXzaHk=
github.com/samber/slog-common v0.15.0 h1:pPUhFBPsKwjdzmUyOj1F5Ow5bw3w6hkole5CqJDNZoY=
github.com/samber/slog-common v0.15.0/go.mod h1:Qjrfhwk79XiCIhBj8+jTq1Cr0u9rlWbjawh3dWXzaHk=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
Expand Down
Loading