Skip to content

Commit

Permalink
Use Span.updateAttributes from swift-distributed-tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Dec 30, 2024
1 parent 507c56e commit c07c030
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
19 changes: 1 addition & 18 deletions Sources/Hummingbird/Middleware/TracingMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public struct TracingMiddleware<Context: RequestContext>: 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)
Expand Down Expand Up @@ -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
}
}

0 comments on commit c07c030

Please sign in to comment.