Skip to content

Commit da2fc15

Browse files
committed
[Tracing] Further try to simplify availaabilty woes for call-sites
1 parent 7396854 commit da2fc15

File tree

5 files changed

+18
-29
lines changed

5 files changed

+18
-29
lines changed

Sources/AsyncHTTPClient/AsyncAwait/Transaction.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ final class Transaction:
3636
let preferredEventLoop: EventLoop
3737
let requestOptions: RequestOptions
3838

39-
#if TracingSupport
40-
let span: (any Span)?
41-
#endif
42-
4339
private let state: NIOLockedValueBox<StateMachine>
4440

4541
#if TracingSupport
@@ -57,7 +53,6 @@ final class Transaction:
5753
self.logger = logger
5854
self.connectionDeadline = connectionDeadline
5955
self.preferredEventLoop = preferredEventLoop
60-
self.span = span
6156
self.state = NIOLockedValueBox(StateMachine(responseContinuation))
6257
}
6358
#endif // TracingSupport
@@ -70,23 +65,17 @@ final class Transaction:
7065
preferredEventLoop: EventLoop,
7166
responseContinuation: CheckedContinuation<HTTPClientResponse, Error>
7267
) {
73-
print("[swift] new transaction = \(request)")
7468
self.request = request
7569
self.requestOptions = requestOptions
7670
self.logger = logger
7771
self.connectionDeadline = connectionDeadline
7872
self.preferredEventLoop = preferredEventLoop
79-
self.span = nil
8073
self.state = NIOLockedValueBox(StateMachine(responseContinuation))
8174
}
8275

8376
func cancel() {
8477
let error = CancellationError()
8578
self.fail(error)
86-
#if TracingSupport
87-
self.span?.recordError(error)
88-
self.span?.end()
89-
#endif
9079
}
9180

9281
// MARK: Request body helpers

Sources/AsyncHTTPClient/HTTPClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ public final class HTTPClient: Sendable {
808808
}
809809
}()
810810

