Skip to content

Commit 4dc3b98

Browse files
authored
fix: provide initial ctx to slog eventHandler (#1133)
1 parent d131251 commit 4dc3b98

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

slog/sentryslog.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func (o Option) NewSentryHandler(ctx context.Context) slog.Handler {
109109
logger.SetAttributes(attribute.String("sentry.origin", SlogOrigin))
110110

111111
eventHandler := &eventHandler{
112+
ctx: ctx,
112113
option: o,
113114
attrs: []slog.Attr{},
114115
groups: []string{},
@@ -164,6 +165,7 @@ func (h *SentryHandler) WithGroup(name string) slog.Handler {
164165
}
165166

166167
type eventHandler struct {
168+
ctx context.Context
167169
option Option
168170
attrs []slog.Attr
169171
groups []string
@@ -179,7 +181,10 @@ func (h *eventHandler) Enabled(_ context.Context, level slog.Level) bool {
179181
}
180182

181183
func (h *eventHandler) Handle(ctx context.Context, record slog.Record) error {
182-
hub := sentry.CurrentHub()
184+
hub := sentry.GetHubFromContext(h.ctx)
185+
if hub == nil {
186+
hub = sentry.CurrentHub()
187+
}
183188
if hubFromContext := sentry.GetHubFromContext(ctx); hubFromContext != nil {
184189
hub = hubFromContext
185190
} else if h.option.Hub != nil {
@@ -198,6 +203,7 @@ func (h *eventHandler) WithAttrs(attrs []slog.Attr) *eventHandler {
198203
copy(groupsCopy, h.groups)
199204

200205
return &eventHandler{
206+
ctx: h.ctx,
201207
option: h.option,
202208
attrs: appendAttrsToGroup(h.groups, h.attrs, attrs...),
203209
groups: groupsCopy,
@@ -215,6 +221,7 @@ func (h *eventHandler) WithGroup(name string) *eventHandler {
215221
newGroups = append(newGroups, name)
216222

217223
return &eventHandler{
224+
ctx: h.ctx,
218225
option: h.option,
219226
attrs: h.attrs,
220227
groups: newGroups,

0 commit comments

Comments
 (0)