diff --git a/core.go b/core.go index 229c1de..fa8343d 100644 --- a/core.go +++ b/core.go @@ -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 @@ -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} } } } @@ -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. diff --git a/field.go b/field.go index 8d1cdf1..3039012 100644 --- a/field.go +++ b/field.go @@ -1,6 +1,8 @@ package zapsentry import ( + "context" + "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -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}} +}