From a64a0abc2530f767af15dd88dda7f64d5f1ff9de Mon Sep 17 00:00:00 2001 From: Moritz Lang <16192401+slashmo@users.noreply.github.com> Date: Tue, 14 Jan 2025 13:17:38 +0100 Subject: [PATCH] Add recordingSpan and currentSpan methods to TracerProtocol (#160) --- Sources/Tracing/TracerProtocol.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Sources/Tracing/TracerProtocol.swift b/Sources/Tracing/TracerProtocol.swift index 4fdbea8..ea4c334 100644 --- a/Sources/Tracing/TracerProtocol.swift +++ b/Sources/Tracing/TracerProtocol.swift @@ -59,6 +59,14 @@ public protocol Tracer: LegacyTracer { file fileID: String, line: UInt ) -> Self.Span + + /// Retrieve the recording span for the given `ServiceContext`. + /// + /// - Note: This API does not enable look up of already finished spans. + /// It was added retroactively with a default implementation returning `nil` and therefore isn't guaranteed to be implemented by all `Tracer`s. + /// - Parameter context: The context containing information that uniquely identifies the span being obtained. + /// - Returns: The span identified by the given `ServiceContext` in case it's still recording. + func activeSpan(identifiedBy context: ServiceContext) -> Span? } @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext @@ -106,6 +114,15 @@ extension Tracer { line: line ) } + + /// Default implementation for ``activeSpan(identifiedBy:)`` which always returns `nil`. + /// This default exists in order to facilitate source-compatible introduction of the ``activeSpan(identifiedBy:)`` protocol requirement. + /// + /// - Parameter context: The context containing information that uniquely identifies the span being obtained. + /// - Returns: `nil`. + public func activeSpan(identifiedBy context: ServiceContext) -> Span? { + nil + } } // ==== ----------------------------------------------------------------------------------------------------------------