Skip to content

Commit 0cd4a4c

Browse files
authored
Modify toSpanData to happen inside attributesLock to prevent data races (#632)
1 parent d0006d3 commit 0cd4a4c

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -192,26 +192,28 @@ public class RecordEventsReadableSpan: ReadableSpan {
192192
}
193193

194194
public func toSpanData() -> SpanData {
195-
return SpanData(traceId: context.traceId,
196-
spanId: context.spanId,
197-
traceFlags: context.traceFlags,
198-
traceState: context.traceState,
199-
parentSpanId: parentContext?.spanId,
200-
resource: resource,
201-
instrumentationScope: instrumentationScopeInfo,
202-
name: name,
203-
kind: kind,
204-
startTime: startTime,
205-
attributes: attributes.attributes,
206-
events: adaptEvents(),
207-
links: adaptLinks(),
208-
status: status,
209-
endTime: endTime ?? clock.now,
210-
hasRemoteParent: hasRemoteParent,
211-
hasEnded: hasEnded,
212-
totalRecordedEvents: getTotalRecordedEvents(),
213-
totalRecordedLinks: totalRecordedLinks,
214-
totalAttributeCount: totalAttributeCount)
195+
attributesSyncLock.withLock {
196+
return SpanData(traceId: context.traceId,
197+
spanId: context.spanId,
198+
traceFlags: context.traceFlags,
199+
traceState: context.traceState,
200+
parentSpanId: parentContext?.spanId,
201+
resource: resource,
202+
instrumentationScope: instrumentationScopeInfo,
203+
name: name,
204+
kind: kind,
205+
startTime: startTime,
206+
attributes: attributes.attributes,
207+
events: adaptEvents(),
208+
links: adaptLinks(),
209+
status: status,
210+
endTime: endTime ?? clock.now,
211+
hasRemoteParent: hasRemoteParent,
212+
hasEnded: hasEnded,
213+
totalRecordedEvents: getTotalRecordedEvents(),
214+
totalRecordedLinks: totalRecordedLinks,
215+
totalAttributeCount: totalAttributeCount)
216+
}
215217
}
216218

217219
private func adaptEvents() -> [SpanData.Event] {

0 commit comments

Comments
 (0)