@@ -30,11 +30,17 @@ public protocol Tracer: Instrument {
30
30
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
31
31
/// - kind: The ``SpanKind`` of the new ``Span``.
32
32
/// - time: The `DispatchTime` at which to start the new ``Span``.
33
+ /// - function: The function name in which the span was started
34
+ /// - fileID: The `fileID` where the span was started.
35
+ /// - line: The file line where the span was started.
33
36
func startSpan(
34
37
_ operationName: String ,
35
38
baggage: Baggage ,
36
39
ofKind kind: SpanKind ,
37
- at time: DispatchWallTime
40
+ at time: DispatchWallTime ,
41
+ function: String ,
42
+ file fileID: String ,
43
+ line: UInt
38
44
) -> Span
39
45
40
46
/// Export all ended spans to the configured backend that have not yet been exported.
@@ -47,25 +53,111 @@ public protocol Tracer: Instrument {
47
53
}
48
54
49
55
extension Tracer {
50
- /// Start a new ``Span`` with the given `Baggage` starting at `DispatchWallTime.now()`.
56
+ #if swift(>=5.3.0)
57
+ /// Start a new ``Span`` with the given `Baggage` starting "now".
51
58
///
52
59
/// - Parameters:
53
60
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
54
61
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
55
62
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
63
+ /// - function: The function name in which the span was started.
64
+ /// - fileID: The `fileID` where the span was started.
65
+ /// - line: The file line where the span was started.
56
66
public func startSpan(
57
67
_ operationName: String ,
58
68
baggage: Baggage ,
59
- ofKind kind: SpanKind = . internal
69
+ ofKind kind: SpanKind = . internal,
70
+ function: String = #function,
71
+ file fileID: String = #fileID,
72
+ line: UInt = #line
60
73
) -> Span {
61
- self . startSpan ( operationName, baggage: baggage, ofKind: kind, at: . now( ) )
74
+ self . startSpan (
75
+ operationName,
76
+ baggage: baggage,
77
+ ofKind: kind,
78
+ at: . now( ) ,
79
+ function: function,
80
+ file: fileID,
81
+ line: line
82
+ )
62
83
}
84
+ #else
85
+ /// Start a new ``Span`` with the given `Baggage` starting "now".
86
+ ///
87
+ /// - Parameters:
88
+ /// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
89
+ /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
90
+ /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
91
+ /// - function: The function name in which the span was started.
92
+ /// - file: The `file` where the span was started.
93
+ /// - line: The file line where the span was started.
94
+ public func startSpan(
95
+ _ operationName: String ,
96
+ baggage: Baggage ,
97
+ ofKind kind: SpanKind = . internal,
98
+ function: String = #function,
99
+ file: String = #file,
100
+ line: UInt = #line
101
+ ) -> Span {
102
+ self . startSpan (
103
+ operationName,
104
+ baggage: baggage,
105
+ ofKind: kind,
106
+ at: . now( ) ,
107
+ function: function,
108
+ file: file,
109
+ line: line
110
+ )
111
+ }
112
+ #endif
63
113
}
64
114
65
115
// ==== ----------------------------------------------------------------------------------------------------------------
66
116
// MARK: Starting spans: `withSpan`
67
117
68
118
extension Tracer {
119
+ #if swift(>=5.3.0)
120
+ /// Execute a specific task within a newly created ``Span``.
121
+ ///
122
+ /// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
123
+ ///
124
+ /// - Parameters:
125
+ /// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
126
+ /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
127
+ /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
128
+ /// - operation: operation to wrap in a span start/end and execute immediately
129
+ /// - function: The function name in which the span was started.
130
+ /// - fileID: The `fileID` where the span was started.
131
+ /// - line: The file line where the span was started.
132
+ /// - Returns: the value returned by `operation`
133
+ /// - Throws: the error the `operation` has thrown (if any)
134
+ public func withSpan< T> (
135
+ _ operationName: String ,
136
+ baggage: Baggage ,
137
+ ofKind kind: SpanKind = . internal,
138
+ function: String = #function,
139
+ file fileID: String = #fileID,
140
+ line: UInt = #line,
141
+ _ operation: ( Span ) throws -> T
142
+ ) rethrows -> T {
143
+ let span = self . startSpan (
144
+ operationName,
145
+ baggage: baggage,
146
+ ofKind: kind,
147
+ at: . now( ) ,
148
+ function: function,
149
+ file: fileID,
150
+ line: line
151
+ )
152
+ defer { span. end ( ) }
153
+ do {
154
+ return try operation ( span)
155
+ } catch {
156
+ span. recordError ( error)
157
+ throw error // rethrow
158
+ }
159
+ }
160
+ #else
69
161
/// Execute a specific task within a newly created ``Span``.
70
162
///
71
163
/// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
@@ -75,15 +167,29 @@ extension Tracer {
75
167
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
76
168
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
77
169
/// - operation: operation to wrap in a span start/end and execute immediately
170
+ /// - function: The function name in which the span was started.
171
+ /// - file: The `#file` where the span was started.
172
+ /// - line: The file line where the span was started.
78
173
/// - Returns: the value returned by `operation`
79
174
/// - Throws: the error the `operation` has thrown (if any)
80
175
public func withSpan< T> (
81
176
_ operationName: String ,
82
177
baggage: Baggage ,
83
178
ofKind kind: SpanKind = . internal,
179
+ function: String = #function,
180
+ file: String = #file,
181
+ line: UInt = #line,
84
182
_ operation: ( Span ) throws -> T
85
183
) rethrows -> T {
86
- let span = self . startSpan ( operationName, baggage: baggage, ofKind: kind)
184
+ let span = self . startSpan (
185
+ operationName,
186
+ baggage: baggage,
187
+ ofKind: kind,
188
+ at: . now( ) ,
189
+ function: function,
190
+ file: file,
191
+ line: line
192
+ )
87
193
defer { span. end ( ) }
88
194
do {
89
195
return try operation ( span)
@@ -92,6 +198,7 @@ extension Tracer {
92
198
throw error // rethrow
93
199
}
94
200
}
201
+ #endif
95
202
}
96
203
97
204
// ==== ----------------------------------------------------------------------------------------------------------------
@@ -109,14 +216,27 @@ extension Tracer {
109
216
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
110
217
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
111
218
/// - operation: operation to wrap in a span start/end and execute immediately
219
+ /// - function: The function name in which the span was started.
220
+ /// - fileID: The `fileID` where the span was started.
221
+ /// - line: The file line where the span was started.
112
222
/// - Returns: the value returned by `operation`
113
223
/// - Throws: the error the `operation` has thrown (if any)
114
224
public func withSpan< T> (
115
225
_ operationName: String ,
116
226
ofKind kind: SpanKind = . internal,
227
+ function: String = #function,
228
+ file fileID: String = #fileID,
229
+ line: UInt = #line,
117
230
_ operation: ( Span ) throws -> T
118
231
) rethrows -> T {
119
- try self . withSpan ( operationName, baggage: . current ?? . topLevel, ofKind: kind) { span in
232
+ try self . withSpan (
233
+ operationName,
234
+ baggage: . current ?? . topLevel,
235
+ ofKind: kind,
236
+ function: function,
237
+ file: fileID,
238
+ line: line
239
+ ) { span in
120
240
try Baggage . $current. withValue ( span. baggage) {
121
241
try operation ( span)
122
242
}
@@ -132,14 +252,27 @@ extension Tracer {
132
252
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
133
253
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
134
254
/// - operation: operation to wrap in a span start/end and execute immediately
255
+ /// - function: The function name in which the span was started.
256
+ /// - fileID: The `fileID` where the span was started.
257
+ /// - line: The file line where the span was started.
135
258
/// - Returns: the value returned by `operation`
136
259
/// - Throws: the error the `operation` has thrown (if any)
137
260
public func withSpan< T> (
138
261
_ operationName: String ,
139
262
ofKind kind: SpanKind = . internal,
263
+ function: String = #function,
264
+ file fileID: String = #fileID,
265
+ line: UInt = #line,
140
266
_ operation: ( Span ) async throws -> T
141
267
) async rethrows -> T {
142
- let span = self . startSpan ( operationName, baggage: . current ?? . topLevel, ofKind: kind)
268
+ let span = self . startSpan (
269
+ operationName,
270
+ baggage: . current ?? . topLevel,
271
+ ofKind: kind,
272
+ function: function,
273
+ file: fileID,
274
+ line: line
275
+ )
143
276
defer { span. end ( ) }
144
277
do {
145
278
return try await Baggage . $current. withValue ( span. baggage) {
@@ -162,15 +295,21 @@ extension Tracer {
162
295
// 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`.
163
296
/// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`.
164
297
/// - operation: operation to wrap in a span start/end and execute immediately
298
+ /// - function: The function name in which the span was started.
299
+ /// - fileID: The `fileID` where the span was started.
300
+ /// - line: The file line where the span was started.
165
301
/// - Returns: the value returned by `operation`
166
302
/// - Throws: the error the `operation` has thrown (if any)
167
303
public func withSpan< T> (
168
304
_ operationName: String ,
169
305
baggage: Baggage ,
170
306
ofKind kind: SpanKind = . internal,
307
+ function: String = #function,
308
+ file fileID: String = #fileID,
309
+ line: UInt = #line,
171
310
_ operation: ( Span ) async throws -> T
172
311
) async rethrows -> T {
173
- let span = self . startSpan ( operationName, baggage: baggage, ofKind: kind)
312
+ let span = self . startSpan ( operationName, baggage: baggage, ofKind: kind, function : function , file : fileID , line : line )
174
313
defer { span. end ( ) }
175
314
do {
176
315
return try await Baggage . $current. withValue ( span. baggage) {
0 commit comments