Skip to content

Bump minimum Swift version to 5.7 #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.6
// swift-tools-version:5.7
import PackageDescription

let package = Package(
Expand Down
4 changes: 0 additions & 4 deletions Sources/Instrumentation/Instrument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
import ServiceContextModule

/// Typealias used to simplify Support of old Swift versions which do not have `Sendable` defined.
#if swift(>=5.6.0)
@preconcurrency public protocol _SwiftInstrumentationSendable: Sendable {}
#else
public protocol _SwiftInstrumentationSendable {}
#endif

/// Conforming types are used to extract values from a specific `Carrier`.
public protocol Extractor: _SwiftInstrumentationSendable {
Expand Down
4 changes: 1 addition & 3 deletions Sources/Tracing/Docs.docc/Guides/ImplementATracer.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ The primary goal and user-facing API of a ``Tracer`` is to create spans.
While you will need to implement all methods of the tracer protocol, the most important one is `startSpan`:

```swift
#if swift(>=5.7.0)
extension MyTracer: Tracer {
func startSpan<Instant: TracerInstant>(
_ operationName: String,
Expand All @@ -147,7 +146,6 @@ extension MyTracer: Tracer {
return span
}
}
#endif
```

If you can require Swift 5.7 prefer doing so, and return the concrete ``Span`` type from the `startSpan` method.
Expand All @@ -170,4 +168,4 @@ public struct MySpan: Tracing.Span {
It is possible to implement a span as a struct or a class, but a ``Span`` **must exhibit reference type behavior**.
In other words, adding an attribute to one reference of a span must be visible in other references to it.

The ability to implement a span using a struct comes in handy when implementing a "Noop" (do nothing) span and avoids heap allocations. Normal spans though generally will be backed by `class` based storage and should flush themselfes to the owning tracer once the span has been ended
The ability to implement a span using a struct comes in handy when implementing a "Noop" (do nothing) span and avoids heap allocations. Normal spans though generally will be backed by `class` based storage and should flush themselves to the owning tracer once the span has been ended
2 changes: 1 addition & 1 deletion Sources/Tracing/Docs.docc/Guides/InstrumentYourLibrary.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ While this code is very simple for illustration purposes, and it may seem surpri

The above steps are enough if you wanted to provide context propagation. It already enables techniques such as **correlation ids** which can be set once, in one system, and then carried through to any downstream services the code makes calls from while the context is set.

Many libraries also have the opportunity to start trace spans themselfes, on behalf of users, in pieces of the library that can provide useful insight in the behavior or the library in production. For example, the `HTTPServer` can start spans as soon as it begins handling HTTP requests, and this way provide a parent span to any spans the user-code would be creating itself.
Many libraries also have the opportunity to start trace spans themselves, on behalf of users, in pieces of the library that can provide useful insight in the behavior or the library in production. For example, the `HTTPServer` can start spans as soon as it begins handling HTTP requests, and this way provide a parent span to any spans the user-code would be creating itself.

Let us revisit the previous sample `HTTPServer` which restored context around invoking the user-code, and further extend it to start a span including basic information about the `HTTPRequest` being handled:

Expand Down
3 changes: 1 addition & 2 deletions Sources/Tracing/InstrumentationSystem+Tracing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension InstrumentationSystem {
#if swift(>=5.7.0)
/// Returns the ``Tracer`` bootstrapped as part of the `InstrumentationSystem`.
///
/// If the system was bootstrapped with a `MultiplexInstrument` this function attempts to locate the _first_
Expand All @@ -28,14 +27,14 @@ extension InstrumentationSystem {
(self._findInstrument(where: { $0 is (any Tracer) }) as? (any Tracer))
return found ?? NoOpTracer()
}
#endif

/// Returns the ``Tracer`` bootstrapped as part of the `InstrumentationSystem`.
///
/// If the system was bootstrapped with a `MultiplexInstrument` this function attempts to locate the _first_
/// tracing instrument as passed to the multiplex instrument. If none is found, a ``NoOpTracer`` is returned.
///
/// - Returns: A ``Tracer`` if the system was bootstrapped with one, and ``NoOpTracer`` otherwise.
@available(*, deprecated, message: "prefer tracer")
public static var legacyTracer: any LegacyTracer {
let found: (any LegacyTracer)? =
(self._findInstrument(where: { $0 is (any LegacyTracer) }) as? (any LegacyTracer))
Expand Down
2 changes: 0 additions & 2 deletions Sources/Tracing/NoOpTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public struct NoOpTracer: LegacyTracer {
}
}

#if swift(>=5.7.0)
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension NoOpTracer: Tracer {
public func startSpan<Instant: TracerInstant>(
Expand All @@ -106,4 +105,3 @@ extension NoOpTracer: Tracer {
NoOpSpan(context: context())
}
}
#endif
2 changes: 0 additions & 2 deletions Sources/Tracing/SpanProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,6 @@ extension SpanAttributes {
}
}

#if swift(>=5.2)
extension SpanAttributes {
/// Enables for type-safe fluent accessors for attributes.
public subscript<T>(dynamicMember dynamicMember: KeyPath<SpanAttribute, SpanAttributeKey<T>>) -> SpanAttribute? {
Expand All @@ -650,7 +649,6 @@ extension SpanAttributes {
SpanAttribute._namespace[keyPath: dynamicMember]
}
}
#endif

extension SpanAttributes: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (String, SpanAttribute)...) {
Expand Down
7 changes: 0 additions & 7 deletions Sources/Tracing/Tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public func startSpan(
)
}

#if swift(>=5.7.0)
/// Start a new ``Span`` using the global bootstrapped tracer reimplementation.
///
/// The current task-local `ServiceContext` is picked up and provided to the underlying tracer.
Expand Down Expand Up @@ -155,7 +154,6 @@ public func startSpan(
line: line
)
}
#endif

// ==== withSpan + sync ---------------------------------------------------

Expand Down Expand Up @@ -251,8 +249,6 @@ public func withSpan<T>(
}
}

#if swift(>=5.7.0)

/// Start a new ``Span`` and automatically end when the `operation` completes,
/// including recording the `error` in case the operation throws.
///
Expand Down Expand Up @@ -299,7 +295,6 @@ public func withSpan<T>(
try operation(anySpan)
}
}
#endif

// ==== withSpan + async --------------------------------------------------

Expand Down Expand Up @@ -395,7 +390,6 @@ public func withSpan<T>(
}
}

