Skip to content

Commit 708ac64

Browse files
committed
+tracer,instrument provide static helper functions
1 parent 1785645 commit 708ac64

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

Sources/Instrumentation/Instrument.swift

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,40 @@
1515
import InstrumentationBaggage
1616

1717
public enum Instrument {
18+
19+
/// Convenience to access the globally bootstrapped instrument on ``InstrumentationSystem``.
20+
///
21+
/// Equivalent to ``InstrumentationSystem/instrument``.
1822
static var current: InstrumentProtocol {
1923
InstrumentationSystem.instrument
2024
}
25+
26+
/// Obtain the ``current`` instrument which was bootstrapped on the global ``InstrumentationSystem``,
27+
/// and invoke ``InstrumentProtocol/extract(_:into:using:)`` on it.
28+
///
29+
/// Extract values from a `Carrier` by using the given extractor and inject them into the given `Baggage`.
30+
/// It's quite common for `Instrument`s to come up with new values if they weren't passed along in the given `Carrier`.
31+
///
32+
/// - Parameters:
33+
/// - carrier: The `Carrier` that was used to propagate values across boundaries.
34+
/// - baggage: The `Baggage` into which these values should be injected.
35+
/// - extractor: The ``Extractor`` that extracts values from the given `Carrier`.
36+
public static func extract<Carrier, Extract>(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract) where Extract: Extractor, Extract.Carrier == Carrier {
37+
Instrument.current.extract(carrier, into: &baggage, using: extractor)
38+
}
39+
40+
/// Obtain the ``current`` instrument which was bootstrapped on the global ``InstrumentationSystem``,
41+
/// and invoke ``InstrumentProtocol/extract(_:into:using:)`` on it.
42+
///
43+
/// Take values from a `Baggage` and inject them into the given `Carrier` using the given ``Injector``.
44+
///
45+
/// - Parameters:
46+
/// - baggage: The `Baggage` from which relevant information will be extracted.
47+
/// - carrier: The `Carrier` into which this information will be injected.
48+
/// - injector: The ``Injector`` used to inject extracted `Baggage` into the given `Carrier`.
49+
public static func inject<Carrier, Inject>(_ baggage: Baggage, into carrier: inout Carrier, using injector: Inject) where Inject: Injector, Inject.Carrier == Carrier {
50+
Instrument.current.inject(baggage, into: &carrier, using: injector)
51+
}
2152
}
2253

2354
/// Conforming types are usually cross-cutting tools like tracers. They are agnostic of what specific `Carrier` is used
@@ -33,7 +64,7 @@ public protocol InstrumentProtocol {
3364
func extract<Carrier, Extract>(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract)
3465
where Extract: Extractor, Extract.Carrier == Carrier
3566

36-
/// Extract values from a `Baggage` and inject them into the given `Carrier` using the given ``Injector``.
67+
/// Take values from a `Baggage` and inject them into the given `Carrier` using the given ``Injector``.
3768
///
3869
/// - Parameters:
3970
/// - baggage: The `Baggage` from which relevant information will be extracted.

Sources/Tracing/Span.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,28 @@ import Dispatch
1919
/// with it. A `Span` can be created from a `Baggage` which contains span information, in which case this span should
2020
/// be considered as "child" of the previous span.
2121
///
22-
/// Creating a `Span` is delegated to a ``Tracer`` and end users should never create them directly.
22+
/// Creating a `Span` is delegated to an implementation of ``TracerProtocol`` and end-users should never instantiate spans directly.
23+
/// Most commonly, a span is started using the `withSpan` API of a tracer, like this:
24+
///
25+
/// ```
26+
/// tracer.withSpan("working-on-\(thing)") { span in
27+
/// // ...
28+
/// }
29+
/// ```
30+
///
31+
/// Once started, a span contains its start time and additional metadata necessary for the tracing backend to handle e.g.
32+
/// further child spans.
33+
///
34+
/// ### Child Spans
35+
///
36+
///
37+
/// ## Manual lifetime management
38+
/// In some situations it may not be possible
39+
///
40+
/// > Warning: Generally spans should be started using the ``TracerProtocol/withSpan(_:ofKind:_:)-11n3y`` method,
41+
/// > however it may sometimes be necessary to use the alternate ``TracerProtocol/startSpan(_:baggage:ofKind:)`` API.
42+
/// > If the `startSpan` API is used to create a span, it must be explicitly ended using ``end()`` on *every* code-path
43+
/// > leading to the logical end of the span. Omitting to end a span or ending it twice is a programmer error.
2344
///
2445
/// - SeeAlso: For more details refer to the [OpenTelemetry Specification: Span](https://github.com/open-telemetry/opentelemetry-specification/blob/v0.7.0/specification/trace/api.md#span) which this type is compatible with.
2546
public protocol Span: AnyObject {
@@ -84,6 +105,7 @@ extension Span {
84105
}
85106

86107
/// Adds a ``SpanLink`` between this `Span` and the given `Span`.
108+
///
87109
/// - Parameter other: The `Span` to link to.
88110
/// - Parameter attributes: The ``SpanAttributes`` describing this link. Defaults to no attributes.
89111
public func addLink(_ other: Span, attributes: SpanAttributes = [:]) {

0 commit comments

Comments
 (0)