Skip to content

Commit be47d00

Browse files
committed
Consistent protocol naming for Instrument :party:
1 parent e889c1a commit be47d00

13 files changed

+96
-52
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ This project uses the context progagation type defined independently in:
4040
+ [Extracting & injecting Baggage](#extracting--injecting-baggage)
4141
+ [Tracing your library](#tracing-your-library)
4242
* In-Depth Guide for: **Instrument developers**
43-
+ [Creating an `InstrumentProtocol`](#instrument-developers--creating-an-instrument)
43+
+ [Creating an `Instrument`](#instrument-developers--creating-an-instrument)
4444
+ [Creating a `Tracer`](#creating-a--tracer-)
4545
* [Contributing](#contributing)
4646

@@ -119,7 +119,7 @@ To your main target, add a dependency on `Tracing` library and the instrument yo
119119
),
120120
```
121121

122-
Then (in an application, libraries should _never_ invoke `bootstrap`), you will want to bootstrap the specific tracer you want to use in your application. A `Tracer` is a type of `InstrumentProtocol` and can be offered used to globally bootstrap the tracing system, like this:
122+
Then (in an application, libraries should _never_ invoke `bootstrap`), you will want to bootstrap the specific tracer you want to use in your application. A `Tracer` is a type of `Instrument` and can be offered used to globally bootstrap the tracing system, like this:
123123

124124

125125
```swift
@@ -295,7 +295,7 @@ To your main target, add a dependency on the `Instrumentation library` and the i
295295

296296
Instead of providing each instrumented library with a specific instrument explicitly, you *bootstrap* the
297297
`InstrumentationSystem` which acts as a singleton that libraries/frameworks access when calling out to the configured
298-
`InstrumentProtocol`:
298+
`Instrument`:
299299

300300
```swift
301301
InstrumentationSystem.bootstrap(FancyInstrument())
@@ -316,7 +316,7 @@ This is because tracing systems may attempt to emit metrics about their status e
316316

317317
#### Bootstrapping multiple instruments using MultiplexInstrument
318318

319-
It is important to note that `InstrumentationSystem.bootstrap(_: InstrumentProtocol)` must only be called once. In case you
319+
It is important to note that `InstrumentationSystem.bootstrap(_: Instrument)` must only be called once. In case you
320320
want to bootstrap the system to use multiple instruments, you group them in a `MultiplexInstrument` first, which you
321321
then pass along to the `bootstrap` method like this:
322322

@@ -481,7 +481,7 @@ func get(url: String, context: LoggingContext) {
481481
}
482482
```
483483

484-
On the receiving side, an HTTP server should use the following `InstrumentProtocol` API to extract the HTTP headers of the given
484+
On the receiving side, an HTTP server should use the following `Instrument` API to extract the HTTP headers of the given
485485
`HTTPRequest` into:
486486

487487
```swift
@@ -538,10 +538,10 @@ func get(url: String, context: LoggingContext) {
538538

539539
## Instrument developers: Creating an instrument
540540

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.
541+
Creating an instrument means adopting the `Instrument` protocol (or `Tracer` in case you develop a tracer).
542+
`Instrument` is part of the `Instrumentation` library & `Tracing` contains the `Tracer` protocol.
543543

544-
`InstrumentProtocol` has two requirements:
544+
`Instrument` has two requirements:
545545

546546
1. A method to inject values inside a `LoggingContext` into a generic carrier (e.g. HTTP headers)
547547
2. A method to extract values from a generic carrier (e.g. HTTP headers) and store them in a `LoggingContext`

Sources/Instrumentation/Instrument.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public protocol Injector: _SwiftInstrumentationSendable {
5050

5151
/// Conforming types are usually cross-cutting tools like tracers. They are agnostic of what specific `Carrier` is used
5252
/// to propagate metadata across boundaries, but instead just specify what values to use for which keys.
53-
public protocol InstrumentProtocol: _SwiftInstrumentationSendable {
53+
public protocol Instrument: _SwiftInstrumentationSendable {
5454
/// Extract values from a `Carrier` by using the given extractor and inject them into the given `Baggage`.
55-
/// It's quite common for `InstrumentProtocol`s to come up with new values if they weren't passed along in the given `Carrier`.
55+
/// It's quite common for `Instrument`s to come up with new values if they weren't passed along in the given `Carrier`.
5656
///
5757
/// - Parameters:
5858
/// - carrier: The `Carrier` that was used to propagate values across boundaries.

Sources/Instrumentation/InstrumentationSystem.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
import InstrumentationBaggage
1616

1717
/// `InstrumentationSystem` is a global facility where the default cross-cutting tool can be configured.
18-
/// It is set up just once in a given program to select the desired ``InstrumentProtocol`` implementation.
18+
/// It is set up just once in a given program to select the desired ``Instrument`` implementation.
1919
///
2020
/// # Bootstrap multiple Instruments
2121
/// If you need to use more that one cross-cutting tool you can do so by using ``MultiplexInstrument``.
2222
///
23-
/// # Access the InstrumentProtocol
24-
/// ``instrument``: Returns whatever you passed to ``bootstrap(_:)`` as an ``InstrumentProtocol``.
23+
/// # Access the Instrument
24+
/// ``instrument``: Returns whatever you passed to ``bootstrap(_:)`` as an ``Instrument``.
2525
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
2626
public enum InstrumentationSystem {
2727
private static let lock = ReadWriteLock()
28-
private static var _instrument: InstrumentProtocol = NoOpInstrument()
28+
private static var _instrument: Instrument = NoOpInstrument()
2929
private static var isInitialized = false
3030

31-
/// Globally select the desired ``InstrumentProtocol`` implementation.
31+
/// Globally select the desired ``Instrument`` implementation.
3232
///
33-
/// - Parameter instrument: The ``InstrumentProtocol`` you want to share globally within your system.
33+
/// - Parameter instrument: The ``Instrument`` you want to share globally within your system.
3434
/// - Warning: Do not call this method more than once. This will lead to a crash.
35-
public static func bootstrap(_ instrument: InstrumentProtocol) {
35+
public static func bootstrap(_ instrument: Instrument) {
3636
self.lock.withWriterLock {
3737
precondition(
3838
!self.isInitialized, """
@@ -48,24 +48,24 @@ public enum InstrumentationSystem {
4848
/// For testing scenarios one may want to set instruments multiple times, rather than the set-once semantics enforced by ``bootstrap(_:)``.
4949
///
5050
/// - Parameter instrument: the instrument to boostrap the system with, if `nil` the ``NoOpInstrument`` is bootstrapped.
51-
internal static func bootstrapInternal(_ instrument: InstrumentProtocol?) {
51+
internal static func bootstrapInternal(_ instrument: Instrument?) {
5252
self.lock.withWriterLock {
5353
self._instrument = instrument ?? NoOpInstrument()
5454
}
5555
}
5656

57-
/// Returns the globally configured ``InstrumentProtocol``.
57+
/// Returns the globally configured ``Instrument``.
5858
///
59-
/// Defaults to a no-op ``InstrumentProtocol`` if ``bootstrap(_:)`` wasn't called before.
60-
public static var instrument: InstrumentProtocol {
59+
/// Defaults to a no-op ``Instrument`` if ``bootstrap(_:)`` wasn't called before.
60+
public static var instrument: Instrument {
6161
self.lock.withReaderLock { self._instrument }
6262
}
6363
}
6464

6565
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
6666
extension InstrumentationSystem {
6767
/// :nodoc: INTERNAL API: Do Not Use
68-
public static func _findInstrument(where predicate: (InstrumentProtocol) -> Bool) -> InstrumentProtocol? {
68+
public static func _findInstrument(where predicate: (Instrument) -> Bool) -> Instrument? {
6969
self.lock.withReaderLock {
7070
if let multiplex = self._instrument as? MultiplexInstrument {
7171
return multiplex.firstInstrument(where: predicate)

Sources/Instrumentation/MultiplexInstrument.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@
1414

1515
import InstrumentationBaggage
1616

17-
/// A pseudo-``InstrumentProtocol`` that may be used to instrument using multiple other ``InstrumentProtocol``s across a
17+
/// A pseudo-``Instrument`` that may be used to instrument using multiple other ``Instrument``s across a
1818
/// common `Baggage`.
1919
public struct MultiplexInstrument {
20-
private var instruments: [InstrumentProtocol]
20+
private var instruments: [Instrument]
2121

2222
/// Create a ``MultiplexInstrument``.
2323
///
24-
/// - Parameter instruments: An array of ``InstrumentProtocol``s, each of which will be used to ``InstrumentProtocol/inject(_:into:using:)`` or ``InstrumentProtocol/extract(_:into:using:)``
24+
/// - Parameter instruments: An array of ``Instrument``s, each of which will be used to ``Instrument/inject(_:into:using:)`` or ``Instrument/extract(_:into:using:)``
2525
/// through the same `Baggage`.
26-
public init(_ instruments: [InstrumentProtocol]) {
26+
public init(_ instruments: [Instrument]) {
2727
self.instruments = instruments
2828
}
2929
}
3030

3131
extension MultiplexInstrument {
32-
func firstInstrument(where predicate: (InstrumentProtocol) -> Bool) -> InstrumentProtocol? {
32+
func firstInstrument(where predicate: (Instrument) -> Bool) -> Instrument? {
3333
self.instruments.first(where: predicate)
3434
}
3535
}
3636

37-
extension MultiplexInstrument: InstrumentProtocol {
37+
extension MultiplexInstrument: Instrument {
3838
public func inject<Carrier, Inject>(_ baggage: Baggage, into carrier: inout Carrier, using injector: Inject)
3939
where Inject: Injector, Carrier == Inject.Carrier
4040
{

Sources/Instrumentation/NoOpInstrument.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
import InstrumentationBaggage
1616

17-
/// A "no op" implementation of an ``InstrumentProtocol``.
18-
public struct NoOpInstrument: InstrumentProtocol {
17+
/// A "no op" implementation of an ``Instrument``.
18+
public struct NoOpInstrument: Instrument {
1919
public init() {}
2020

2121
public func inject<Carrier, Inject>(_ baggage: Baggage, into carrier: inout Carrier, using injector: Inject)

Sources/Tracing/Docs.docc/InDepthGuide.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ When instrumenting server applications there are typically three parties involve
88

99
1. **Application developers** create server-side applications
1010
2. **Library/Framework developers** provide building blocks to create these applications
11-
3. **InstrumentProtocol developers** provide tools to collect distributed metadata about your application
11+
3. **Instrument developers** provide tools to collect distributed metadata about your application
1212

1313
For applications to be instrumented correctly these three parts have to play along nicely.
1414

@@ -42,7 +42,7 @@ To your main target, add a dependency on the `Instrumentation library` and the i
4242

4343
Instead of providing each instrumented library with a specific instrument explicitly, you *bootstrap* the
4444
`InstrumentationSystem` which acts as a singleton that libraries/frameworks access when calling out to the configured
45-
`InstrumentProtocol`:
45+
`Instrument`:
4646

4747
```swift
4848
InstrumentationSystem.bootstrap(FancyInstrument())
@@ -63,7 +63,7 @@ This is because tracing systems may attempt to emit metrics about their status e
6363

6464
#### Bootstrapping multiple instruments using MultiplexInstrument
6565

66-
It is important to note that `InstrumentationSystem.bootstrap(_: InstrumentProtocol)` must only be called once. In case you
66+
It is important to note that `InstrumentationSystem.bootstrap(_: Instrument)` must only be called once. In case you
6767
want to bootstrap the system to use multiple instruments, you group them in a `MultiplexInstrument` first, which you
6868
then pass along to the `bootstrap` method like this:
6969

@@ -225,7 +225,7 @@ func get(url: String, context: LoggingContext) {
225225
}
226226
```
227227

228-
On the receiving side, an HTTP server should use the following `InstrumentProtocol` API to extract the HTTP headers of the given
228+
On the receiving side, an HTTP server should use the following `Instrument` API to extract the HTTP headers of the given
229229
`HTTPRequest` into:
230230

231231
```swift
@@ -280,12 +280,12 @@ func get(url: String, context: LoggingContext) {
280280
> In the above example we used the semantic `http.method` attribute that gets exposed via the
281281
`TracingOpenTelemetrySupport` library.
282282

283-
## InstrumentProtocol developers: Creating an instrument
283+
## Instrument developers: Creating an instrument
284284

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

288-
`InstrumentProtocol` has two requirements:
288+
`Instrument` has two requirements:
289289

290290
1. A method to inject values inside a `LoggingContext` into a generic carrier (e.g. HTTP headers)
291291
2. A method to extract values from a generic carrier (e.g. HTTP headers) and store them in a `LoggingContext`

Sources/Tracing/Docs.docc/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ To your main target, add a dependency on the `Tracing` library and the instrumen
6262
),
6363
```
6464

65-
Then (in an application, libraries should _never_ invoke `bootstrap`), you will want to bootstrap the specific tracer you want to use in your application. A ``Tracer`` is a type of `InstrumentProtocol` and can be offered used to globally bootstrap the tracing system, like this:
65+
Then (in an application, libraries should _never_ invoke `bootstrap`), you will want to bootstrap the specific tracer you want to use in your application. A ``Tracer`` is a type of `Instrument` and can be offered used to globally bootstrap the tracing system, like this:
6666

6767

6868
```swift

Sources/Tracing/Tracer.swift

+49-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public func startSpan<Clock: TracerClock>(
8181
///
8282
/// - Parameters:
8383
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
84-
/// - clock: The clock to use as time source for the start time of the ``Span``
8584
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
8685
/// - kind: The ``SpanKind`` of the new ``Span``.
8786
/// - function: The function name in which the span was started
@@ -128,9 +127,9 @@ public func startSpan(
128127
///
129128
/// - Parameters:
130129
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
131-
/// - clock: The clock to use as time source for the start time of the ``Span``
132130
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
133131
/// - kind: The ``SpanKind`` of the new ``Span``.
132+
/// - clock: The clock to use as time source for the start time of the ``Span``
134133
/// - function: The function name in which the span was started
135134
/// - fileID: The `fileID` where the span was started.
136135
/// - line: The file line where the span was started.
@@ -174,9 +173,9 @@ public func startSpan(
174173
///
175174
/// - Parameters:
176175
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
176+
/// - clock: The clock to use as time source for the start time of the ``Span``
177177
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
178178
/// - kind: The ``SpanKind`` of the new ``Span``.
179-
/// - clock: The clock to use as time source for the start time of the ``Span``
180179
/// - function: The function name in which the span was started
181180
/// - fileID: The `fileID` where the span was started.
182181
/// - line: The file line where the span was started.
@@ -207,6 +206,28 @@ public func withSpan<T, Clock: TracerClock>(
207206
}
208207
}
209208

209+
/// Start a new ``Span`` and automatically end when the `operation` completes,
210+
/// including recording the `error` in case the operation throws.
211+
///
212+
/// The current task-local `Baggage` is picked up and provided to the underlying tracer.
213+
/// It is also possible to pass a specific `baggage` explicitly, in which case attempting
214+
/// to pick up the task-local baggage is prevented. This can be useful when we know that
215+
/// we're about to start a top-level span, or if a span should be started from a different,
216+
/// stored away previously,
217+
///
218+
/// - Warning: You MUST NOT ``Span/end()`` the span explicitly, because at the end of the `withSpan`
219+
/// operation closure returning the span will be closed automatically.
220+
///
221+
/// - Parameters:
222+
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
223+
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
224+
/// - kind: The ``SpanKind`` of the new ``Span``.
225+
/// - function: The function name in which the span was started
226+
/// - fileID: The `fileID` where the span was started.
227+
/// - line: The file line where the span was started.
228+
/// - operation: The operation that this span should be measuring
229+
/// - Returns: the value returned by `operation`
230+
/// - Throws: the error the `operation` has thrown (if any)
210231
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
211232
public func withSpan<T>(
212233
_ operationName: String,
@@ -231,6 +252,30 @@ public func withSpan<T>(
231252
}
232253

233254
#if swift(>=5.7.0)
255+
256+
/// Start a new ``Span`` and automatically end when the `operation` completes,
257+
/// including recording the `error` in case the operation throws.
258+
///
259+
/// The current task-local `Baggage` is picked up and provided to the underlying tracer.
260+
/// It is also possible to pass a specific `baggage` explicitly, in which case attempting
261+
/// to pick up the task-local baggage is prevented. This can be useful when we know that
262+
/// we're about to start a top-level span, or if a span should be started from a different,
263+
/// stored away previously,
264+
///
265+
/// - Warning: You MUST NOT ``Span/end()`` the span explicitly, because at the end of the `withSpan`
266+
/// operation closure returning the span will be closed automatically.
267+
///
268+
/// - Parameters:
269+
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
270+
/// - clock: The clock to use as time source for the start time of the ``Span``
271+
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
272+
/// - kind: The ``SpanKind`` of the new ``Span``.
273+
/// - function: The function name in which the span was started
274+
/// - fileID: The `fileID` where the span was started.
275+
/// - line: The file line where the span was started.
276+
/// - operation: The operation that this span should be measuring
277+
/// - Returns: the value returned by `operation`
278+
/// - Throws: the error the `operation` has thrown (if any)
234279
public func withSpan<T>(
235280
_ operationName: String,
236281
baggage: @autoclosure () -> Baggage = .current ?? .topLevel,
@@ -271,9 +316,9 @@ public func withSpan<T>(
271316
///
272317
/// - Parameters:
273318
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
319+
/// - clock: The clock to use as time source for the start time of the ``Span``
274320
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
275321
/// - kind: The ``SpanKind`` of the new ``Span``.
276-
/// - clock: The clock to use as time source for the start time of the ``Span``
277322
/// - function: The function name in which the span was started
278323
/// - fileID: The `fileID` where the span was started.
279324
/// - line: The file line where the span was started.
@@ -320,7 +365,6 @@ public func withSpan<T, Clock: TracerClock>(
320365
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
321366
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
322367
/// - kind: The ``SpanKind`` of the new ``Span``.
323-
/// - clock: The clock to use as time source for the start time of the ``Span``
324368
/// - function: The function name in which the span was started
325369
/// - fileID: The `fileID` where the span was started.
326370
/// - line: The file line where the span was started.

Sources/Tracing/TracerProtocol+Legacy.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Dispatch
1717
@_exported import InstrumentationBaggage
1818

1919
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
20-
public protocol LegacyTracer: InstrumentProtocol {
20+
public protocol LegacyTracer: Instrument {
2121
/// Start a new span returning an existential ``Span`` reference.
2222
///
2323
/// - Warning: This method will be deprecated in favor of `Tracer/withSpan` as soon as this project is able to require Swift 5.7.

0 commit comments

Comments
 (0)