@@ -40,34 +40,85 @@ public protocol TracerStartSpanOps {
40
40
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
41
41
/// - kind: The ``SpanKind`` of the new ``Span``.
42
42
/// - time: The `DispatchTime` at which to start the new ``Span``.
43
+ /// - function: The function name in which the span was started
44
+ /// - fileID: The `fileID` where the span was started.
45
+ /// - line: The file line where the span was started.
43
46
func startSpan(
44
47
_ operationName: String ,
45
48
baggage: Baggage ,
46
49
ofKind kind: SpanKind ,
47
- at time: DispatchWallTime
50
+ at time: DispatchWallTime ,
51
+ function: String ,
52
+ file fileID: String ,
53
+ line: UInt
48
54
) -> Span
49
55
}
50
56
51
- extension TracerStartSpanOps {
52
- /// Start a new ``Span`` with the given `Baggage` starting at `DispatchWallTime.now()`.
57
+ extension Tracer {
58
+ #if swift(>=5.3.0)
59
+ /// Start a new ``Span`` with the given `Baggage` starting "now".
60
+ ///
61
+ /// - Parameters:
62
+ /// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
63
+ /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
64
+ /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
65
+ /// - function: The function name in which the span was started.
66
+ /// - fileID: The `fileID` where the span was started.
67
+ /// - line: The file line where the span was started.
68
+ public func startSpan(
69
+ _ operationName: String ,
70
+ baggage: Baggage ,
71
+ ofKind kind: SpanKind = . internal,
72
+ function: String = #function,
73
+ file fileID: String = #fileID,
74
+ line: UInt = #line
75
+ ) -> Span {
76
+ self . startSpan (
77
+ operationName,
78
+ baggage: baggage,
79
+ ofKind: kind,
80
+ at: . now( ) ,
81
+ function: function,
82
+ file: fileID,
83
+ line: line
84
+ )
85
+ }
86
+ #else
87
+ /// Start a new ``Span`` with the given `Baggage` starting "now".
53
88
///
54
89
/// - Parameters:
55
90
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
56
91
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
57
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.
58
96
public func startSpan(
59
97
_ operationName: String ,
60
98
baggage: Baggage ,
61
- ofKind kind: SpanKind = . internal
99
+ ofKind kind: SpanKind = . internal,
100
+ function: String = #function,
101
+ file: String = #file,
102
+ line: UInt = #line
62
103
) -> Span {
63
- self . startSpan ( operationName, baggage: baggage, ofKind: kind, at: . now( ) )
104
+ self . startSpan (
105
+ operationName,
106
+ baggage: baggage,
107
+ ofKind: kind,
108
+ at: . now( ) ,
109
+ function: function,
110
+ file: file,
111
+ line: line
112
+ )
64
113
}
114
+ #endif
65
115
}
66
116
67
117
// ==== ----------------------------------------------------------------------------------------------------------------
68
118
// MARK: Starting spans: `withSpan`
69
119
70
120
extension TracerStartSpanOps {
121
+ #if swift(>=5.3.0)
71
122
/// Execute a specific task within a newly created ``Span``.
72
123
///
73
124
/// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
@@ -77,15 +128,29 @@ extension TracerStartSpanOps {
77
128
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
78
129
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
79
130
/// - operation: operation to wrap in a span start/end and execute immediately
131
+ /// - function: The function name in which the span was started.
132
+ /// - fileID: The `fileID` where the span was started.
133
+ /// - line: The file line where the span was started.
80
134
/// - Returns: the value returned by `operation`
81
135
/// - Throws: the error the `operation` has thrown (if any)
82
136
public func withSpan< T> (
83
137
_ operationName: String ,
84
138
baggage: Baggage ,
85
139
ofKind kind: SpanKind = . internal,
140
+ function: String = #function,
141
+ file fileID: String = #fileID,
142
+ line: UInt = #line,
86
143
_ operation: ( Span ) throws -> T
87
144
) rethrows -> T {
88
- let span = self . startSpan ( operationName, baggage: baggage, ofKind: kind)
145
+ let span = self . startSpan (
146
+ operationName,
147
+ baggage: baggage,
148
+ ofKind: kind,
149
+ at: . now( ) ,
150
+ function: function,
151
+ file: fileID,
152
+ line: line
153
+ )
89
154
defer { span. end ( ) }
90
155
do {
91
156
return try operation ( span)
@@ -94,7 +159,7 @@ extension TracerStartSpanOps {
94
159
throw error // rethrow
95
160
}
96
161
}
97
-
162
+ #else
98
163
/// Execute a specific task within a newly created ``Span``.
99
164
///
100
165
/// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
@@ -104,23 +169,38 @@ extension TracerStartSpanOps {
104
169
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
105
170
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
106
171
/// - operation: operation to wrap in a span start/end and execute immediately
172
+ /// - function: The function name in which the span was started.
173
+ /// - file: The `#file` where the span was started.
174
+ /// - line: The file line where the span was started.
107
175
/// - Returns: the value returned by `operation`
108
176
/// - Throws: the error the `operation` has thrown (if any)
109
177
public func withSpan< T> (
110
178
_ operationName: String ,
111
179
baggage: Baggage ,
112
180
ofKind kind: SpanKind = . internal,
113
- _ operation: ( ) throws -> T
181
+ function: String = #function,
182
+ file: String = #file,
183
+ line: UInt = #line,
184
+ _ operation: ( Span ) throws -> T
114
185
) rethrows -> T {
115
- let span = self . startSpan ( operationName, baggage: baggage, ofKind: kind)
186
+ let span = self . startSpan (
187
+ operationName,
188
+ baggage: baggage,
189
+ ofKind: kind,
190
+ at: . now( ) ,
191
+ function: function,
192
+ file: file,
193
+ line: line
194
+ )
116
195
defer { span. end ( ) }
117
196
do {
118
- return try operation ( )
197
+ return try operation ( span )
119
198
} catch {
120
199
span. recordError ( error)
121
200
throw error // rethrow
122
201
}
123
202
}
203
+ #endif
124
204
}
125
205
126
206
// ==== ----------------------------------------------------------------------------------------------------------------
@@ -138,14 +218,27 @@ extension TracerStartSpanOps {
138
218
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
139
219
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
140
220
/// - operation: operation to wrap in a span start/end and execute immediately
221
+ /// - function: The function name in which the span was started.
222
+ /// - fileID: The `fileID` where the span was started.
223
+ /// - line: The file line where the span was started.
141
224
/// - Returns: the value returned by `operation`
142
225
/// - Throws: the error the `operation` has thrown (if any)
143
226
public func withSpan< T> (
144
227
_ operationName: String ,
145
228
ofKind kind: SpanKind = . internal,
229
+ function: String = #function,
230
+ file fileID: String = #fileID,
231
+ line: UInt = #line,
146
232
_ operation: ( Span ) throws -> T
147
233
) rethrows -> T {
148
- try self . withSpan ( operationName, baggage: . current ?? . topLevel, ofKind: kind) { span in
234
+ try self . withSpan (
235
+ operationName,
236
+ baggage: . current ?? . topLevel,
237
+ ofKind: kind,
238
+ function: function,
239
+ file: fileID,
240
+ line: line
241
+ ) { span in
149
242
try Baggage . $current. withValue ( span. baggage) {
150
243
try operation ( span)
151
244
}
@@ -187,14 +280,27 @@ extension TracerStartSpanOps {
187
280
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
188
281
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
189
282
/// - operation: operation to wrap in a span start/end and execute immediately
283
+ /// - function: The function name in which the span was started.
284
+ /// - fileID: The `fileID` where the span was started.
285
+ /// - line: The file line where the span was started.
190
286
/// - Returns: the value returned by `operation`
191
287
/// - Throws: the error the `operation` has thrown (if any)
192
288
public func withSpan< T> (
193
289
_ operationName: String ,
194
290
ofKind kind: SpanKind = . internal,
291
+ function: String = #function,
292
+ file fileID: String = #fileID,
293
+ line: UInt = #line,
195
294
_ operation: ( Span ) async throws -> T
196
295
) async rethrows -> T {
197
- let span = self . startSpan ( operationName, baggage: . current ?? . topLevel, ofKind: kind)
296
+ let span = self . startSpan (
297
+ operationName,
298
+ baggage: . current ?? . topLevel,
299
+ ofKind: kind,
300
+ function: function,
301
+ file: fileID,
302
+ line: line
303
+ )
198
304
defer { span. end ( ) }
199
305
do {
200
306
return try await Baggage . $current. withValue ( span. baggage) {
@@ -217,15 +323,21 @@ extension TracerStartSpanOps {
217
323
// task local and modified before passing into this function. The baggage will be made the current task-local baggage for the duration of the `operation`.
218
324
/// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`.
219
325
/// - operation: operation to wrap in a span start/end and execute immediately
326
+ /// - function: The function name in which the span was started.
327
+ /// - fileID: The `fileID` where the span was started.
328
+ /// - line: The file line where the span was started.
220
329
/// - Returns: the value returned by `operation`
221
330
/// - Throws: the error the `operation` has thrown (if any)
222
331
public func withSpan< T> (
223
332
_ operationName: String ,
224
333
baggage: Baggage ,
225
334
ofKind kind: SpanKind = . internal,
335
+ function: String = #function,
336
+ file fileID: String = #fileID,
337
+ line: UInt = #line,
226
338
_ operation: ( Span ) async throws -> T
227
339
) async rethrows -> T {
228
- let span = self . startSpan ( operationName, baggage: baggage, ofKind: kind)
340
+ let span = self . startSpan ( operationName, baggage: baggage, ofKind: kind, function : function , file : fileID , line : line )
229
341
defer { span. end ( ) }
230
342
do {
231
343
return try await Baggage . $current. withValue ( span. baggage) {
0 commit comments