Skip to content

Commit

Permalink
Modify to happen inside attributesLock to prevent data races
Browse files Browse the repository at this point in the history
  • Loading branch information
ArielDemarco committed Nov 6, 2024
1 parent d0006d3 commit 1137364
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 1137364

Please sign in to comment.