Skip to content

Commit

Permalink
Modify toSpanData to happen inside attributesLock to prevent data…
Browse files Browse the repository at this point in the history
… races (#632)
  • Loading branch information
ArielDemarco authored Nov 7, 2024
1 parent d0006d3 commit 0cd4a4c
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,28 @@ public class RecordEventsReadableSpan: ReadableSpan {
}

public func toSpanData() -> SpanData {
return SpanData(traceId: context.traceId,
spanId: context.spanId,
traceFlags: context.traceFlags,
traceState: context.traceState,
parentSpanId: parentContext?.spanId,
resource: resource,
instrumentationScope: instrumentationScopeInfo,
name: name,
kind: kind,
startTime: startTime,
attributes: attributes.attributes,
events: adaptEvents(),
links: adaptLinks(),
status: status,
endTime: endTime ?? clock.now,
hasRemoteParent: hasRemoteParent,
hasEnded: hasEnded,
totalRecordedEvents: getTotalRecordedEvents(),
totalRecordedLinks: totalRecordedLinks,
totalAttributeCount: totalAttributeCount)
attributesSyncLock.withLock {
return SpanData(traceId: context.traceId,
spanId: context.spanId,
traceFlags: context.traceFlags,
traceState: context.traceState,
parentSpanId: parentContext?.spanId,
resource: resource,
instrumentationScope: instrumentationScopeInfo,
name: name,
kind: kind,
startTime: startTime,
attributes: attributes.attributes,
events: adaptEvents(),
links: adaptLinks(),
status: status,
endTime: endTime ?? clock.now,
hasRemoteParent: hasRemoteParent,
hasEnded: hasEnded,
totalRecordedEvents: getTotalRecordedEvents(),
totalRecordedLinks: totalRecordedLinks,
totalAttributeCount: totalAttributeCount)
}
}

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

0 comments on commit 0cd4a4c

Please sign in to comment.