@@ -42,7 +42,7 @@ public protocol Tracer: Instrument {
4242}
4343
4444extension 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()`.
4646 ///
4747 /// - Parameters:
4848 /// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
@@ -55,15 +55,17 @@ extension Tracer {
5555 ) -> Span {
5656 return self . startSpan ( operationName, baggage: baggage, ofKind: kind, at: . now( ) )
5757 }
58+ }
5859
59- // ==== --------------------------------------------------------------------
60- // MARK: LoggingContext accepting
60+ // ==== -------------------------------------------- --------------------------------------------------------------------
61+ // MARK: Span creation: with `LoggingContext`
6162
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()`.
6365 ///
6466 /// - Parameters:
6567 /// - 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`.
6769 /// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`.
6870 public func startSpan(
6971 _ operationName: String ,
@@ -73,3 +75,62 @@ extension Tracer {
7375 return self . startSpan ( operationName, baggage: context. baggage, ofKind: kind, at: . now( ) )
7476 }
7577}
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