Skip to content

Commit b42cf43

Browse files
authored
Provide explicit set/get to avoid SpanAttributeConvertible wrapping (#95)
1 parent e9c7324 commit b42cf43

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

Sources/Tracing/SpanProtocol.swift

+10
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,16 @@ extension SpanAttributes {
537537
}
538538
}
539539

540+
/// Similar to `subscript(_:)` however returns the stored `SpanAttribute` rather than going through `SpanAttributeConvertible`.
541+
public func get(_ name: String) -> SpanAttribute? {
542+
self._attributes[name]
543+
}
544+
545+
/// Similar to `subscript(_:)` however accepts a `SpanAttribute` rather than going through `SpanAttributeConvertible`.
546+
public mutating func set(_ name: String, value: SpanAttribute?) {
547+
self._attributes[name] = value
548+
}
549+
540550
/// - Parameter callback: The function to call for each attribute.
541551
public func forEach(_ callback: (String, SpanAttribute) -> Void) {
542552
self._attributes.forEach {

Tests/TracingTests/SpanTests.swift

+35-2
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,48 @@ final class SpanTests: XCTestCase {
181181

182182
XCTAssertEqual(child.links.count, 1)
183183
XCTAssertEqual(child.links[0].baggage[TestBaggageContextKey.self], "test")
184-
#if swift(>=5.2)
185184
XCTAssertEqual(child.links[0].attributes.sampleHttp.statusCode, 418)
186-
#endif
187185
guard case .some(.int64(let statusCode)) = child.links[0].attributes["http.status_code"]?.toSpanAttribute() else {
188186
XCTFail("Expected int value for http.status_code")
189187
return
190188
}
191189
XCTAssertEqual(statusCode, 418)
192190
}
191+
192+
func testSpanAttributeSetterGetter() {
193+
var parentBaggage = Baggage.topLevel
194+
parentBaggage[TestBaggageContextKey.self] = "test"
195+
196+
let parent = TestSpan(
197+
operationName: "client",
198+
startTime: .now(),
199+
baggage: parentBaggage,
200+
kind: .client,
201+
onEnd: { _ in }
202+
)
203+
let childBaggage = Baggage.topLevel
204+
let child = TestSpan(
205+
operationName: "server",
206+
startTime: .now(),
207+
baggage: childBaggage,
208+
kind: .server,
209+
onEnd: { _ in }
210+
)
211+
212+
var attributes = SpanAttributes()
213+
attributes.set("http.status_code", value: .int32(418))
214+
child.addLink(parent, attributes: attributes)
215+
216+
XCTAssertEqual(child.links.count, 1)
217+
XCTAssertEqual(child.links[0].baggage[TestBaggageContextKey.self], "test")
218+
XCTAssertEqual(child.links[0].attributes.sampleHttp.statusCode, 418)
219+
guard case .some(.int32(let statusCode)) = child.links[0].attributes["http.status_code"]?.toSpanAttribute() else {
220+
XCTFail("Expected int value for http.status_code")
221+
return
222+
}
223+
XCTAssertEqual(statusCode, 418)
224+
XCTAssertEqual(attributes.get("http.status_code"), SpanAttribute.int32(418))
225+
}
193226
}
194227

195228
// ==== ----------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)