Skip to content

Commit f335a11

Browse files
committed
mark types Sendable
1 parent 1eb0a48 commit f335a11

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

Sources/Instrumentation/Instrument.swift

+18-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,21 @@
1414

1515
import InstrumentationBaggage
1616

17+
/// Typealias used to simplify Support of old Swift versions which do not have `Sendable` defined.
18+
#if swift(>=5.6.0)
19+
@preconcurrency public protocol _SwiftInstrumentationSendable: Sendable {}
20+
#else
21+
public protocol _SwiftInstrumentationSendable {}
22+
#endif
23+
1724
/// Conforming types are used to extract values from a specific `Carrier`.
18-
public protocol Extractor {
25+
public protocol Extractor: _SwiftInstrumentationSendable {
1926
/// The carrier to extract values from.
27+
#if swift(>=5.6.0)
28+
associatedtype Carrier: Sendable
29+
#else
2030
associatedtype Carrier
31+
#endif
2132

2233
/// Extract the value for the given key from the `Carrier`.
2334
///
@@ -28,9 +39,13 @@ public protocol Extractor {
2839
}
2940

3041
/// Conforming types are used to inject values into a specific `Carrier`.
31-
public protocol Injector {
42+
public protocol Injector: _SwiftInstrumentationSendable {
3243
/// The carrier to inject values into.
44+
#if swift(>=5.6.0)
45+
associatedtype Carrier: Sendable
46+
#else
3347
associatedtype Carrier
48+
#endif
3449

3550
/// Inject the given value for the given key into the given `Carrier`.
3651
///
@@ -43,7 +58,7 @@ public protocol Injector {
4358

4459
/// Conforming types are usually cross-cutting tools like tracers. They are agnostic of what specific `Carrier` is used
4560
/// to propagate metadata across boundaries, but instead just specify what values to use for which keys.
46-
public protocol Instrument {
61+
public protocol Instrument: _SwiftInstrumentationSendable {
4762
/// Extract values from a `Carrier` by using the given extractor and inject them into the given `Baggage`.
4863
/// It's quite common for `Instrument`s to come up with new values if they weren't passed along in the given `Carrier`.
4964
///

Sources/Tracing/Span.swift

+18-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Dispatch
2222
/// Creating a `Span` is delegated to a ``Tracer`` and end users should never create them directly.
2323
///
2424
/// - SeeAlso: For more details refer to the [OpenTelemetry Specification: Span](https://github.com/open-telemetry/opentelemetry-specification/blob/v0.7.0/specification/trace/api.md#span) which this type is compatible with.
25-
public protocol Span: AnyObject {
25+
public protocol Span: AnyObject, _SwiftTracingSendableSpan {
2626
/// The read-only `Baggage` of this `Span`, set when starting this `Span`.
2727
var baggage: Baggage { get }
2828

@@ -650,10 +650,26 @@ public struct SpanLink {
650650

651651
/// Create a new `SpanLink`.
652652
/// - Parameters:
653-
/// - context: The `Baggage` identifying the targeted ``Span``.
653+
/// - baggage: The `Baggage` identifying the targeted ``Span``.
654654
/// - attributes: ``SpanAttributes`` that further describe the link. Defaults to no attributes.
655655
public init(baggage: Baggage, attributes: SpanAttributes = [:]) {
656656
self.baggage = baggage
657657
self.attributes = attributes
658658
}
659659
}
660+
661+
#if compiler(>=5.6)
662+
@preconcurrency public protocol _SwiftTracingSendableSpan: Sendable {}
663+
#else
664+
public protocol _SwiftTracingSendableSpan {}
665+
#endif
666+
667+
#if compiler(>=5.6)
668+
extension SpanAttributes: Sendable {}
669+
extension SpanAttribute: @unchecked Sendable {}
670+
extension SpanStatus: Sendable {}
671+
extension SpanEvent: @unchecked Sendable {} // unchecked because of DispatchWallTime
672+
extension SpanKind: Sendable {}
673+
extension SpanStatus.Code: Sendable {}
674+
extension SpanLink: Sendable {}
675+
#endif

Tests/TracingTests/DynamicTracepointTracerTests.swift

+7
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ final class DynamicTracepointTracerTests: XCTestCase {
104104
}
105105
}
106106

107+
/// Only intended to be used in single-threaded testing.
107108
final class DynamicTracepointTestTracer: Tracer {
108109
private(set) var activeTracepoints: Set<TracepointID> = []
109110

@@ -227,6 +228,7 @@ final class DynamicTracepointTestTracer: Tracer {
227228
}
228229

229230
extension DynamicTracepointTestTracer {
231+
/// Only intended to be used in single-threaded testing.
230232
final class TracepointSpan: Tracing.Span {
231233
private let operationName: String
232234
private let kind: SpanKind
@@ -288,3 +290,8 @@ extension DynamicTracepointTestTracer {
288290
}
289291
}
290292
}
293+
294+
#if compiler(>=5.6.0)
295+
extension DynamicTracepointTestTracer: @unchecked Sendable {}
296+
extension DynamicTracepointTestTracer.TracepointSpan: @unchecked Sendable {}
297+
#endif

Tests/TracingTests/TestTracer.swift

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Instrumentation
1818
import InstrumentationBaggage
1919
import Tracing
2020

21+
/// Only intended to be used in single-threaded testing.
2122
final class TestTracer: Tracer {
2223
private(set) var spans = [TestSpan]()
2324
var onEndSpan: (Span) -> Void = { _ in }
@@ -93,6 +94,7 @@ extension Baggage {
9394
}
9495
}
9596

97+
/// Only intended to be used in single-threaded testing.
9698
final class TestSpan: Span {
9799
private let operationName: String
98100
private let kind: SpanKind
@@ -156,3 +158,8 @@ final class TestSpan: Span {
156158
self.onEnd(self)
157159
}
158160
}
161+
162+
#if compiler(>=5.6.0)
163+
extension TestTracer: @unchecked Sendable {}
164+
extension TestSpan: @unchecked Sendable {}
165+
#endif

Tests/TracingTests/TracedLockTests.swift

+5
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,8 @@ private final class TracedLockPrintlnTracer: Tracer {
158158
}
159159
}
160160
}
161+
162+
#if compiler(>=5.6.0)
163+
extension TracedLockPrintlnTracer: Sendable {}
164+
extension TracedLockPrintlnTracer.TracedLockPrintlnSpan: @unchecked Sendable {}
165+
#endif

0 commit comments

Comments
 (0)