1212//
1313//===----------------------------------------------------------------------===//
1414
15- #if TracingSupport
16-
1715import Logging
1816import NIOConcurrencyHelpers
1917import NIOCore
2018import NIOHTTP1
2119import NIOSSL
2220
21+ #if TracingSupport
2322import Tracing
23+ #endif
24+
25+ #if TracingSupport
26+ typealias HTTPClientTracingSupportTracerType = Tracer
27+ #else
28+ enum TracingSupportDisabledTracer { }
29+ typealias HTTPClientTracingSupportTracerType = TracingSupportDisabledTracer
30+ #endif
31+
32+ protocol _TracingSupportOperations {
33+ associatedtype TracerType
34+
35+ /// Starts the "overall" Span that encompases the beginning of a request until receipt of the head part of the response.
36+ mutating func startRequestSpan( tracer: TracerType ? )
37+
38+ /// Fails the active overall span given some internal error, e.g. timeout, pool shutdown etc.
39+ /// This is not to be used for failing a span given a failure status coded HTTPResponse.
40+ mutating func failRequestSpan( error: any Error )
41+
42+ /// Ends the active overall span upon receipt of the response head.
43+ ///
44+ /// If the status code is in error range, this will automatically fail the span.
45+ mutating func endRequestSpan( response: HTTPResponseHead )
46+ }
47+
48+ extension RequestBag . LoopBoundState : _TracingSupportOperations { }
49+
50+ #if !TracingSupport
51+ /// Operations used to start/end spans at apropriate times from the Request lifecycle.
52+ extension RequestBag . LoopBoundState {
53+ typealias TracerType = HTTPClientTracingSupportTracerType
54+
55+ @inlinable
56+ mutating func startRequestSpan( tracer: TracerType ? ) { }
57+
58+ @inlinable
59+ mutating func failRequestSpan( error: any Error ) { }
60+
61+ @inlinable
62+ mutating func endRequestSpan( response: HTTPResponseHead ) { }
63+ }
64+
65+ #else // TracingSupport
2466
2567extension RequestBag . LoopBoundState {
68+ typealias TracerType = Tracer
2669
2770 mutating func startRequestSpan( tracer: ( any Tracer ) ? ) {
2871 guard let tracer else {
@@ -33,7 +76,7 @@ extension RequestBag.LoopBoundState {
3376 self . activeRequestSpan = tracer. startSpan ( " \( request. method) " )
3477 }
3578
36- // TODO: should be able to record the reason for the failure, e.g. timeout, cancellation, server error etc
79+ // TODO: should be able to record the reason for the failure, e.g. timeout, cancellation etc.
3780 mutating func failRequestSpan( error: any Error ) {
3881 guard let span = activeRequestSpan else {
3982 return
@@ -49,7 +92,7 @@ extension RequestBag.LoopBoundState {
4992 return
5093 }
5194
52- span. attributes [ " http.response.status_code " ] = SpanAttribute . int ( Int64 ( response. status. code) )
95+ span. attributes [ " http.response.status_code " ] = SpanAttribute . int64 ( Int64 ( response. status. code) )
5396 if response. status. code >= 400 {
5497 span. setStatus ( . init( code: . error) )
5598 }
0 commit comments