Skip to content

Commit 888db17

Browse files
author
Ignacio Bonafonte
authored
Merge branch 'main' into fix-metrics-thread-races
2 parents 0c9253c + f5644b3 commit 888db17

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

Sources/OpenTelemetrySdk/Trace/AttributesDictionary.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct AttributesDictionary {
2929
}
3030
set {
3131
if newValue == nil {
32-
removeValueForKey(key: key)
32+
_ = removeValueForKey(key: key)
3333
} else {
3434
_ = updateValue(value: newValue!, forKey: key)
3535
}
@@ -81,11 +81,11 @@ public struct AttributesDictionary {
8181
}
8282
}
8383

84-
public mutating func removeValueForKey(key: String) {
84+
public mutating func removeValueForKey(key: String) -> AttributeValue? {
8585
keys = keys.filter {
8686
$0 != key
8787
}
88-
attributes.removeValue(forKey: key)
88+
return attributes.removeValue(forKey: key)
8989
}
9090

9191
public mutating func removeAll(keepCapacity: Int) {

Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ public class RecordEventsReadableSpan: ReadableSpan {
242242
}
243243

244244
if value == nil {
245-
attributes.removeValueForKey(key: key)
245+
if attributes.removeValueForKey(key: key) != nil {
246+
totalAttributeCount -= 1
247+
}
248+
return
246249
}
247250
totalAttributeCount += 1
248251
if attributes[key] == nil, totalAttributeCount > maxNumberOfAttributes {

Tests/OpenTelemetrySdkTests/Trace/RecordEventsReadableSpanTests.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,24 @@ class RecordEventsReadableSpanTest: XCTestCase {
235235
let spanData = span.toSpanData()
236236
XCTAssertEqual(spanData.events.count, 2)
237237
}
238-
238+
239239
func testWithInitializedAttributes() {
240-
241-
let attributes = ["hello" : AttributeValue.string("world")]
242-
240+
let attributes = ["hello": AttributeValue.string("world")]
241+
243242
let span = createTestSpan(attributes: attributes)
244-
243+
245244
XCTAssertEqual(attributes.count, span.totalAttributeCount, "total attributes not counted properly")
245+
XCTAssertEqual(span.toSpanData().attributes.count, span.totalAttributeCount, "total attributes not counted properly")
246+
247+
}
248+
249+
func testRemovingAttributes() {
250+
let attributes = ["remove": AttributeValue.string("me")]
251+
let span = createTestSpan(attributes: attributes)
252+
span.setAttribute(key: "keep", value: "me")
253+
span.setAttribute(key: "remove", value: nil)
254+
XCTAssertEqual(1, span.totalAttributeCount, "total attributes not counted properly")
255+
XCTAssertEqual(span.toSpanData().attributes.count, span.totalAttributeCount, "total attributes not counted properly")
246256
}
247257

248258
func testDroppingAttributes() {

0 commit comments

Comments
 (0)