Skip to content

Provide explicit set/get to avoid SpanAttributeConvertible wrapping #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/Tracing/SpanProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,16 @@ extension SpanAttributes {
}
}

/// Similar to `subscript(_:)` however returns the stored `SpanAttribute` rather than going through `SpanAttributeConvertible`.
public func get(_ name: String) -> SpanAttribute? {
self._attributes[name]
}

/// Similar to `subscript(_:)` however accepts a `SpanAttribute` rather than going through `SpanAttributeConvertible`.
public mutating func set(_ name: String, value: SpanAttribute?) {
self._attributes[name] = value
}

/// - Parameter callback: The function to call for each attribute.
public func forEach(_ callback: (String, SpanAttribute) -> Void) {
self._attributes.forEach {
Expand Down
37 changes: 35 additions & 2 deletions Tests/TracingTests/SpanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,48 @@ final class SpanTests: XCTestCase {

XCTAssertEqual(child.links.count, 1)
XCTAssertEqual(child.links[0].baggage[TestBaggageContextKey.self], "test")
#if swift(>=5.2)
XCTAssertEqual(child.links[0].attributes.sampleHttp.statusCode, 418)
#endif
guard case .some(.int64(let statusCode)) = child.links[0].attributes["http.status_code"]?.toSpanAttribute() else {
XCTFail("Expected int value for http.status_code")
return
}
XCTAssertEqual(statusCode, 418)
}

func testSpanAttributeSetterGetter() {
var parentBaggage = Baggage.topLevel
parentBaggage[TestBaggageContextKey.self] = "test"

let parent = TestSpan(
operationName: "client",
startTime: .now(),
baggage: parentBaggage,
kind: .client,
onEnd: { _ in }
)
let childBaggage = Baggage.topLevel
let child = TestSpan(
operationName: "server",
startTime: .now(),
baggage: childBaggage,
kind: .server,
onEnd: { _ in }
)

var attributes = SpanAttributes()
attributes.set("http.status_code", value: .int32(418))
child.addLink(parent, attributes: attributes)

XCTAssertEqual(child.links.count, 1)
XCTAssertEqual(child.links[0].baggage[TestBaggageContextKey.self], "test")
XCTAssertEqual(child.links[0].attributes.sampleHttp.statusCode, 418)
guard case .some(.int32(let statusCode)) = child.links[0].attributes["http.status_code"]?.toSpanAttribute() else {
XCTFail("Expected int value for http.status_code")
return
}
XCTAssertEqual(statusCode, 418)
XCTAssertEqual(attributes.get("http.status_code"), SpanAttribute.int32(418))
}
}

// ==== ----------------------------------------------------------------------------------------------------------------
Expand Down