Skip to content

Commit

Permalink
Multi Error Reporting, Custom Report Attrs, and Clean Up
Browse files Browse the repository at this point in the history
Implemented ability to report multiple apm errors from one log. If a user adds
multiple "reportable" error attributes to the log msg (default is "error" & "err"),
instead of trying to join the errors into one or discarding one, the apmslog
handler will report both errors.

Added ability for a user to define what slog attribute keys they want to report
as errors. Because there is no standard way in slog to attach an error to a msg
log, I wanted to add the ability for the user to decide what is and what is not
going to be reported. By default, slog attribute keys that are "error" or "err"
are reported, but with the new `WithErrorRecordAttrs(keys)` function a user
can define which keys will be reported.

Cleaned up `ApmHandler` struct and methods. Since we want the user to use the
included `NewApmHandler` function and its functional option functions, I
decided to make all Struct fields private.

Additionally added a check on if the `ApmHandler`'s `tracer` field is nill before
trying to use it. It is still possible for a user to pass in a nil tracer using
the `WithTracer` functional option.
  • Loading branch information
cmenke authored and charliemenke committed Apr 13, 2024
1 parent d95e553 commit 043bfb8
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 2 deletions.
2 changes: 1 addition & 1 deletion module/apmslog/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ func ExampleHandler() {
// the log msg with its error will be reported to apm
logger.ErrorContext(ctx, "I will report this error to apm", "error", errors.New("new error"))

// the log msg and BOTH errors will be reported to apm. [ error, err ] slog attribute keys are by default reported
// BOTH errors with the log msg will be reported to apm. [ error, err ] slog attribute keys are by default reported
logger.ErrorContext(ctx, "I will report this error to apm", "error", errors.New("new error"), "err", errors.New("new err"))
}
1 change: 0 additions & 1 deletion module/apmslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func (h *ApmHandler) WithGroup(name string) slog.Handler {
}

func (h *ApmHandler) Handle(ctx context.Context, r slog.Record) error {

// attempt to extract any available trace info from context
var traceId apm.TraceID
var transactionId apm.SpanID
Expand Down

0 comments on commit 043bfb8

Please sign in to comment.