From 9f3d266359a168b4c7c08a8e616d6172b46a935d Mon Sep 17 00:00:00 2001 From: Yehor Popovych Date: Wed, 24 Jul 2024 16:25:38 +0100 Subject: [PATCH 1/2] fixed build for older Apple OS versions --- Sources/OpenTelemetryApi/Context/ContextManager.swift | 2 ++ .../Context/OpenTelemetryContextProvider.swift | 2 ++ .../Context/TaskLocalContextManager.swift | 1 + Sources/OpenTelemetryApi/Trace/SpanBuilder.swift | 3 +++ Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift | 1 + .../OpenTelemetryTestUtils/OpenTelemetryTestCase.swift | 8 ++++++-- Tests/OpenTelemetrySdkTests/ConcurrencyTests.swift | 2 +- 7 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Sources/OpenTelemetryApi/Context/ContextManager.swift b/Sources/OpenTelemetryApi/Context/ContextManager.swift index 7641fd2c..1dffa4a1 100644 --- a/Sources/OpenTelemetryApi/Context/ContextManager.swift +++ b/Sources/OpenTelemetryApi/Context/ContextManager.swift @@ -14,6 +14,7 @@ public protocol ContextManager: AnyObject { func withCurrentContextValue(forKey: OpenTelemetryContextKeys, value: AnyObject?, _ operation: () throws -> T) rethrows -> T #if canImport(_Concurrency) /// Updates the current context value with the given key for the duration of the passed closure. If `value` is non-`nil` the key is set to that value. If `value` is `nil` the key is removed from the current context for the duration of the closure. + @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) func withCurrentContextValue(forKey: OpenTelemetryContextKeys, value: AnyObject?, _ operation: () async throws -> T) async rethrows -> T #endif } @@ -22,6 +23,7 @@ public protocol ContextManager: AnyObject { public protocol ImperativeContextManager: ContextManager { } public extension ContextManager where Self: ImperativeContextManager { + @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) func withCurrentContextValue(forKey key: OpenTelemetryContextKeys, value: AnyObject?, _ operation: () async throws -> T) async rethrows -> T { var oldValue: AnyObject? if let value { diff --git a/Sources/OpenTelemetryApi/Context/OpenTelemetryContextProvider.swift b/Sources/OpenTelemetryApi/Context/OpenTelemetryContextProvider.swift index 64c9c95d..94382866 100644 --- a/Sources/OpenTelemetryApi/Context/OpenTelemetryContextProvider.swift +++ b/Sources/OpenTelemetryApi/Context/OpenTelemetryContextProvider.swift @@ -59,10 +59,12 @@ public struct OpenTelemetryContextProvider { #if canImport(_Concurrency) /// Sets `span` as the active span for the duration of the given closure. While the span will no longer be active after the closure exits, this method does **not** end the span. Prefer `SpanBuilderBase.withActiveSpan` which handles starting, activating, and ending the span. + @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public func withActiveSpan(_ span: SpanBase, _ operation: () async throws -> T) async rethrows -> T { try await contextManager.withCurrentContextValue(forKey: .span, value: span, operation) } + @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public func withActiveBaggage(_ span: Baggage, _ operation: () async throws -> T) async rethrows -> T { try await contextManager.withCurrentContextValue(forKey: .baggage, value: span, operation) } diff --git a/Sources/OpenTelemetryApi/Context/TaskLocalContextManager.swift b/Sources/OpenTelemetryApi/Context/TaskLocalContextManager.swift index 14d0f265..07deb35d 100644 --- a/Sources/OpenTelemetryApi/Context/TaskLocalContextManager.swift +++ b/Sources/OpenTelemetryApi/Context/TaskLocalContextManager.swift @@ -11,6 +11,7 @@ import Foundation /// Unlike the `os.activity` context manager, this class does not handle setting and removing context manually. You must always use the closure based APIs for setting active context when using this manager. The `OpenTelemetryConcurrency` module assists with this by hiding the imperative APIs by default. /// /// - Note: This restriction means this class is not suitable for dynamic context injection. If you require dynamic context injection, you will need a custom context manager. +@available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public class TaskLocalContextManager: ContextManager { #if swift(>=5.9) package static let instance = TaskLocalContextManager() diff --git a/Sources/OpenTelemetryApi/Trace/SpanBuilder.swift b/Sources/OpenTelemetryApi/Trace/SpanBuilder.swift index 8516e056..fdb8d951 100644 --- a/Sources/OpenTelemetryApi/Trace/SpanBuilder.swift +++ b/Sources/OpenTelemetryApi/Trace/SpanBuilder.swift @@ -110,6 +110,7 @@ public protocol SpanBuilderBase: AnyObject { #if canImport(_Concurrency) /// Starts a new Span and makes it active for the duration of the passed closure. The span will be ended before this method returns. + @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) func withActiveSpan(_ operation: (any SpanBase) async throws -> T) async rethrows -> T #endif @@ -125,6 +126,7 @@ public protocol SpanBuilderBase: AnyObject { #if canImport(_Concurrency) /// Starts a new Span. The span will be ended before this method returns. + @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) func withStartedSpan(_ operation: (any SpanBase) async throws -> T) async rethrows -> T #endif } @@ -146,6 +148,7 @@ public extension SpanBuilderBase { } #if canImport(_Concurrency) + @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) func withStartedSpan(_ operation: (any SpanBase) async throws -> T) async rethrows -> T { let span = self.startSpan() defer { diff --git a/Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift b/Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift index 45ed21a6..dda0846f 100644 --- a/Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift +++ b/Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift @@ -174,6 +174,7 @@ class SpanBuilderSdk: SpanBuilder { } #if canImport(_Concurrency) + @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public func withActiveSpan(_ operation: (any SpanBase) async throws -> T) async rethrows -> T { let createdSpan = self.prepareSpan() defer { diff --git a/Sources/OpenTelemetryTestUtils/OpenTelemetryTestCase.swift b/Sources/OpenTelemetryTestUtils/OpenTelemetryTestCase.swift index 272770e5..170cd6f4 100644 --- a/Sources/OpenTelemetryTestUtils/OpenTelemetryTestCase.swift +++ b/Sources/OpenTelemetryTestUtils/OpenTelemetryTestCase.swift @@ -76,7 +76,9 @@ open class OpenTelemetryContextTestCase: XCTestCase { public static func concurrencyContextManagers() -> [ContextManager] { var managers = [ContextManager]() #if canImport(_Concurrency) - managers.append(TaskLocalContextManager.instance) + if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) { + managers.append(TaskLocalContextManager.instance) + } #endif return managers } @@ -88,7 +90,9 @@ open class OpenTelemetryContextTestCase: XCTestCase { managers.append(ActivityContextManager.instance) #endif #if canImport(_Concurrency) - managers.append(TaskLocalContextManager.instance) + if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) { + managers.append(TaskLocalContextManager.instance) + } #endif return managers } diff --git a/Tests/OpenTelemetrySdkTests/ConcurrencyTests.swift b/Tests/OpenTelemetrySdkTests/ConcurrencyTests.swift index e1b02bf2..e8923ab4 100644 --- a/Tests/OpenTelemetrySdkTests/ConcurrencyTests.swift +++ b/Tests/OpenTelemetrySdkTests/ConcurrencyTests.swift @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#if canImport(_Concurrency) +#if canImport(_Concurrency) && canImport(OpenTelemetryConcurrency) import OpenTelemetryTestUtils import XCTest import OpenTelemetrySdk From 5aff39d92b4b93f5c2bbc636fd35b8a1d8c37df1 Mon Sep 17 00:00:00 2001 From: Yehor Popovych Date: Thu, 25 Jul 2024 11:41:31 +0100 Subject: [PATCH 2/2] changed OSX to macOS --- Sources/OpenTelemetryApi/Context/ContextManager.swift | 4 ++-- .../Context/OpenTelemetryContextProvider.swift | 4 ++-- .../OpenTelemetryApi/Context/TaskLocalContextManager.swift | 2 +- Sources/OpenTelemetryApi/Trace/SpanBuilder.swift | 6 +++--- Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift | 2 +- Sources/OpenTelemetryTestUtils/OpenTelemetryTestCase.swift | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Sources/OpenTelemetryApi/Context/ContextManager.swift b/Sources/OpenTelemetryApi/Context/ContextManager.swift index 1dffa4a1..55888ce9 100644 --- a/Sources/OpenTelemetryApi/Context/ContextManager.swift +++ b/Sources/OpenTelemetryApi/Context/ContextManager.swift @@ -14,7 +14,7 @@ public protocol ContextManager: AnyObject { func withCurrentContextValue(forKey: OpenTelemetryContextKeys, value: AnyObject?, _ operation: () throws -> T) rethrows -> T #if canImport(_Concurrency) /// Updates the current context value with the given key for the duration of the passed closure. If `value` is non-`nil` the key is set to that value. If `value` is `nil` the key is removed from the current context for the duration of the closure. - @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) func withCurrentContextValue(forKey: OpenTelemetryContextKeys, value: AnyObject?, _ operation: () async throws -> T) async rethrows -> T #endif } @@ -23,7 +23,7 @@ public protocol ContextManager: AnyObject { public protocol ImperativeContextManager: ContextManager { } public extension ContextManager where Self: ImperativeContextManager { - @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) func withCurrentContextValue(forKey key: OpenTelemetryContextKeys, value: AnyObject?, _ operation: () async throws -> T) async rethrows -> T { var oldValue: AnyObject? if let value { diff --git a/Sources/OpenTelemetryApi/Context/OpenTelemetryContextProvider.swift b/Sources/OpenTelemetryApi/Context/OpenTelemetryContextProvider.swift index 94382866..aac0b485 100644 --- a/Sources/OpenTelemetryApi/Context/OpenTelemetryContextProvider.swift +++ b/Sources/OpenTelemetryApi/Context/OpenTelemetryContextProvider.swift @@ -59,12 +59,12 @@ public struct OpenTelemetryContextProvider { #if canImport(_Concurrency) /// Sets `span` as the active span for the duration of the given closure. While the span will no longer be active after the closure exits, this method does **not** end the span. Prefer `SpanBuilderBase.withActiveSpan` which handles starting, activating, and ending the span. - @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public func withActiveSpan(_ span: SpanBase, _ operation: () async throws -> T) async rethrows -> T { try await contextManager.withCurrentContextValue(forKey: .span, value: span, operation) } - @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public func withActiveBaggage(_ span: Baggage, _ operation: () async throws -> T) async rethrows -> T { try await contextManager.withCurrentContextValue(forKey: .baggage, value: span, operation) } diff --git a/Sources/OpenTelemetryApi/Context/TaskLocalContextManager.swift b/Sources/OpenTelemetryApi/Context/TaskLocalContextManager.swift index 07deb35d..2c56f729 100644 --- a/Sources/OpenTelemetryApi/Context/TaskLocalContextManager.swift +++ b/Sources/OpenTelemetryApi/Context/TaskLocalContextManager.swift @@ -11,7 +11,7 @@ import Foundation /// Unlike the `os.activity` context manager, this class does not handle setting and removing context manually. You must always use the closure based APIs for setting active context when using this manager. The `OpenTelemetryConcurrency` module assists with this by hiding the imperative APIs by default. /// /// - Note: This restriction means this class is not suitable for dynamic context injection. If you require dynamic context injection, you will need a custom context manager. -@available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) +@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public class TaskLocalContextManager: ContextManager { #if swift(>=5.9) package static let instance = TaskLocalContextManager() diff --git a/Sources/OpenTelemetryApi/Trace/SpanBuilder.swift b/Sources/OpenTelemetryApi/Trace/SpanBuilder.swift index fdb8d951..15aabca8 100644 --- a/Sources/OpenTelemetryApi/Trace/SpanBuilder.swift +++ b/Sources/OpenTelemetryApi/Trace/SpanBuilder.swift @@ -110,7 +110,7 @@ public protocol SpanBuilderBase: AnyObject { #if canImport(_Concurrency) /// Starts a new Span and makes it active for the duration of the passed closure. The span will be ended before this method returns. - @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) func withActiveSpan(_ operation: (any SpanBase) async throws -> T) async rethrows -> T #endif @@ -126,7 +126,7 @@ public protocol SpanBuilderBase: AnyObject { #if canImport(_Concurrency) /// Starts a new Span. The span will be ended before this method returns. - @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) func withStartedSpan(_ operation: (any SpanBase) async throws -> T) async rethrows -> T #endif } @@ -148,7 +148,7 @@ public extension SpanBuilderBase { } #if canImport(_Concurrency) - @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) func withStartedSpan(_ operation: (any SpanBase) async throws -> T) async rethrows -> T { let span = self.startSpan() defer { diff --git a/Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift b/Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift index dda0846f..3c79e161 100644 --- a/Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift +++ b/Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift @@ -174,7 +174,7 @@ class SpanBuilderSdk: SpanBuilder { } #if canImport(_Concurrency) - @available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public func withActiveSpan(_ operation: (any SpanBase) async throws -> T) async rethrows -> T { let createdSpan = self.prepareSpan() defer { diff --git a/Sources/OpenTelemetryTestUtils/OpenTelemetryTestCase.swift b/Sources/OpenTelemetryTestUtils/OpenTelemetryTestCase.swift index 170cd6f4..5c92b9dd 100644 --- a/Sources/OpenTelemetryTestUtils/OpenTelemetryTestCase.swift +++ b/Sources/OpenTelemetryTestUtils/OpenTelemetryTestCase.swift @@ -76,7 +76,7 @@ open class OpenTelemetryContextTestCase: XCTestCase { public static func concurrencyContextManagers() -> [ContextManager] { var managers = [ContextManager]() #if canImport(_Concurrency) - if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) { + if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) { managers.append(TaskLocalContextManager.instance) } #endif @@ -90,7 +90,7 @@ open class OpenTelemetryContextTestCase: XCTestCase { managers.append(ActivityContextManager.instance) #endif #if canImport(_Concurrency) - if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) { + if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) { managers.append(TaskLocalContextManager.instance) } #endif