File tree Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -529,6 +529,14 @@ extension SpanAttributes {
529
529
}
530
530
}
531
531
532
+ /// Merges the given `SpanAttributes` into these `SpanAttributes` by overwriting values of duplicate keys with those of the
533
+ /// `other` attributes.
534
+ ///
535
+ /// - Parameter other: `SpanAttributes` to merge.
536
+ public mutating func merge( _ other: SpanAttributes ) {
537
+ self . _attributes. merge ( other. _attributes, uniquingKeysWith: { _, rhs in rhs } )
538
+ }
539
+
532
540
/// - Returns: Number of attributes stored.
533
541
public var count : Int {
534
542
return self . _attributes. count
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ extension SpanTests {
35
35
( " testSpanAttributesUX " , testSpanAttributesUX) ,
36
36
( " testSpanAttributesCustomValue " , testSpanAttributesCustomValue) ,
37
37
( " testSpanAttributesAreIterable " , testSpanAttributesAreIterable) ,
38
+ ( " testSpanAttributesMerge " , testSpanAttributesMerge) ,
38
39
( " testSpanParentConvenience " , testSpanParentConvenience) ,
39
40
]
40
41
}
Original file line number Diff line number Diff line change @@ -137,6 +137,26 @@ final class SpanTests: XCTestCase {
137
137
}
138
138
}
139
139
140
+ func testSpanAttributesMerge( ) {
141
+ var attributes : SpanAttributes = [
142
+ " 0 " : 0 ,
143
+ " 1 " : true ,
144
+ " 2 " : " test " ,
145
+ ]
146
+ let other : SpanAttributes = [
147
+ " 0 " : 1 ,
148
+ " 1 " : false ,
149
+ " 3 " : " new " ,
150
+ ]
151
+
152
+ attributes. merge ( other)
153
+
154
+ XCTAssertEqual ( attributes [ " 0 " ] ? . toSpanAttribute ( ) , 1 )
155
+ XCTAssertEqual ( attributes [ " 1 " ] ? . toSpanAttribute ( ) , false )
156
+ XCTAssertEqual ( attributes [ " 2 " ] ? . toSpanAttribute ( ) , " test " )
157
+ XCTAssertEqual ( attributes [ " 3 " ] ? . toSpanAttribute ( ) , " new " )
158
+ }
159
+
140
160
func testSpanParentConvenience( ) {
141
161
var parentBaggage = Baggage . topLevel
142
162
parentBaggage [ TestBaggageContextKey . self] = " test "
You can’t perform that action at this time.
0 commit comments