12
12
//
13
13
//===----------------------------------------------------------------------===//
14
14
15
- #if TracingSupport
16
-
17
15
import Logging
18
16
import NIOConcurrencyHelpers
19
17
import NIOCore
20
18
import NIOHTTP1
21
19
import NIOSSL
22
20
21
+ #if TracingSupport
23
22
import 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
24
66
25
67
extension RequestBag . LoopBoundState {
68
+ typealias TracerType = Tracer
26
69
27
70
mutating func startRequestSpan( tracer: ( any Tracer ) ? ) {
28
71
guard let tracer else {
@@ -33,7 +76,7 @@ extension RequestBag.LoopBoundState {
33
76
self . activeRequestSpan = tracer. startSpan ( " \( request. method) " )
34
77
}
35
78
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.
37
80
mutating func failRequestSpan( error: any Error ) {
38
81
guard let span = activeRequestSpan else {
39
82
return
@@ -49,7 +92,7 @@ extension RequestBag.LoopBoundState {
49
92
return
50
93
}
51
94
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) )
53
96
if response. status. code >= 400 {
54
97
span. setStatus ( . init( code: . error) )
55
98
}
0 commit comments