Skip to content
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
9 changes: 7 additions & 2 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ func (c *core) Write(ent zapcore.Entry, fs []zapcore.Field) error {
}
}

var hint *sentry.EventHint

event := sentry.NewEvent()
event.Message = ent.Message
event.Timestamp = ent.Time
Expand All @@ -171,8 +173,11 @@ func (c *core) Write(ent zapcore.Entry, fs []zapcore.Field) error {
}
for _, f := range fs {
if f.Type == zapcore.SkipType {
if t, ok := f.Interface.(tagField); ok {
switch t := f.Interface.(type) {
case tagField:
event.Tags[t.Key] = t.Value
case ctxField:
hint = &sentry.EventHint{Context: t.Value}
}
}
}
Expand All @@ -186,7 +191,7 @@ func (c *core) Write(ent zapcore.Entry, fs []zapcore.Field) error {
}
}

_ = c.client.CaptureEvent(event, nil, c.scope())
_ = c.client.CaptureEvent(event, hint, c.scope())
}

// We may be crashing the program, so should flush any buffered events.
Expand Down
14 changes: 14 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package zapsentry

import (
"context"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand All @@ -13,3 +15,15 @@ type tagField struct {
func Tag(key string, value string) zap.Field {
return zap.Field{Key: key, Type: zapcore.SkipType, Interface: tagField{key, value}}
}

type ctxField struct {
Value context.Context
}

// Context adds a context to the logger.
// This can be used e.g. to pass trace information to sentry and allow linking events to their respective traces.
//
// See also https://docs.sentry.io/platforms/go/performance/instrumentation/opentelemetry/#linking-errors-to-transactions
func Context(ctx context.Context) zap.Field {
return zap.Field{Key: "context", Type: zapcore.SkipType, Interface: ctxField{ctx}}
}