From 7a41c20c25866064f22b2bfa2c8194083e7e1595 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sun, 5 Jan 2025 15:01:58 +0000 Subject: [PATCH] Use `Span.updateAttributes` from swift-distributed-tracing (#646) --- Package.swift | 2 +- .../Middleware/TracingMiddleware.swift | 19 +------------------ 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/Package.swift b/Package.swift index fd165712..e2eeba86 100644 --- a/Package.swift +++ b/Package.swift @@ -24,7 +24,7 @@ let package = Package( .package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"), .package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0"), .package(url: "https://github.com/apple/swift-metrics.git", from: "2.5.0"), - .package(url: "https://github.com/apple/swift-distributed-tracing.git", from: "1.0.1"), + .package(url: "https://github.com/apple/swift-distributed-tracing.git", from: "1.1.0"), .package(url: "https://github.com/apple/swift-nio.git", from: "2.63.0"), .package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.20.0"), .package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.34.1"), diff --git a/Sources/Hummingbird/Middleware/TracingMiddleware.swift b/Sources/Hummingbird/Middleware/TracingMiddleware.swift index 8a5a334c..b97dc9ce 100644 --- a/Sources/Hummingbird/Middleware/TracingMiddleware.swift +++ b/Sources/Hummingbird/Middleware/TracingMiddleware.swift @@ -51,7 +51,7 @@ public struct TracingMiddleware: RouterMiddleware { // span name is updated after route has run let operationName = "HTTP \(request.method.rawValue) route not found" - let span = InstrumentationSystem.tracer.startSpan(operationName, context: serviceContext, ofKind: .server) + let span = startSpan(operationName, context: serviceContext, ofKind: .server) span.updateAttributes { attributes in if let staticAttributes = self.attributes { attributes.merge(staticAttributes) @@ -176,20 +176,3 @@ private struct HTTPHeadersExtractor: Extractor { return headers[headerName] } } - -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 could 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 - func updateAttributes(_ update: (inout SpanAttributes) -> Void) { - var attributes = self.attributes - update(&attributes) - self.attributes = attributes - } -}