#if swift(>=5.7.0)
/// Start a new ``Span`` and automatically end when the `operation` completes,
/// including recording the `error` in case the operation throws.
///
Expand Down Expand Up @@ -442,4 +436,3 @@ public func withSpan<T>(
try await operation(anySpan)
}
}
#endif
5 changes: 5 additions & 0 deletions Sources/Tracing/TracerProtocol+Legacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Dispatch
/// rather than these `startAnySpan` APIs which unconditionally always return existential Spans even when not necessary
/// (under Swift 5.7+ type-system enhancement wrt. protocols with associated types)..
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
@available(*, deprecated, renamed: "Tracer")
public protocol LegacyTracer: Instrument {
/// Start a new span returning an existential ``Span`` reference.
///
Expand Down Expand Up @@ -54,6 +55,8 @@ public protocol LegacyTracer: Instrument {
/// - function: The function name in which the span was started
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.

@available(*, deprecated, message: "prefer withSpan")
func startAnySpan<Instant: TracerInstant>(
_ operationName: String,
context: @autoclosure () -> ServiceContext,
Expand All @@ -70,6 +73,7 @@ public protocol LegacyTracer: Instrument {
/// such as when using some FaaS providers that may suspend the process after an invocation, but before the backend exports the completed spans.
///
/// This function should not block indefinitely, implementations should offer a configurable timeout for flush operations.
@available(*, deprecated)
func forceFlush()
}

Expand Down Expand Up @@ -108,6 +112,7 @@ extension LegacyTracer {
/// - function: The function name in which the span was started
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
@available(*, deprecated, message: "prefer withSpan")
public func startAnySpan<Instant: TracerInstant>(
_ operationName: String,
at instant: @autoclosure () -> Instant,
Expand Down
4 changes: 0 additions & 4 deletions Sources/Tracing/TracerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
// ==== -----------------------------------------------------------------------
// MARK: Tracer protocol

#if swift(>=5.7.0)

/// A tracer capable of creating new trace spans.
///
/// A tracer is a special kind of instrument with the added ability to start a ``Span``.
Expand Down Expand Up @@ -325,5 +323,3 @@ extension Tracer {
}
}
}

#endif // Swift 5.7
22 changes: 1 addition & 21 deletions Tests/TracingTests/DynamicTracepointTracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ final class DynamicTracepointTracerTests: XCTestCase {
// since the parent of this span was captured, this shall be captured as well
}
}
#if swift(>=5.7.0)
tracer.withSpan("dont") { _ in
// don't capture this span...
}
Expand All @@ -58,13 +57,8 @@ final class DynamicTracepointTracerTests: XCTestCase {
// since the parent of this span was captured, this shall be captured as well
}
}
#endif

#if swift(>=5.7.0)
XCTAssertEqual(tracer.spans.count, 4)
#else
XCTAssertEqual(tracer.spans.count, 2)
#endif

for span in tracer.spans {
XCTAssertEqual(span.context.traceID, "trace-id-fake-\(fileID)-\(fakeLine)")
Expand Down Expand Up @@ -99,31 +93,17 @@ final class DynamicTracepointTracerTests: XCTestCase {
}

func logic(fakeLine: UInt) {
#if swift(>=5.7)
InstrumentationSystem.tracer.withSpan("\(#function)-dont", line: fakeLine) { _ in
// inside
}
#else
InstrumentationSystem.legacyTracer.withAnySpan("\(#function)-dont", line: fakeLine) { _ in
// inside
}
#endif
}

func traceMeLogic(fakeLine: UInt) {
#if swift(>=5.7)
InstrumentationSystem.tracer.withSpan("\(#function)-yes", line: fakeLine) { _ in
InstrumentationSystem.tracer.withSpan("\(#function)-yes-inside", line: fakeLine + 11) { _ in
// inside
}
}
#else
InstrumentationSystem.legacyTracer.withAnySpan("\(#function)-yes", line: fakeLine) { _ in
InstrumentationSystem.legacyTracer.withAnySpan("\(#function)-yes-inside", line: fakeLine + 11) { _ in
// inside
}
}
#endif
}
}

Expand Down Expand Up @@ -193,7 +173,7 @@ final class DynamicTracepointTestTracer: LegacyTracer {
}

private func shouldRecord(tracepoint: TracepointID) -> Bool {
#if swift(>=5.5) && canImport(_Concurrency)
#if canImport(_Concurrency)
if self.isActive(tracepoint: tracepoint) {
// this tracepoint was specifically activated!
return true
Expand Down
2 changes: 0 additions & 2 deletions Tests/TracingTests/TestTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ final class TestTracer: LegacyTracer {
}
}

#if swift(>=5.7.0)
extension TestTracer: Tracer {
func startSpan<Instant: TracerInstant>(
_ operationName: String,
Expand All @@ -86,7 +85,6 @@ extension TestTracer: Tracer {
return span
}
}
#endif

extension TestTracer {
enum TraceIDKey: ServiceContextKey {
Expand Down
2 changes: 0 additions & 2 deletions Tests/TracingTests/TracedLockTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ private final class TracedLockPrintlnTracer: LegacyTracer {
}
}

#if swift(>=5.7.0)
extension TracedLockPrintlnTracer: Tracer {
func startSpan<Instant: TracerInstant>(
_ operationName: String,
Expand All @@ -180,7 +179,6 @@ extension TracedLockPrintlnTracer: Tracer {
)
}
}
#endif

#if compiler(>=5.6.0)
extension TracedLockPrintlnTracer: Sendable {}
Expand Down
3 changes: 0 additions & 3 deletions Tests/TracingTests/TracerTests+swift57.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import ServiceContextModule
import Tracing
import XCTest

#if swift(>=5.7.0)
// Specifically make sure we don't have to implement startAnySpan

final class SampleSwift57Tracer: Tracer {
Expand Down Expand Up @@ -130,5 +129,3 @@ final class SampleSwift57Span: Span {

extension SampleSwift57Tracer: @unchecked Sendable {} // only intended for single threaded SampleSwift57ing
extension SampleSwift57Span: @unchecked Sendable {} // only intended for single threaded SampleSwift57ing

#endif
Loading