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

Report request and error #2

Merged
merged 6 commits into from
Sep 1, 2024
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ type Option struct {
Level slog.Leveler

// Rollbar client
Client *rollbar.Client
Timeout time.Duration // default: 10s
Client *rollbar.Client
Timeout time.Duration // default: 10s
SkipFrames *int // default: 2

// optional: customize Rollbar event builder
Converter Converter
Expand All @@ -101,7 +102,7 @@ type Option struct {

// optional: see slog.HandlerOptions
AddSource bool
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr // default: removeRequestAttr
}
```

Expand Down
91 changes: 78 additions & 13 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@ package slogrollbar

import (
"context"
"log/slog"
"net/http"
"time"

"github.com/rollbar/rollbar-go"
slogcommon "github.com/samber/slog-common"

"log/slog"
)

type wrappedError struct {
msg string
error
}

func (w wrappedError) Unwrap() error {
return w.error
}

func (w wrappedError) Error() string {
return w.msg
}

type Option struct {
// log level (default: debug)
Level slog.Leveler

// Rollbar client
Client *rollbar.Client
Timeout time.Duration // default: 10s
Client *rollbar.Client
Timeout time.Duration // default: 10s
SkipFrames *int // default: 2

// optional: customize Rollbar event builder
Converter Converter
Expand All @@ -25,7 +39,7 @@ type Option struct {

// optional: see slog.HandlerOptions
AddSource bool
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr // default: removeRequestAttr
}

func (o Option) NewRollbarHandler() slog.Handler {
Expand All @@ -45,6 +59,15 @@ func (o Option) NewRollbarHandler() slog.Handler {
o.AttrFromContext = []func(ctx context.Context) []slog.Attr{}
}

if o.ReplaceAttr == nil {
o.ReplaceAttr = removeRequestAttr
}

if o.SkipFrames == nil {
o.SkipFrames = new(int)
*o.SkipFrames = 2
}

return &RollbarHandler{
option: o,
attrs: []slog.Attr{},
Expand All @@ -66,19 +89,47 @@ func (h *RollbarHandler) Enabled(_ context.Context, level slog.Level) bool {

func (h *RollbarHandler) Handle(ctx context.Context, record slog.Record) error {
fromContext := slogcommon.ContextExtractor(ctx, h.option.AttrFromContext)
extra := h.option.Converter(h.option.AddSource, h.option.ReplaceAttr, append(h.attrs, fromContext...), h.groups, &record)
extra := h.option.Converter(h.option.AddSource, h.option.ReplaceAttr,
append(h.attrs, fromContext...), h.groups, &record)
level := LogLevels[record.Level]

ctx, cancel := context.WithTimeout(context.Background(), h.option.Timeout)
defer cancel()

// if level == rollbar.ERR || level == rollbar.CRIT {
// skip := framesToSkip(2)
// h.option.Client.ErrorWithStackSkipWithExtrasAndContext(ctx, rollbar.ERR, err, skip, extra)
// return nil
// }

h.option.Client.MessageWithExtrasAndContext(ctx, level, record.Message, extra)
// extract error and request from slog.Record
var r *http.Request
var err error

record.Attrs(func(a slog.Attr) bool {
if err != nil && r != nil {
return false
}

switch v := a.Value.Any().(type) {
case *http.Request:
// Pass the request to rollbar for additinoal context
r = v
case error:
// Keep the original message, but report as an error to Rollbar
// This enables stack tracing if the error has it.
err = wrappedError{msg: record.Message, error: v}
samber marked this conversation as resolved.
Show resolved Hide resolved
}
return true
})

if err != nil {
if r == nil {
h.option.Client.ErrorWithStackSkipWithExtrasAndContext(ctx, level, err, *h.option.SkipFrames, extra)
} else {
h.option.Client.RequestErrorWithStackSkipWithExtrasAndContext(ctx, level, r, err, *h.option.SkipFrames, extra)
}
} else {
if r == nil {
h.option.Client.MessageWithExtrasAndContext(ctx, level, record.Message, extra)
} else {
h.option.Client.RequestMessageWithExtrasAndContext(ctx, level, r, record.Message, extra)
}
}

return nil
}
Expand All @@ -103,3 +154,17 @@ func (h *RollbarHandler) WithGroup(name string) slog.Handler {
groups: append(h.groups, name),
}
}

func removeRequestAttr(groups []string, a slog.Attr) slog.Attr {
// Leave group untouched
if len(groups) > 1 {
return a
}

// rollbar does not know how send http.Request objects
if _, ok := a.Value.Any().(*http.Request); ok {
return slog.Attr{}
}

return a
}
Loading