Skip to content

Commit

Permalink
Add recordingSpan and currentSpan methods to TracerProtocol
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
slashmo committed Dec 18, 2024
1 parent 55a0bf7 commit cf16c0a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Sources/Tracing/TracerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ 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.
/// - 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 recordingSpan(identifiedBy context: ServiceContext) -> Span?
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
Expand Down Expand Up @@ -106,6 +113,23 @@ extension Tracer {
line: line
)
}

/// Attempt to retrieve the span for the given `ServiceContext`.
///
/// - Parameter context: The context containing information that uniquely identifies the span being obtained.
/// - Returns: The span which created the given `ServiceContext`.
public func recordingSpan(identifiedBy context: ServiceContext) -> Span? {
nil
}

/// Attempt to retrieve the current recording span based on the task-local `ServiceContext`.
///
/// - Returns: A span if one can be obtained via the task-local `ServiceContext`.
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func currentSpan() -> Span? {
guard let context = ServiceContext.current else { return nil }
return recordingSpan(identifiedBy: context)
}
}

// ==== ----------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit cf16c0a

Please sign in to comment.