|
15 | 15 | /// combining `TraceParent` and `TraceState`.
|
16 | 16 | public struct TraceContext: Equatable {
|
17 | 17 | /// The `TraceParent` identifying this trace context.
|
18 |
| - public let parent: TraceParent |
| 18 | + public private(set) var parent: TraceParent |
19 | 19 |
|
20 | 20 | /// The `TraceState` containing potentially vendor-specific trace information.
|
21 | 21 | public let state: TraceState
|
@@ -44,3 +44,37 @@ public struct TraceContext: Equatable {
|
44 | 44 | self.state = TraceState(rawValue: stateRawValue) ?? TraceState([])
|
45 | 45 | }
|
46 | 46 | }
|
| 47 | + |
| 48 | +extension TraceContext { |
| 49 | + /// Replace the parent-id of the current `traceParent` with a newly generated one, using the system random number generator. |
| 50 | + public mutating func regenerateParentID() { |
| 51 | + var generator = SystemRandomNumberGenerator() |
| 52 | + self.regenerateParentID(using: &generator) |
| 53 | + } |
| 54 | + |
| 55 | + /// Replace the parent-id of the current `traceParent` with a newly generated one, using the given generator as a source for |
| 56 | + /// randomness. |
| 57 | + /// |
| 58 | + /// - Parameter generator: The random number generator used as a source for generating the new parent-id. |
| 59 | + public mutating func regenerateParentID<G: RandomNumberGenerator>(using generator: inout G) { |
| 60 | + self.parent.parentID = TraceParent.randomParentID(using: &generator) |
| 61 | + } |
| 62 | + |
| 63 | + /// Return a copy with its `traceParent.parentID` replaced with a newly generated one, using the system random number generator. |
| 64 | + /// |
| 65 | + /// - Returns: A copy of this `TraceContext` with a new trace-parent parent-id. |
| 66 | + public func regeneratingParentID() -> TraceContext { |
| 67 | + var generator = SystemRandomNumberGenerator() |
| 68 | + return self.regeneratingParentID(using: &generator) |
| 69 | + } |
| 70 | + |
| 71 | + /// Return a copy with its `traceParent.parentID` replaced with a newly generated one, using the system random number generator. |
| 72 | + /// |
| 73 | + /// - Parameter generator: The random number generator used as a source for generating the new parent-id. |
| 74 | + /// - Returns: A copy of this `TraceContext` with a new trace-parent parent-id. |
| 75 | + public func regeneratingParentID<G: RandomNumberGenerator>(using generator: inout G) -> TraceContext { |
| 76 | + var copy = self |
| 77 | + copy.regenerateParentID(using: &generator) |
| 78 | + return copy |
| 79 | + } |
| 80 | +} |
0 commit comments