Skip to content
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

Adopt Swift 6.0 #isolation; Resolves async closure behavior in withSpan #148

Merged
merged 3 commits into from
Jul 19, 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
Expand Up @@ -8,7 +8,7 @@ let package = Package(
.library(name: "Tracing", targets: ["Tracing"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-service-context.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-service-context.git", from: "1.1.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
Expand Down
91 changes: 90 additions & 1 deletion Sources/Tracing/Tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,42 @@ public func withSpan<T>(
/// - instant: the time instant at which the span started
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc comment is wrong, should be ofKind to match the public API

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? https://www.swift.org/documentation/docc/writing-symbol-documentation-in-your-source-files shows to document the actual second name, as those are unique and the prior labels aren't.

/// - isolation: Defaulted parameter for inheriting isolation of calling actor
/// - 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.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
#if swift(>=6.0)
Copy link
Contributor

@porglezomp porglezomp Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is #if swift(>=6.0) correct here? Should this use #if compiler(>=6.0) instead, if a Swift 6 compiler in 5.x mode understands #isolation? I just tried to adopt this in pre-6 code with a 6.0 compiler and it was using the previous overload.

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
public func withSpan<T, Instant: TracerInstant>(
_ operationName: String,
at instant: @autoclosure () -> Instant,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
isolation: isolated(any Actor)? = #isolation,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (any Span) async throws -> T
) async rethrows -> T {
try await InstrumentationSystem.legacyTracer.withAnySpan(
operationName,
at: instant(),
context: context(),
ofKind: kind,
function: function,
file: fileID,
line: line
) { anySpan in
try await operation(anySpan)
}
}
#endif

@_disfavoredOverload
@available(*, deprecated, message: "Prefer #isolation version of this API")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the deprecated attribute be applied here when the #isolation API isn't available? Should this be in the same #if as above?

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
public func withSpan<T, Instant: TracerInstant>(
_ operationName: String,
Expand Down Expand Up @@ -360,13 +390,42 @@ public func withSpan<T, Instant: TracerInstant>(
/// - Parameters:
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - ofKind: The ``SpanKind`` of the new ``Span``.
/// - isolation: Defaulted parameter for inheriting isolation of calling actor
/// - 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.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
#if swift(>=6.0)
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
public func withSpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
isolation: isolated(any Actor)? = #isolation,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (any Span) async throws -> T
) async rethrows -> T {
try await InstrumentationSystem.legacyTracer.withAnySpan(
operationName,
at: DefaultTracerClock.now,
context: context(),
ofKind: kind,
function: function,
file: fileID,
line: line
) { anySpan in
try await operation(anySpan)
}
}
#endif

@_disfavoredOverload
@available(*, deprecated, message: "Prefer #isolation version of this API")
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
public func withSpan<T>(
_ operationName: String,
Expand Down Expand Up @@ -407,12 +466,42 @@ public func withSpan<T>(
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - instant: the time instant at which the span started
/// - isolation: Defaulted parameter for inheriting isolation of calling actor
/// - 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.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
#if swift(>=6.0)
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public func withSpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
isolation: isolated(any Actor)? = #isolation,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (any Span) async throws -> T
) async rethrows -> T {
try await InstrumentationSystem.legacyTracer.withAnySpan(
operationName,
at: instant(),
context: context(),
ofKind: kind,
function: function,
file: fileID,
line: line
) { anySpan in
try await operation(anySpan)
}
}
#endif

@_disfavoredOverload
@available(*, deprecated, message: "Prefer #isolation version of this API")
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public func withSpan<T>(
_ operationName: String,
Expand Down
100 changes: 100 additions & 0 deletions Sources/Tracing/TracerProtocol+Legacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,48 @@ extension LegacyTracer {
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - isolation: Defaulted parameter for inheriting isolation of calling actor
/// - 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.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
#if swift(>=6.0)
public func withAnySpan<T, Instant: TracerInstant>(
_ operationName: String,
at instant: @autoclosure () -> Instant,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
isolation: isolated(any Actor)? = #isolation,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (any Tracing.Span) async throws -> T
) async rethrows -> T {
let span = self.startAnySpan(
operationName,
at: instant(),
context: context(),
ofKind: kind,
function: function,
file: fileID,
line: line
)
defer { span.end() }
do {
return try await ServiceContext.$current.withValue(span.context) {
try await operation(span)
}
} catch {
span.recordError(error)
throw error // rethrow
}
}
#endif

@_disfavoredOverload
@available(*, deprecated, message: "Prefer #isolation version of this API")
public func withAnySpan<T, Instant: TracerInstant>(
_ operationName: String,
at instant: @autoclosure () -> Instant,
Expand Down Expand Up @@ -354,12 +390,47 @@ extension LegacyTracer {
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - isolation: Defaulted parameter for inheriting isolation of calling actor
/// - 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.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
#if swift(>=6.0)
public func withAnySpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
isolation: (any Actor)? = #isolation,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (any Tracing.Span) async throws -> T
) async rethrows -> T {
let span = self.startAnySpan(
operationName,
at: DefaultTracerClock.now,
context: context(),
ofKind: kind,
function: function,
file: fileID,
line: line
)
defer { span.end() }
do {
return try await ServiceContext.$current.withValue(span.context) {
try await operation(span)
}
} catch {
span.recordError(error)
throw error // rethrow
}
}
#endif

@_disfavoredOverload
@available(*, deprecated, message: "Prefer #isolation version of this API")
public func withAnySpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
Expand Down Expand Up @@ -524,12 +595,41 @@ extension Tracer {
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - instant: the time instant at which the span started
/// - isolation: Defaulted parameter for inheriting isolation of calling actor
/// - 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.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
#if swift(>=6.0)
public func withAnySpan<T>(
_ operationName: String,
at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
isolation: (any Actor)? = #isolation,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
@_inheritActorContext @_implicitSelfCapture _ operation: (any Tracing.Span) async throws -> T
) async rethrows -> T {
try await self.withSpan(
operationName,
context: context(),
ofKind: kind,
at: instant(),
function: function,
file: fileID,
line: line
) { span in
try await operation(span)
}
}
#endif

@_disfavoredOverload
@available(*, deprecated, message: "Prefer #isolation version of this API")
public func withAnySpan<T>(
_ operationName: String,
at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
Expand Down
71 changes: 71 additions & 0 deletions Sources/Tracing/TracerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,47 @@ extension Tracer {
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - instant: the time instant at which the span started
/// - isolation: Defaulted parameter for inheriting isolation of calling actor
/// - 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.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
#if swift(>=6.0)
public func withSpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
isolation: (any Actor)? = #isolation,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
@_inheritActorContext @_implicitSelfCapture _ operation: (Self.Span) async throws -> T
) async rethrows -> T {
let span = self.startSpan(
operationName,
context: context(),
ofKind: kind,
at: DefaultTracerClock.now,
function: function,
file: fileID,
line: line
)
defer { span.end() }
do {
return try await ServiceContext.$current.withValue(span.context) {
try await operation(span)
}
} catch {
span.recordError(error)
throw error // rethrow
}
}
#endif

@_disfavoredOverload
@available(*, deprecated, message: "Prefer #isolation version of this API")
public func withSpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
Expand Down Expand Up @@ -287,12 +322,48 @@ extension Tracer {
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - instant: the time instant at which the span started
/// - isolation: Defaulted parameter for inheriting isolation of calling actor
/// - 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.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
#if swift(>=6.0)
public func withSpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
isolation: (any Actor)? = #isolation,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
@_inheritActorContext @_implicitSelfCapture _ operation: (Self.Span) async throws -> T
) async rethrows -> T {
let span = self.startSpan(
operationName,
context: context(),
ofKind: kind,
at: instant(),
function: function,
file: fileID,
line: line
)
defer { span.end() }
do {
return try await ServiceContext.$current.withValue(span.context) {
try await operation(span)
}
} catch {
span.recordError(error)
throw error // rethrow
}
}
#endif

@_disfavoredOverload
@available(*, deprecated, message: "Prefer #isolation version of this API")
public func withSpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
Expand Down
2 changes: 2 additions & 0 deletions Tests/TracingTests/ActorTracingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class ActorTracingTests: XCTestCase {
super.tearDown()
InstrumentationSystem.bootstrapInternal(nil)
}

func test() {}
}

func work() async {}
Expand Down