Skip to content

Commit

Permalink
fixed build for older Apple OS versions (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Jul 26, 2024
1 parent 44a7b30 commit 7d12a8b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Sources/OpenTelemetryApi/Context/ContextManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public protocol ContextManager: AnyObject {
func withCurrentContextValue<T>(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<T>(forKey: OpenTelemetryContextKeys, value: AnyObject?, _ operation: () async throws -> T) async rethrows -> T
#endif
}
Expand All @@ -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<T>(forKey key: OpenTelemetryContextKeys, value: AnyObject?, _ operation: () async throws -> T) async rethrows -> T {
var oldValue: AnyObject?
if let value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_ 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<T>(_ span: Baggage, _ operation: () async throws -> T) async rethrows -> T {
try await contextManager.withCurrentContextValue(forKey: .baggage, value: span, operation)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions Sources/OpenTelemetryApi/Trace/SpanBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_ operation: (any SpanBase) async throws -> T) async rethrows -> T
#endif

Expand All @@ -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<T>(_ operation: (any SpanBase) async throws -> T) async rethrows -> T
#endif
}
Expand All @@ -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<T>(_ operation: (any SpanBase) async throws -> T) async rethrows -> T {
let span = self.startSpan()
defer {
Expand Down
1 change: 1 addition & 0 deletions Sources/OpenTelemetrySdk/Trace/SpanBuilderSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_ operation: (any SpanBase) async throws -> T) async rethrows -> T {
let createdSpan = self.prepareSpan()
defer {
Expand Down
8 changes: 6 additions & 2 deletions Sources/OpenTelemetryTestUtils/OpenTelemetryTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/OpenTelemetrySdkTests/ConcurrencyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#if canImport(_Concurrency)
#if canImport(_Concurrency) && canImport(OpenTelemetryConcurrency)
import OpenTelemetryTestUtils
import XCTest
import OpenTelemetrySdk
Expand Down

0 comments on commit 7d12a8b

Please sign in to comment.