|
11 | 11 | // |
12 | 12 | //===----------------------------------------------------------------------===// |
13 | 13 |
|
14 | | -extension W3C { |
15 | | - public struct TraceParent { |
16 | | - public let traceID: String |
17 | | - public let parentID: String |
18 | | - public let traceFlags: String |
| 14 | +/// Represents the incoming request in a tracing system in a common format, understood by all vendors. |
| 15 | +/// |
| 16 | +/// Example raw value: `00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01` |
| 17 | +/// |
| 18 | +/// - SeeAlso: [W3C TraceContext: TraceParent](https://www.w3.org/TR/2020/REC-trace-context-1-20200206/#traceparent-header) |
| 19 | +public struct TraceParent { |
| 20 | + /// The ID of the whole trace forest, used to uniquely identify a distributed trace through a system. |
| 21 | + /// |
| 22 | + /// - SeeAlso: [W3C TraceContext: trace-id](https://www.w3.org/TR/2020/REC-trace-context-1-20200206/#trace-id) |
| 23 | + public let traceID: String |
19 | 24 |
|
20 | | - public init(traceID: String, parentID: String, traceFlags: String) { |
21 | | - self.traceID = traceID |
22 | | - self.parentID = parentID |
23 | | - self.traceFlags = traceFlags |
24 | | - } |
| 25 | + /// The ID of the incoming request as known by the caller (in some tracing systems, this is known as the span-id, where a span is the execution of |
| 26 | + /// a client request). |
| 27 | + /// |
| 28 | + /// - SeeAlso: [W3C TraceContext: parent-id](https://www.w3.org/TR/2020/REC-trace-context-1-20200206/#parent-id) |
| 29 | + public let parentID: String |
25 | 30 |
|
26 | | - public var sampled: Bool { |
27 | | - self.traceFlags == "01" |
28 | | - } |
| 31 | + /// An 8-bit field that controls tracing flags such as sampling, trace level, etc. |
| 32 | + /// |
| 33 | + /// - SeeAlso: [W3C TraceContext: trace-flags](https://www.w3.org/TR/2020/REC-trace-context-1-20200206/#trace-flags) |
| 34 | + public let traceFlags: String |
29 | 35 |
|
30 | | - public static let headerName = "traceparent" |
| 36 | + init(traceID: String, parentID: String, traceFlags: String) { |
| 37 | + self.traceID = traceID |
| 38 | + self.parentID = parentID |
| 39 | + self.traceFlags = traceFlags |
| 40 | + } |
31 | 41 |
|
32 | | - private static let version = "00" |
| 42 | + /// When `true`, the least significant bit (right-most), denotes that the caller may have recorded trace data. |
| 43 | + /// When `false`, the caller did not record trace data out-of-band. |
| 44 | + /// |
| 45 | + /// - SeeAlso: [W3C TraceContext: Sampled flag](https://www.w3.org/TR/2020/REC-trace-context-1-20200206/#sampled-flag) |
| 46 | + public var sampled: Bool { |
| 47 | + self.traceFlags == "01" |
33 | 48 | } |
34 | 49 |
|
35 | | - // TODO: Trace State |
| 50 | + /// The HTTP header name for `TraceParent`. |
| 51 | + public static let headerName = "traceparent" |
| 52 | + |
| 53 | + /// Hard-coded version to "00" as it's the only version currently supported by this package. |
| 54 | + private static let version = "00" |
36 | 55 | } |
37 | 56 |
|
38 | | -extension W3C.TraceParent: Equatable { |
| 57 | +extension TraceParent: Equatable { |
| 58 | + // custom implementation to avoid automatic equality check of rawValue which is unnecessary computational overhead |
39 | 59 | public static func == (lhs: Self, rhs: Self) -> Bool { |
40 | 60 | lhs.traceID == rhs.traceID |
41 | 61 | && lhs.parentID == rhs.parentID |
42 | 62 | && lhs.traceFlags == rhs.traceFlags |
43 | 63 | } |
44 | 64 | } |
45 | 65 |
|
46 | | -extension W3C.TraceParent: RawRepresentable { |
| 66 | +extension TraceParent: RawRepresentable { |
| 67 | + /// Initialize a `TraceParent` from an HTTP header value. Fails if the value cannot be parsed. |
| 68 | + /// |
| 69 | + /// - Parameter rawValue: The value of the traceparent HTTP header. |
47 | 70 | public init?(rawValue: String) { |
48 | 71 | guard rawValue.count == 55 else { return nil } |
49 | 72 |
|
@@ -72,7 +95,14 @@ extension W3C.TraceParent: RawRepresentable { |
72 | 95 | self.traceFlags = String(traceFlagsComponent) |
73 | 96 | } |
74 | 97 |
|
| 98 | + /// A `String` representation of this trace parent, suitable for injecting into HTTP headers. |
75 | 99 | public var rawValue: String { |
76 | 100 | "\(Self.version)-\(self.traceID)-\(self.parentID)-\(self.traceFlags)" |
77 | 101 | } |
78 | 102 | } |
| 103 | + |
| 104 | +extension TraceParent: CustomStringConvertible { |
| 105 | + public var description: String { |
| 106 | + self.rawValue |
| 107 | + } |
| 108 | +} |
0 commit comments