Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recordingSpan and currentSpan methods to TracerProtocol #160

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Sources/Tracing/TracerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 recordingSpan(identifiedBy context: ServiceContext) -> Span?
}

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

/// Default implementation for ``recordingSpan(identifiedBy:)`` which always returns `nil`.
/// This default exists in order to facilitate source-compatible introduction of the ``recordingSpan(identifiedBy:)`` protocol requirement.
///
/// - Parameter context: The context containing information that uniquely identifies the span being obtained.
/// - Returns: `nil`.
public func recordingSpan(identifiedBy context: ServiceContext) -> Span? {
nil
}

/// Attempt to retrieve the current recording span based on the task-local `ServiceContext`.
///
/// - Note: This method should be considered best-effort as it might not (yet) be supported by the application author's `Tracer` of choice.
/// - Returns: A span if one can be obtained via the task-local `ServiceContext`.
public func currentSpan() -> Span? {
guard let context = ServiceContext.current else { return nil }
return recordingSpan(identifiedBy: context)
}
}

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