From fe5dc71dd9217c148cac995abf58f408fbf86559 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Wed, 6 Sep 2023 07:49:35 +0100 Subject: [PATCH 1/3] Add Span.updateAttributes --- Sources/Tracing/SpanProtocol.swift | 17 +++++++++++++++++ Tests/TracingTests/SpanTests.swift | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/Sources/Tracing/SpanProtocol.swift b/Sources/Tracing/SpanProtocol.swift index 3b919093..a9d50040 100644 --- a/Sources/Tracing/SpanProtocol.swift +++ b/Sources/Tracing/SpanProtocol.swift @@ -161,6 +161,23 @@ extension Span { } } +extension Span { + /// Update Span attributes in a block instead of individually + /// + /// Updating a span attribute will involve some type of thread synchronisation + /// primitive to avoid multiple threads updating the attributes at the same + /// time. If you update each attributes individually this can cause slowdown. + /// This function updates the attributes in one call to avoid hitting the + /// thread synchronisation code multiple times + /// + /// - Parameter update: closure used to update span attributes + public func updateAttributes(_ update: (inout SpanAttributes) -> Void) { + var attributes = self.attributes + update(&attributes) + self.attributes = attributes + } +} + // ==== ---------------------------------------------------------------------------------------------------------------- // MARK: Span Event diff --git a/Tests/TracingTests/SpanTests.swift b/Tests/TracingTests/SpanTests.swift index 37548919..9a52f963 100644 --- a/Tests/TracingTests/SpanTests.swift +++ b/Tests/TracingTests/SpanTests.swift @@ -248,6 +248,23 @@ final class SpanTests: XCTestCase { XCTAssertEqual(statusCode, 418) XCTAssertEqual(attributes.get("http.status_code"), SpanAttribute.int32(418)) } + + func testSpanUpdateAttributes() { + let span = TestSpan( + operationName: "client", + startTime: DefaultTracerClock.now, + context: ServiceContext.topLevel, + kind: .client, + onEnd: { _ in } + ) + span.updateAttributes { attributes in + attributes.set("http.status_code", value: .int32(200)) + attributes.set("http.method", value: .string("GET")) + } + + XCTAssertEqual(span.attributes.get("http.status_code"), .int32(200)) + XCTAssertEqual(span.attributes.get("http.method"), .string("GET")) + } } // ==== ---------------------------------------------------------------------------------------------------------------- From 35f4f2a23b4d95fee1ff81197d9870bad9611196 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Wed, 6 Sep 2023 16:57:42 +0100 Subject: [PATCH 2/3] Update Sources/Tracing/SpanProtocol.swift Co-authored-by: Moritz Lang --- Sources/Tracing/SpanProtocol.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Tracing/SpanProtocol.swift b/Sources/Tracing/SpanProtocol.swift index a9d50040..24a65722 100644 --- a/Sources/Tracing/SpanProtocol.swift +++ b/Sources/Tracing/SpanProtocol.swift @@ -162,7 +162,7 @@ extension Span { } extension Span { - /// Update Span attributes in a block instead of individually + /// Update Span attributes in a block instead of individually. /// /// Updating a span attribute will involve some type of thread synchronisation /// primitive to avoid multiple threads updating the attributes at the same From 49ce2a6b2f90fdc99cab8e1b9c411cea94198bab Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Wed, 6 Sep 2023 16:57:57 +0100 Subject: [PATCH 3/3] Update Sources/Tracing/SpanProtocol.swift Co-authored-by: Moritz Lang --- Sources/Tracing/SpanProtocol.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Tracing/SpanProtocol.swift b/Sources/Tracing/SpanProtocol.swift index 24a65722..c5ae484a 100644 --- a/Sources/Tracing/SpanProtocol.swift +++ b/Sources/Tracing/SpanProtocol.swift @@ -168,7 +168,7 @@ extension Span { /// primitive to avoid multiple threads updating the attributes at the same /// time. If you update each attributes individually this can cause slowdown. /// This function updates the attributes in one call to avoid hitting the - /// thread synchronisation code multiple times + /// thread synchronisation code multiple times. /// /// - Parameter update: closure used to update span attributes public func updateAttributes(_ update: (inout SpanAttributes) -> Void) {