From 4070ce50520b1a57cc708a89dd10152b39cdc9a2 Mon Sep 17 00:00:00 2001 From: Clack Cole Date: Thu, 10 Oct 2024 13:06:41 -0600 Subject: [PATCH] fix: Works around a Linux specific compilation issue This issue occurs against newer Swift development toolchains. https://github.com/PassiveLogic/open-telemetry-swift/issues/3 --- .../Processors/BatchLogRecordProcessor.swift | 20 +++++++++++++---- .../SpanProcessors/BatchSpanProcessor.swift | 22 ++++++++++++++----- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Sources/OpenTelemetrySdk/Logs/Processors/BatchLogRecordProcessor.swift b/Sources/OpenTelemetrySdk/Logs/Processors/BatchLogRecordProcessor.swift index ca14379..61cdc5f 100644 --- a/Sources/OpenTelemetrySdk/Logs/Processors/BatchLogRecordProcessor.swift +++ b/Sources/OpenTelemetrySdk/Logs/Processors/BatchLogRecordProcessor.swift @@ -38,7 +38,7 @@ public class BatchLogRecordProcessor : LogRecordProcessor { } } -private class BatchWorker : Thread { +private class BatchWorker { let logRecordExporter : LogRecordExporter let scheduleDelay : TimeInterval let maxQueueSize : Int @@ -49,7 +49,10 @@ private class BatchWorker : Thread { private let cond = NSCondition() var logRecordList = [ReadableLogRecord]() var queue : OperationQueue - + // TODO: Workaround for the following compiler issue, and can be removed once resolved + // https://github.com/swiftlang/swift/issues/76752 + private let _thread : Thread + init(logRecordExporter: LogRecordExporter, scheduleDelay: TimeInterval, exportTimeout: TimeInterval, @@ -67,8 +70,17 @@ private class BatchWorker : Thread { queue = OperationQueue() queue.name = "BatchWorker Queue" queue.maxConcurrentOperationCount = 1 + self._thread = Thread() } - + + func start() { + self._thread.start() + } + + func cancel() { + self._thread.cancel() + } + func emit(logRecord: ReadableLogRecord) { cond.lock() defer { cond.unlock()} @@ -84,7 +96,7 @@ private class BatchWorker : Thread { } } - override func main() { + func main() { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) repeat { autoreleasepool { diff --git a/Sources/OpenTelemetrySdk/Trace/SpanProcessors/BatchSpanProcessor.swift b/Sources/OpenTelemetrySdk/Trace/SpanProcessors/BatchSpanProcessor.swift index 6472b72..652ff65 100644 --- a/Sources/OpenTelemetrySdk/Trace/SpanProcessors/BatchSpanProcessor.swift +++ b/Sources/OpenTelemetrySdk/Trace/SpanProcessors/BatchSpanProcessor.swift @@ -56,7 +56,7 @@ public struct BatchSpanProcessor: SpanProcessor { /// BatchWorker is a thread that batches multiple spans and calls the registered SpanExporter to export /// the data. /// The list of batched data is protected by a NSCondition which ensures full concurrency. -private class BatchWorker: Thread { +private class BatchWorker { let spanExporter: SpanExporter let scheduleDelay: TimeInterval let maxQueueSize: Int @@ -67,7 +67,10 @@ private class BatchWorker: Thread { private let cond = NSCondition() var spanList = [ReadableSpan]() var queue: OperationQueue - + // TODO: Workaround for the following compiler issue, and can be removed once resolved + // https://github.com/swiftlang/swift/issues/76752 + private let _thread: Thread + init(spanExporter: SpanExporter, scheduleDelay: TimeInterval, exportTimeout: TimeInterval, maxQueueSize: Int, maxExportBatchSize: Int, willExportCallback: ((inout [SpanData]) -> Void)?) { self.spanExporter = spanExporter self.scheduleDelay = scheduleDelay @@ -79,8 +82,17 @@ private class BatchWorker: Thread { queue = OperationQueue() queue.name = "BatchWorker Queue" queue.maxConcurrentOperationCount = 1 + self._thread = Thread() } - + + func start() { + self._thread.start() + } + + func cancel() { + self._thread.cancel() + } + func addSpan(span: ReadableSpan) { cond.lock() defer { cond.unlock() } @@ -99,7 +111,7 @@ private class BatchWorker: Thread { } #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) - override func main() { + func main() { repeat { autoreleasepool { var spansCopy: [ReadableSpan] @@ -117,7 +129,7 @@ private class BatchWorker: Thread { } while true } #else - override func main() { + func main() { repeat { var spansCopy: [ReadableSpan] cond.lock()