From 1137364db029c9a6a203aa6151455ac237c19a58 Mon Sep 17 00:00:00 2001 From: Ariel Demarco Date: Wed, 6 Nov 2024 12:07:54 -0300 Subject: [PATCH 1/3] Modify to happen inside attributesLock to prevent data races --- .../Trace/RecordEventsReadableSpan.swift | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift b/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift index 8b3a04c4..1d244198 100644 --- a/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift +++ b/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift @@ -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] { From fcc80eeafafb1af5726ace203035d8851a801796 Mon Sep 17 00:00:00 2001 From: Ariel Demarco Date: Wed, 6 Nov 2024 12:25:14 -0300 Subject: [PATCH 2/3] Added also the usage of eventsSyncLock to wrap the toSpanData operation --- .../Trace/RecordEventsReadableSpan.swift | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift b/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift index 1d244198..38c28229 100644 --- a/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift +++ b/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift @@ -192,27 +192,29 @@ public class RecordEventsReadableSpan: ReadableSpan { } public func toSpanData() -> SpanData { - 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) + eventsSyncLock.withLock { + 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) + } } } From 00e9d2c9a33635040e6f86b7b03d0987a7fd896a Mon Sep 17 00:00:00 2001 From: Ariel Demarco Date: Wed, 6 Nov 2024 18:13:10 -0300 Subject: [PATCH 3/3] [ROLLBACK] removed usage of eventsSyncLock --- .../Trace/RecordEventsReadableSpan.swift | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift b/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift index 38c28229..1d244198 100644 --- a/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift +++ b/Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift @@ -192,29 +192,27 @@ public class RecordEventsReadableSpan: ReadableSpan { } public func toSpanData() -> SpanData { - eventsSyncLock.withLock { - 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) - } + 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) } }