Skip to content

Commit 7af28be

Browse files
committed
Add recordingSpan and currentSpan methods to TracerProtocol
**Motivation:** Currently, it is only possible to implicitly work with the current span by transparently creating a child span (using ServiceContext.current) under the hood. This is sufficient for almost all use-cases, but does not work in case a piece of code wants to add an event to the current span without having a handle on said span, e.g. if the span was created by a library. **Modifications:** I added a `recordingSpan(identifiedBy context: ServiceContext) -> Span?` requirement to `TracerProtocol`. This way, `Tracer` implementations may look up and return a span identified by the data they stored in the provided `ServiceContext`. It's worth noting that this method is only intended for obtaining spans which are still recording as opposed to already ended ones that may not even be in memory anymore. I also added a default implementation of this method to avoid introducing a breaking change. On top of this new protocol requirement, I added an extension to `TracerProtocol` with sugar to obtain the current span based on the task-local `ServiceContext`. **Result:** Library authors and application developers are now able to look up the current recording span to interact with it by e.g. adding events and attributes.
1 parent 5293d4e commit 7af28be

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Sources/Tracing/TracerProtocol.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public protocol Tracer: LegacyTracer {
5959
file fileID: String,
6060
line: UInt
6161
) -> Self.Span
62+
63+
/// Retrieve the recording span for the given `ServiceContext`.
64+
///
65+
/// - Note: This API does not enable look up of already finished spans.
66+
/// It was added retroactively with a default implementation returning `nil` and therefore isn't guaranteed to be implemented by all `Tracer`s.
67+
/// - Parameter context: The context containing information that uniquely identifies the span being obtained.
68+
/// - Returns: The span identified by the given `ServiceContext` in case it's still recording.
69+
func recordingSpan(identifiedBy context: ServiceContext) -> Span?
6270
}
6371

6472
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
@@ -106,6 +114,24 @@ extension Tracer {
106114
line: line
107115
)
108116
}
117+
118+
/// Default implementation for ``recordingSpan(identifiedBy:)`` which always returns `nil`.
119+
/// This default exists in order to facilitate source-compatible introduction of the ``recordingSpan(identifiedBy:)`` protocol requirement.
120+
///
121+
/// - Parameter context: The context containing information that uniquely identifies the span being obtained.
122+
/// - Returns: `nil`.
123+
public func recordingSpan(identifiedBy context: ServiceContext) -> Span? {
124+
nil
125+
}
126+
127+
/// Attempt to retrieve the current recording span based on the task-local `ServiceContext`.
128+
///
129+
/// - Note: This method should be considered best-effort as it might not (yet) be supported by the application author's `Tracer` of choice.
130+
/// - Returns: A span if one can be obtained via the task-local `ServiceContext`.
131+
public func currentSpan() -> Span? {
132+
guard let context = ServiceContext.current else { return nil }
133+
return recordingSpan(identifiedBy: context)
134+
}
109135
}
110136

111137
// ==== ----------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)