@@ -53,7 +53,10 @@ public protocol Tracer: Instrument {
53
53
}
54
54
55
55
extension Tracer {
56
- /// Start a new ``Span`` with the given `Baggage` starting at `DispatchWallTime.now()`.
56
+
57
+
58
+ #if swift(>=5.3.0)
59
+ /// Start a new ``Span`` with the given `Baggage` starting "now".
57
60
///
58
61
/// - Parameters:
59
62
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
@@ -80,12 +83,43 @@ extension Tracer {
80
83
line: line
81
84
)
82
85
}
86
+ #else
87
+ /// Start a new ``Span`` with the given `Baggage` starting "now".
88
+ ///
89
+ /// - Parameters:
90
+ /// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
91
+ /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
92
+ /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
93
+ /// - function: The function name in which the span was started
94
+ /// - file: The `file` where the span was started.
95
+ /// - line: The file line where the span was started.
96
+ public func startSpan(
97
+ _ operationName: String ,
98
+ baggage: Baggage ,
99
+ ofKind kind: SpanKind = . internal,
100
+ function: String = #function,
101
+ file: String = #file,
102
+ line: UInt = #line
103
+ ) -> Span {
104
+ self . startSpan (
105
+ operationName,
106
+ baggage: baggage,
107
+ ofKind: kind,
108
+ at: . now( ) ,
109
+ function: function,
110
+ file: file,
111
+ line: line
112
+ )
113
+ }
114
+ #endif
115
+
83
116
}
84
117
85
118
// ==== ----------------------------------------------------------------------------------------------------------------
86
119
// MARK: Starting spans: `withSpan`
87
120
88
121
extension Tracer {
122
+ #if swift(>=5.3.0)
89
123
/// Execute a specific task within a newly created ``Span``.
90
124
///
91
125
/// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
@@ -126,6 +160,48 @@ extension Tracer {
126
160
throw error // rethrow
127
161
}
128
162
}
163
+ #else
164
+ /// Execute a specific task within a newly created ``Span``.
165
+ ///
166
+ /// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
167
+ ///
168
+ /// - Parameters:
169
+ /// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
170
+ /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
171
+ /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
172
+ /// - operation: operation to wrap in a span start/end and execute immediately
173
+ /// - function: The function name in which the span was started
174
+ /// - file: The `#file` where the span was started.
175
+ /// - line: The file line where the span was started.
176
+ /// - Returns: the value returned by `operation`
177
+ /// - Throws: the error the `operation` has thrown (if any)
178
+ public func withSpan< T> (
179
+ _ operationName: String ,
180
+ baggage: Baggage ,
181
+ ofKind kind: SpanKind = . internal,
182
+ function: String = #function,
183
+ file: String = #file,
184
+ line: UInt = #line,
185
+ _ operation: ( Span ) throws -> T
186
+ ) rethrows -> T {
187
+ let span = self . startSpan (
188
+ operationName,
189
+ baggage: baggage,
190
+ ofKind: kind,
191
+ at: . now( ) ,
192
+ function: function,
193
+ file: file,
194
+ line: line
195
+ )
196
+ defer { span. end ( ) }
197
+ do {
198
+ return try operation ( span)
199
+ } catch {
200
+ span. recordError ( error)
201
+ throw error // rethrow
202
+ }
203
+ }
204
+ #endif
129
205
}
130
206
131
207
// ==== ----------------------------------------------------------------------------------------------------------------
@@ -202,7 +278,7 @@ extension Tracer {
202
278
)
203
279
defer { span. end ( ) }
204
280
do {
205
- return try await Baggage . $current . withValue ( span. baggage) {
281
+ return try await Baggage . withValue ( span. baggage) {
206
282
try await operation ( span)
207
283
}
208
284
} catch {
@@ -239,7 +315,7 @@ extension Tracer {
239
315
let span = self . startSpan ( operationName, baggage: baggage, ofKind: kind, function: function, file: fileID, line: line)
240
316
defer { span. end ( ) }
241
317
do {
242
- return try await Baggage . $current . withValue ( span. baggage) {
318
+ return try await Baggage . withValue ( span. baggage) {
243
319
try await operation ( span)
244
320
}
245
321
} catch {
0 commit comments