@@ -42,7 +42,7 @@ public protocol Tracer: Instrument {
42
42
}
43
43
44
44
extension Tracer {
45
- /// Start a new `Span` with the given `Baggage` starting at `Timestamp .now()`.
45
+ /// Start a new `Span` with the given `Baggage` starting at `DispatchWallTime .now()`.
46
46
///
47
47
/// - Parameters:
48
48
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
@@ -55,15 +55,17 @@ extension Tracer {
55
55
) -> Span {
56
56
return self . startSpan ( operationName, baggage: baggage, ofKind: kind, at: . now( ) )
57
57
}
58
+ }
58
59
59
- // ==== --------------------------------------------------------------------
60
- // MARK: LoggingContext accepting
60
+ // ==== -------------------------------------------- --------------------------------------------------------------------
61
+ // MARK: Span creation: with `LoggingContext`
61
62
62
- /// Start a new `Span` with the given `Baggage` starting at `Timestamp.now()`.
63
+ extension Tracer {
64
+ /// Start a new `Span` with the given `Baggage` starting at `DispatchWallTime.now()`.
63
65
///
64
66
/// - Parameters:
65
67
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
66
- /// - context: Logging context containing a `Baggage` whichi may contain trace identifiers of a parent `Span`.
68
+ /// - context: Logging context containing a `Baggage` which may contain trace identifiers of a parent `Span`.
67
69
/// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`.
68
70
public func startSpan(
69
71
_ operationName: String ,
@@ -73,3 +75,62 @@ extension Tracer {
73
75
return self . startSpan ( operationName, baggage: context. baggage, ofKind: kind, at: . now( ) )
74
76
}
75
77
}
78
+
79
+ // ==== ----------------------------------------------------------------------------------------------------------------
80
+ // MARK: Starting spans: `withSpan`
81
+
82
+ extension Tracer {
83
+ /// Execute a specific task within a newly created `Span`.
84
+ ///
85
+ /// DO NOT `end()` the passed in span manually. It will be ended automatically when the `function` returns.
86
+ ///
87
+ /// - Parameters:
88
+ /// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
89
+ /// - context: Logging context containing a `Baggage` which may contain trace identifiers of a parent `Span`.
90
+ /// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`.
91
+ /// - function: function to wrap in a span start/end and execute immediately
92
+ /// - Returns: the value returned by `function`
93
+ /// - Throws: the error the `function` has thrown (if any)
94
+ public func withSpan< T> (
95
+ _ operationName: String ,
96
+ context: LoggingContext ,
97
+ ofKind kind: SpanKind = . internal,
98
+ _ function: ( Span ) throws -> T
99
+ ) rethrows -> T {
100
+ let span = self . startSpan ( operationName, context: context, ofKind: kind)
101
+ do {
102
+ return try function ( span)
103
+ } catch {
104
+ span. recordError ( error)
105
+ span. end ( )
106
+ throw error // rethrow
107
+ }
108
+ }
109
+
110
+ /// Execute a specific task within a newly created `Span`.
111
+ ///
112
+ /// DO NOT `end()` the passed in span manually. It will be ended automatically when the `function` returns.
113
+ ///
114
+ /// - Parameters:
115
+ /// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
116
+ /// - baggage: Baggage potentially containing trace identifiers of a parent `Span`.
117
+ /// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`.
118
+ /// - function: function to wrap in a span start/end and execute immediately
119
+ /// - Returns: the value returned by `function`
120
+ /// - Throws: the error the `function` has thrown (if any)
121
+ public func withSpan< T> (
122
+ _ operationName: String ,
123
+ baggage: Baggage ,
124
+ ofKind kind: SpanKind = . internal,
125
+ _ function: ( Span ) throws -> T
126
+ ) rethrows -> T {
127
+ let span = self . startSpan ( operationName, baggage: baggage, ofKind: kind)
128
+ do {
129
+ return try function ( span)
130
+ } catch {
131
+ span. recordError ( error)
132
+ span. end ( )
133
+ throw error // rethrow
134
+ }
135
+ }
136
+ }
0 commit comments