diff --git a/Sources/OpenTelemetryApi/Context/ContextManager.swift b/Sources/OpenTelemetryApi/Context/ContextManager.swift index 7641fd2c..55888ce9 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(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 } @@ -22,6 +23,7 @@ public protocol ContextManager: AnyObject { public protocol ImperativeContextManager: ContextManager { } public extension ContextManager where Self: ImperativeContextManager { + @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 64c9c95d..aac0b485 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(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(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 14d0f265..2c56f729 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(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 8516e056..15aabca8 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(macOS 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(macOS 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(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 45ed21a6..3c79e161 100644 --- a/Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift +++ b/Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift @@ -174,6 +174,7 @@ class SpanBuilderSdk: SpanBuilder { } #if canImport(_Concurrency) + @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 272770e5..5c92b9dd 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(macOS 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(macOS 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