Skip to content

Commit

Permalink
Introduce top level withSpan; reclaim Tracer for protocol
Browse files Browse the repository at this point in the history
**Motivation:**
The TracerProtocol naming was annoyingly problematic and turns out we
can and should actually move the withSpan APIs as top level functions
which is typical Swift practice for such "global functionality".

This way we can `withSpan() {}` with less noise, and also gain back the
Tracer protocol name.
  • Loading branch information
ktoso committed Mar 30, 2023
1 parent a5b5160 commit 470ab6a
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 317 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ Spans form hierarchies with their parent spans, and end up being visualized usin
The above trace is achieved by starting and ending spans in all the mentioned functions, for example, like this:

```swift
let tracer: any TracerProtocol
let tracer: any Tracer

func makeDinner(context: LoggingContext) async throws -> Meal {
tracer.withSpan(operationName: "makeDinner", context) {
Expand Down Expand Up @@ -538,8 +538,8 @@ func get(url: String, context: LoggingContext) {

## Instrument developers: Creating an instrument

Creating an instrument means adopting the `InstrumentProtocol` protocol (or `TracerProtocol` in case you develop a tracer).
`InstrumentProtocol` is part of the `Instrumentation` library & `Tracing` contains the `TracerProtocol` protocol.
Creating an instrument means adopting the `InstrumentProtocol` protocol (or `Tracer` in case you develop a tracer).
`InstrumentProtocol` is part of the `Instrumentation` library & `Tracing` contains the `Tracer` protocol.

`InstrumentProtocol` has two requirements:

Expand All @@ -556,7 +556,7 @@ how to retrieve values from the `LoggingContext` and how to set values on it.

When creating a tracer you need to create two types:

1. Your tracer conforming to `TracerProtocol`
1. Your tracer conforming to `Tracer`
2. A span class conforming to `Span`

> The `Span` conforms to the standard rules defined in [OpenTelemetry](https://github.com/open-telemetry/opentelemetry-specification/blob/v0.7.0/specification/trace/api.md#span), so if unsure about usage patterns, you can refer to this specification and examples referring to it.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Tracing/Docs.docc/InDepthGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Spans form hierarchies with their parent spans, and end up being visualized usin
The above trace is achieved by starting and ending spans in all the mentioned functions, for example, like this:

```swift
let tracer: any TracerProtocol
let tracer: any Tracer

func makeDinner(context: LoggingContext) async throws -> Meal {
tracer.withSpan(operationName: "makeDinner", context) {
Expand Down
12 changes: 6 additions & 6 deletions Sources/Tracing/InstrumentationSystem+Tracing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ extension InstrumentationSystem {
/// tracing instrument as passed to the multiplex instrument. If none is found, a ``NoOpTracer`` is returned.
///
/// - Returns: A ``Tracer`` if the system was bootstrapped with one, and ``NoOpTracer`` otherwise.
public static var tracer: any TracerProtocol {
let found: (any TracerProtocol)? =
(self._findInstrument(where: { $0 is (any TracerProtocol) }) as? (any TracerProtocol))
public static var tracer: any Tracer {
let found: (any Tracer)? =
(self._findInstrument(where: { $0 is (any Tracer) }) as? (any Tracer))
return found ?? NoOpTracer()
}
#endif
Expand All @@ -35,9 +35,9 @@ extension InstrumentationSystem {
/// tracing instrument as passed to the multiplex instrument. If none is found, a ``NoOpTracer`` is returned.
///
/// - Returns: A ``Tracer`` if the system was bootstrapped with one, and ``NoOpTracer`` otherwise.
public static var legacyTracer: any LegacyTracerProtocol {
let found: (any LegacyTracerProtocol)? =
(self._findInstrument(where: { $0 is (any LegacyTracerProtocol) }) as? (any LegacyTracerProtocol))
public static var legacyTracer: any LegacyTracer {
let found: (any LegacyTracer)? =
(self._findInstrument(where: { $0 is (any LegacyTracer) }) as? (any LegacyTracer))
return found ?? NoOpTracer()
}
}
4 changes: 2 additions & 2 deletions Sources/Tracing/NoOpTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Dispatch

/// Tracer that ignores all operations, used when no tracing is required.
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
public struct NoOpTracer: LegacyTracerProtocol {
public struct NoOpTracer: LegacyTracer {
public typealias TracerSpan = NoOpSpan

public init() {}
Expand Down Expand Up @@ -91,7 +91,7 @@ public struct NoOpTracer: LegacyTracerProtocol {
}

#if swift(>=5.7.0)
extension NoOpTracer: TracerProtocol {
extension NoOpTracer: Tracer {
public func startSpan<Clock: TracerClock>(
_ operationName: String,
baggage: @autoclosure () -> Baggage,
Expand Down
Loading

0 comments on commit 470ab6a

Please sign in to comment.