Skip to content

Commit e889c1a

Browse files
committed
Introduce top level withSpan; reclaim Tracer for protocol
**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.
1 parent db10407 commit e889c1a

13 files changed

+400
-317
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ Spans form hierarchies with their parent spans, and end up being visualized usin
444444
The above trace is achieved by starting and ending spans in all the mentioned functions, for example, like this:
445445

446446
```swift
447-
let tracer: any TracerProtocol
447+
let tracer: any Tracer
448448

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

539539
## Instrument developers: Creating an instrument
540540

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

544544
`InstrumentProtocol` has two requirements:
545545

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

557557
When creating a tracer you need to create two types:
558558

559-
1. Your tracer conforming to `TracerProtocol`
559+
1. Your tracer conforming to `Tracer`
560560
2. A span class conforming to `Span`
561561

562562
> 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.

Sources/Tracing/Docs.docc/InDepthGuide.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Spans form hierarchies with their parent spans, and end up being visualized usin
188188
The above trace is achieved by starting and ending spans in all the mentioned functions, for example, like this:
189189

190190
```swift
191-
let tracer: any TracerProtocol
191+
let tracer: any Tracer
192192

193193
func makeDinner(context: LoggingContext) async throws -> Meal {
194194
tracer.withSpan(operationName: "makeDinner", context) {

Sources/Tracing/InstrumentationSystem+Tracing.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ extension InstrumentationSystem {
2222
/// tracing instrument as passed to the multiplex instrument. If none is found, a ``NoOpTracer`` is returned.
2323
///
2424
/// - Returns: A ``Tracer`` if the system was bootstrapped with one, and ``NoOpTracer`` otherwise.
25-
public static var tracer: any TracerProtocol {
26-
let found: (any TracerProtocol)? =
27-
(self._findInstrument(where: { $0 is (any TracerProtocol) }) as? (any TracerProtocol))
25+
public static var tracer: any Tracer {
26+
let found: (any Tracer)? =
27+
(self._findInstrument(where: { $0 is (any Tracer) }) as? (any Tracer))
2828
return found ?? NoOpTracer()
2929
}
3030
#endif
@@ -35,9 +35,9 @@ extension InstrumentationSystem {
3535
/// tracing instrument as passed to the multiplex instrument. If none is found, a ``NoOpTracer`` is returned.
3636
///
3737
/// - Returns: A ``Tracer`` if the system was bootstrapped with one, and ``NoOpTracer`` otherwise.
38-
public static var legacyTracer: any LegacyTracerProtocol {
39-
let found: (any LegacyTracerProtocol)? =
40-
(self._findInstrument(where: { $0 is (any LegacyTracerProtocol) }) as? (any LegacyTracerProtocol))
38+
public static var legacyTracer: any LegacyTracer {
39+
let found: (any LegacyTracer)? =
40+
(self._findInstrument(where: { $0 is (any LegacyTracer) }) as? (any LegacyTracer))
4141
return found ?? NoOpTracer()
4242
}
4343
}

Sources/Tracing/NoOpTracer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Dispatch
1818

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

2424
public init() {}
@@ -91,7 +91,7 @@ public struct NoOpTracer: LegacyTracerProtocol {
9191
}
9292

9393
#if swift(>=5.7.0)
94-
extension NoOpTracer: TracerProtocol {
94+
extension NoOpTracer: Tracer {
9595
public func startSpan<Clock: TracerClock>(
9696
_ operationName: String,
9797
baggage: @autoclosure () -> Baggage,

0 commit comments

Comments
 (0)