811-
let task: HTTPClient.Task<Delegate.Response>
811+
let task: HTTPClient.Task<Delegate.Response>
812812
#if TracingSupport
813813
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
814814
task = Task<Delegate.Response>(
@@ -825,7 +825,7 @@ public final class HTTPClient: Sendable {
825825
)
826826
}
827827
#else
828-
let task = Task<Delegate.Response>(
828+
task = Task<Delegate.Response>(
829829
eventLoop: taskEL,
830830
logger: logger,
831831
makeOrGetFileIOThreadPool: self.makeOrGetFileIOThreadPool

Sources/AsyncHTTPClient/HTTPHandler.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,12 +929,11 @@ extension HTTPClient {
929929
public let logger: Logger // We are okay to store the logger here because a Task is for only one request.
930930

931931
#if TracingSupport
932-
let anyTracer: Optional<any Sendable> // Ok to store the tracer here because a Task is for only one request.
932+
let anyTracer: (any Sendable)? = nil // Ok to store the tracer here because a Task is for only one request.
933933

934934
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
935935
public var tracer: (any Tracer)? {
936936
get {
937-
print("[swift][\(#fileID):\(#line)] _tracer = \(anyTracer)")
938937
return anyTracer as! (any Tracer)?
939938
}
940939
}
@@ -973,7 +972,6 @@ extension HTTPClient {
973972
self.eventLoop = eventLoop
974973
self.promise = eventLoop.makePromise()
975974
self.logger = logger
976-
self.anyTracer = nil
977975
self.makeOrGetFileIOThreadPool = makeOrGetFileIOThreadPool
978976
self.state = NIOLockedValueBox(State(isCancelled: false, taskDelegate: nil))
979977
}
@@ -1004,7 +1002,6 @@ extension HTTPClient {
10041002
self.eventLoop = eventLoop
10051003
self.promise = eventLoop.makePromise()
10061004
self.logger = logger
1007-
print("[swift] set any tracer on TASK = \(tracer)")
10081005
self.anyTracer = tracer
10091006
self.makeOrGetFileIOThreadPool = makeOrGetFileIOThreadPool
10101007
self.state = NIOLockedValueBox(State(isCancelled: false, taskDelegate: nil))

Sources/AsyncHTTPClient/RequestBag.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,21 @@ final class RequestBag<Delegate: HTTPClientResponseDelegate & Sendable>: Sendabl
6969
self.task.logger
7070
}
7171

72+
// Available unconditionally, so we can simplify callsites which can just try to pass this value
73+
// regardless if the real tracer exists or not.
7274
var anyTracer: (any Sendable)? {
75+
#if TracingSupport
7376
self.task.anyTracer
77+
#else
78+
nil
79+
#endif
7480
}
81+
#if TracingSupport
7582
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
7683
var tracer: (any Tracer)? {
7784
self.task.tracer
7885
}
86+
#endif // TracingSupport
7987

8088
let connectionDeadline: NIODeadline
8189

@@ -101,8 +109,6 @@ final class RequestBag<Delegate: HTTPClientResponseDelegate & Sendable>: Sendabl
101109
self.eventLoopPreference = eventLoopPreference
102110
self.task = task
103111

104-
assert(task.anyTracer != nil, "tracer was nil!")
105-
106112
let loopBoundState = LoopBoundState(
107113
request: request,
108114
state: StateMachine(redirectHandler: redirectHandler),
@@ -134,7 +140,6 @@ final class RequestBag<Delegate: HTTPClientResponseDelegate & Sendable>: Sendabl
134140

135141
private func willExecuteRequest0(_ executor: HTTPRequestExecutor) {
136142
// Immediately start a span for the "whole" request
137-
print("[swift] WILL EXECUTE \(self.anyTracer)")
138143
self.loopBoundState.value.startRequestSpan(tracer: self.anyTracer)
139144

140145
let action = self.loopBoundState.value.state.willExecuteRequest(executor)

Sources/AsyncHTTPClient/TracingSupport.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protocol _TracingSupportOperations {
4646
// associatedtype TracerType
4747

4848
/// Starts the "overall" Span that encompases the beginning of a request until receipt of the head part of the response.
49-
mutating func startRequestSpan(tracer: Any?)
49+
mutating func startRequestSpan<T>(tracer: T?)
5050

5151
/// Fails the active overall span given some internal error, e.g. timeout, pool shutdown etc.
5252
/// This is not to be used for failing a span given a failure status coded HTTPResponse.
@@ -64,13 +64,14 @@ extension RequestBag.LoopBoundState: _TracingSupportOperations {}
6464
#if !TracingSupport
6565
/// Operations used to start/end spans at apropriate times from the Request lifecycle.
6666
extension RequestBag.LoopBoundState {
67-
typealias TracerType = HTTPClientTracingSupportTracerType
68-
6967
@inlinable
70-
mutating func startRequestSpan(tracer: Any?) {}
68+
mutating func startRequestSpan<T>(tracer: T?) {}
7169

7270
@inlinable
7371
mutating func failRequestSpan(error: any Error) {}
72+
73+
@inlinable
74+
mutating func failRequestSpanAsCancelled() {}
7475

7576
@inlinable
7677
mutating func endRequestSpan(response: HTTPResponseHead) {}
@@ -81,12 +82,9 @@ extension RequestBag.LoopBoundState {
8182
extension RequestBag.LoopBoundState {
8283
// typealias TracerType = Tracer
8384

84-
mutating func startRequestSpan(tracer: Any?) {
85+
mutating func startRequestSpan<T>(tracer: T?) {
8586
guard #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *),
86-
let tracer = tracer as? (any Tracer)?,
87-
let tracer else {
88-
// print("[swift][\(#fileID):\(#line)] MISSING TRACER: \(tracer)")
89-
fatalError("[swift][\(#fileID):\(#line)] MISSING TRACER: \(tracer)")
87+
let tracer = tracer as! (any Tracer)? else {
9088
return
9189
}
9290

0 commit comments

Comments
 (0)