forked from apple/swift-distributed-tracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTracerProtocol.swift
325 lines (315 loc) · 14.9 KB
/
TracerProtocol.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Distributed Tracing open source project
//
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
// authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
@_exported import Instrumentation
@_exported import ServiceContextModule
// ==== -----------------------------------------------------------------------
// MARK: Tracer protocol
/// A tracer capable of creating new trace spans.
///
/// A tracer is a special kind of instrument with the added ability to start a ``Span``.
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
public protocol Tracer: LegacyTracer {
/// The concrete type of span this tracer will be producing/
associatedtype Span: Tracing.Span
/// Start a new ``Span`` with the given `ServiceContext`.
///
/// The current task-local `ServiceContext` is picked up and provided to the underlying tracer.
/// It is also possible to pass a specific `context` explicitly, in which case attempting
/// to pick up the task-local context is prevented. This can be useful when we know that
/// we're about to start a top-level span, or if a span should be started from a different,
/// stored away previously,
///
/// - Note: Prefer ``withSpan(_:context:ofKind:at:function:file:line:_:)-8gw3v`` to start
/// a span as it automatically takes care of ending the span, and recording errors when thrown.
/// Use `startSpan` iff you need to pass the span manually to a different
/// location in your source code to end it.
///
/// - Warning: You must `end()` the span when it the measured operation has completed explicitly,
/// otherwise the span object will potentially never be released nor reported.
///
/// - Parameters:
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - instant: the time instant at which the span started
/// - function: The function name in which the span was started
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
func startSpan<Instant: TracerInstant>(
_ operationName: String,
context: @autoclosure () -> ServiceContext,
ofKind kind: SpanKind,
at instant: @autoclosure () -> Instant,
function: String,
file fileID: String,
line: UInt
) -> Self.Span
}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
extension Tracer {
/// Start a new ``Span`` with the given `ServiceContext`.
///
/// The current task-local `ServiceContext` is picked up and provided to the underlying tracer.
/// It is also possible to pass a specific `context` explicitly, in which case attempting
/// to pick up the task-local context is prevented. This can be useful when we know that
/// we're about to start a top-level span, or if a span should be started from a different,
/// stored away previously,
///
/// - Note: Prefer ``withSpan(_:context:ofKind:at:function:file:line:_:)-8gw3v`` to start
/// a span as it automatically takes care of ending the span, and recording errors when thrown.
/// Use `startSpan` iff you need to pass the span manually to a different
/// location in your source code to end it.
///
/// - Warning: You must `end()` the span when it the measured operation has completed explicitly,
/// otherwise the span object will potentially never be released nor reported.
///
/// - Parameters:
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - instant: the time instant at which the span started
/// - function: The function name in which the span was started
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
public func startSpan(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line
) -> Self.Span {
self.startSpan(
operationName,
context: context(),
ofKind: kind,
at: instant(),
function: function,
file: fileID,
line: line
)
}
}
// ==== ----------------------------------------------------------------------------------------------------------------
// MARK: Starting spans: `withSpan`
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension Tracer {
/// Start a new ``Span`` and automatically end when the `operation` completes,
/// including recording the `error` in case the operation throws.
///
/// The current task-local `ServiceContext` is picked up and provided to the underlying tracer.
/// It is also possible to pass a specific `context` explicitly, in which case attempting
/// to pick up the task-local context is prevented. This can be useful when we know that
/// we're about to start a top-level span, or if a span should be started from a different,
/// stored away previously,
///
/// - Warning: You MUST NOT ``Span/end()`` the span explicitly, because at the end of the `withSpan`
/// operation closure returning the span will be closed automatically.
///
/// - Parameters:
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - instant: the time instant at which the span started
/// - function: The function name in which the span was started
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
public func withSpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (Self.Span) throws -> T
) rethrows -> T {
let span = self.startSpan(
operationName,
context: context(),
ofKind: kind,
at: instant(),
function: function,
file: fileID,
line: line
)
defer { span.end() }
do {
return try ServiceContext.$current.withValue(span.context) {
try operation(span)
}
} catch {
span.recordError(error)
throw error // rethrow
}
}
/// Start a new ``Span`` and automatically end when the `operation` completes,
/// including recording the `error` in case the operation throws.
///
/// The current task-local `ServiceContext` is picked up and provided to the underlying tracer.
/// It is also possible to pass a specific `context` explicitly, in which case attempting
/// to pick up the task-local context is prevented. This can be useful when we know that
/// we're about to start a top-level span, or if a span should be started from a different,
/// stored away previously,
///
/// - Warning: You MUST NOT ``Span/end()`` the span explicitly, because at the end of the `withSpan`
/// operation closure returning the span will be closed automatically.
///
/// - Parameters:
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - instant: the time instant at which the span started
/// - function: The function name in which the span was started
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
public func withSpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (Self.Span) throws -> T
) rethrows -> T {
let span = self.startSpan(
operationName,
context: context(),
ofKind: kind,
at: DefaultTracerClock.now,
function: function,
file: fileID,
line: line
)
defer { span.end() }
do {
return try ServiceContext.$current.withValue(span.context) {
try operation(span)
}
} catch {
span.recordError(error)
throw error // rethrow
}
}
/// Start a new ``Span`` and automatically end when the `operation` completes,
/// including recording the `error` in case the operation throws.
///
/// The current task-local `ServiceContext` is picked up and provided to the underlying tracer.
/// It is also possible to pass a specific `context` explicitly, in which case attempting
/// to pick up the task-local context is prevented. This can be useful when we know that
/// we're about to start a top-level span, or if a span should be started from a different,
/// stored away previously,
///
/// - Warning: You MUST NOT ``Span/end()`` the span explicitly, because at the end of the `withSpan`
/// operation closure returning the span will be closed automatically.
///
/// - Parameters:
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - instant: the time instant at which the span started
/// - function: The function name in which the span was started
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
public func withSpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
@_inheritActorContext @_implicitSelfCapture _ operation: (Self.Span) async throws -> T
) async rethrows -> T {
let span = self.startSpan(
operationName,
context: context(),
ofKind: kind,
at: DefaultTracerClock.now,
function: function,
file: fileID,
line: line
)
defer { span.end() }
do {
return try await ServiceContext.$current.withValue(span.context) {
try await operation(span)
}
} catch {
span.recordError(error)
throw error // rethrow
}
}
/// Start a new ``Span`` and automatically end when the `operation` completes,
/// including recording the `error` in case the operation throws.
///
/// The current task-local `ServiceContext` is picked up and provided to the underlying tracer.
/// It is also possible to pass a specific `context` explicitly, in which case attempting
/// to pick up the task-local context is prevented. This can be useful when we know that
/// we're about to start a top-level span, or if a span should be started from a different,
/// stored away previously,
///
/// - Warning: You MUST NOT ``Span/end()`` the span explicitly, because at the end of the `withSpan`
/// operation closure returning the span will be closed automatically.
///
/// - Parameters:
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - instant: the time instant at which the span started
/// - function: The function name in which the span was started
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
public func withSpan<T>(
_ operationName: String,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
@_inheritActorContext @_implicitSelfCapture _ operation: (Self.Span) async throws -> T
) async rethrows -> T {
let span = self.startSpan(
operationName,
context: context(),
ofKind: kind,
at: instant(),
function: function,
file: fileID,
line: line
)
defer { span.end() }
do {
return try await ServiceContext.$current.withValue(span.context) {
try await operation(span)
}
} catch {
span.recordError(error)
throw error // rethrow
}
}
}