Skip to content

Commit d33e3ee

Browse files
authored
Merge pull request #8 from slashmo/feature/regenerate-parent-id
Add methods for regenerating the parent id
2 parents 04676ba + 192bb33 commit d33e3ee

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

Sources/W3CTraceContext/TraceContext.swift

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/// combining `TraceParent` and `TraceState`.
1616
public struct TraceContext: Equatable {
1717
/// The `TraceParent` identifying this trace context.
18-
public let parent: TraceParent
18+
public private(set) var parent: TraceParent
1919

2020
/// The `TraceState` containing potentially vendor-specific trace information.
2121
public let state: TraceState
@@ -44,3 +44,37 @@ public struct TraceContext: Equatable {
4444
self.state = TraceState(rawValue: stateRawValue) ?? TraceState([])
4545
}
4646
}
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+
}

Sources/W3CTraceContext/TraceParent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public struct TraceParent {
2626
/// a client request).
2727
///
2828
/// - SeeAlso: [W3C TraceContext: parent-id](https://www.w3.org/TR/2020/REC-trace-context-1-20200206/#parent-id)
29-
public let parentID: String
29+
public internal(set) var parentID: String
3030

3131
/// An 8-bit field that controls tracing flags such as sampling, trace level, etc.
3232
///

Tests/W3CTraceContextTests/TraceContextTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,20 @@ final class TraceContextTests: XCTestCase {
6161
)
6262
)
6363
}
64+
65+
func test_regenerate_trace_parent_parent_id_in_place() {
66+
var traceContext = TraceContext(parent: .random(), state: .none)
67+
let previousTraceParentID = traceContext.parent.parentID
68+
69+
traceContext.regenerateParentID()
70+
71+
XCTAssertNotEqual(traceContext.parent.parentID, previousTraceParentID)
72+
}
73+
74+
func test_regenerate_trace_parent_parent_id() {
75+
let traceContext = TraceContext(parent: .random(), state: .none)
76+
let newTraceContext = traceContext.regeneratingParentID()
77+
78+
XCTAssertNotEqual(newTraceContext.parent.parentID, traceContext.parent.parentID)
79+
}
6480
}

0 commit comments

Comments
 (0)