Skip to content

Commit a100bf5

Browse files
authored
Fix race conditions on log when accessing scope (#1050)
1 parent 17421c4 commit a100bf5

File tree

3 files changed

+436
-48
lines changed

3 files changed

+436
-48
lines changed

log.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"maps"
77
"os"
88
"strings"
9+
"sync"
910
"time"
1011

1112
"github.com/getsentry/sentry-go/attribute"
@@ -43,6 +44,7 @@ type sentryLogger struct {
4344
ctx context.Context
4445
client *Client
4546
attributes map[string]Attribute
47+
mu sync.RWMutex
4648
}
4749

4850
type logEntry struct {
@@ -64,7 +66,12 @@ func NewLogger(ctx context.Context) Logger {
6466

6567
client := hub.Client()
6668
if client != nil && client.batchLogger != nil {
67-
return &sentryLogger{ctx, client, make(map[string]Attribute)}
69+
return &sentryLogger{
70+
ctx: ctx,
71+
client: client,
72+
attributes: make(map[string]Attribute),
73+
mu: sync.RWMutex{},
74+
}
6875
}
6976

7077
DebugLogger.Println("fallback to noopLogger: enableLogs disabled")
@@ -89,13 +96,21 @@ func (l *sentryLogger) log(ctx context.Context, level LogLevel, severity int, me
8996

9097
var traceID TraceID
9198
var spanID SpanID
99+
var span *Span
100+
var user User
92101

93-
span := hub.Scope().span
94-
if span != nil {
95-
traceID = span.TraceID
96-
spanID = span.SpanID
97-
} else {
98-
traceID = hub.Scope().propagationContext.TraceID
102+
scope := hub.Scope()
103+
if scope != nil {
104+
scope.mu.Lock()
105+
span = scope.span
106+
if span != nil {
107+
traceID = span.TraceID
108+
spanID = span.SpanID
109+
} else {
110+
traceID = scope.propagationContext.TraceID
111+
}
112+
user = scope.user
113+
scope.mu.Unlock()
99114
}
100115

101116
attrs := map[string]Attribute{}
@@ -110,9 +125,12 @@ func (l *sentryLogger) log(ctx context.Context, level LogLevel, severity int, me
110125
}
111126
}
112127

128+
l.mu.RLock()
113129
for k, v := range l.attributes {
114130
attrs[k] = v
115131
}
132+
l.mu.RUnlock()
133+
116134
for k, v := range entryAttrs {
117135
attrs[k] = v
118136
}
@@ -129,19 +147,16 @@ func (l *sentryLogger) log(ctx context.Context, level LogLevel, severity int, me
129147
} else if serverAddr, err := os.Hostname(); err == nil {
130148
attrs["sentry.server.address"] = Attribute{Value: serverAddr, Type: AttributeString}
131149
}
132-
scope := hub.Scope()
133-
if scope != nil {
134-
user := scope.user
135-
if !user.IsEmpty() {
136-
if user.ID != "" {
137-
attrs["user.id"] = Attribute{Value: user.ID, Type: AttributeString}
138-
}
139-
if user.Name != "" {
140-
attrs["user.name"] = Attribute{Value: user.Name, Type: AttributeString}
141-
}
142-
if user.Email != "" {
143-
attrs["user.email"] = Attribute{Value: user.Email, Type: AttributeString}
144-
}
150+
151+
if !user.IsEmpty() {
152+
if user.ID != "" {
153+
attrs["user.id"] = Attribute{Value: user.ID, Type: AttributeString}
154+
}
155+
if user.Name != "" {
156+
attrs["user.name"] = Attribute{Value: user.Name, Type: AttributeString}
157+
}
158+
if user.Email != "" {
159+
attrs["user.email"] = Attribute{Value: user.Email, Type: AttributeString}
145160
}
146161
}
147162
if span != nil {
@@ -177,6 +192,9 @@ func (l *sentryLogger) log(ctx context.Context, level LogLevel, severity int, me
177192
}
178193

179194
func (l *sentryLogger) SetAttributes(attrs ...attribute.Builder) {
195+
l.mu.Lock()
196+
defer l.mu.Unlock()
197+
180198
for _, v := range attrs {
181199
t, ok := mapTypesToStr[v.Value.Type()]
182200
if !ok || t == "" {

0 commit comments

Comments
 (0)