Skip to content

Commit 8f5315e

Browse files
ktososlashmo
andauthored
startSpan should take tracepoint location (#68)
Co-authored-by: Moritz Lang <[email protected]>
1 parent 6173049 commit 8f5315e

12 files changed

+502
-16
lines changed

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let package = Package(
88
.library(name: "Tracing", targets: ["Tracing"]),
99
],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
11+
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
1212
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
1313
],
1414
targets: [

[email protected]

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let package = Package(
88
.library(name: "Tracing", targets: ["Tracing"]),
99
],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
11+
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
1212
],
1313
targets: [
1414
// ==== --------------------------------------------------------------------------------------------------------

[email protected]

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let package = Package(
88
.library(name: "Tracing", targets: ["Tracing"]),
99
],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
11+
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
1212
],
1313
targets: [
1414
// ==== --------------------------------------------------------------------------------------------------------

[email protected]

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let package = Package(
88
.library(name: "Tracing", targets: ["Tracing"]),
99
],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
11+
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
1212
],
1313
targets: [
1414
// ==== --------------------------------------------------------------------------------------------------------

[email protected]

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let package = Package(
88
.library(name: "Tracing", targets: ["Tracing"]),
99
],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
11+
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
1212
],
1313
targets: [
1414
// ==== --------------------------------------------------------------------------------------------------------

Sources/Tracing/NoOpTracer.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public struct NoOpTracer: Tracer {
2424
_ operationName: String,
2525
baggage: Baggage,
2626
ofKind kind: SpanKind,
27-
at time: DispatchWallTime
27+
at time: DispatchWallTime,
28+
function: String,
29+
file fileID: String,
30+
line: UInt
2831
) -> Span {
2932
NoOpSpan(baggage: baggage)
3033
}

Sources/Tracing/Tracer.swift

+147-8
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ public protocol Tracer: Instrument {
3030
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
3131
/// - kind: The ``SpanKind`` of the new ``Span``.
3232
/// - 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.
3336
func startSpan(
3437
_ operationName: String,
3538
baggage: Baggage,
3639
ofKind kind: SpanKind,
37-
at time: DispatchWallTime
40+
at time: DispatchWallTime,
41+
function: String,
42+
file fileID: String,
43+
line: UInt
3844
) -> Span
3945

4046
/// Export all ended spans to the configured backend that have not yet been exported.
@@ -47,25 +53,111 @@ public protocol Tracer: Instrument {
4753
}
4854

4955
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".
5158
///
5259
/// - Parameters:
5360
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
5461
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
5562
/// - 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.
5666
public func startSpan(
5767
_ operationName: String,
5868
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
6073
) -> 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+
)
6283
}
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
63113
}
64114

65115
// ==== ----------------------------------------------------------------------------------------------------------------
66116
// MARK: Starting spans: `withSpan`
67117

68118
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
69161
/// Execute a specific task within a newly created ``Span``.
70162
///
71163
/// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
@@ -75,15 +167,29 @@ extension Tracer {
75167
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
76168
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
77169
/// - 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.
78173
/// - Returns: the value returned by `operation`
79174
/// - Throws: the error the `operation` has thrown (if any)
80175
public func withSpan<T>(
81176
_ operationName: String,
82177
baggage: Baggage,
83178
ofKind kind: SpanKind = .internal,
179+
function: String = #function,
180+
file: String = #file,
181+
line: UInt = #line,
84182
_ operation: (Span) throws -> T
85183
) 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+
)
87193
defer { span.end() }
88194
do {
89195
return try operation(span)
@@ -92,6 +198,7 @@ extension Tracer {
92198
throw error // rethrow
93199
}
94200
}
201+
#endif
95202
}
96203

97204
// ==== ----------------------------------------------------------------------------------------------------------------
@@ -109,14 +216,27 @@ extension Tracer {
109216
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
110217
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
111218
/// - 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.
112222
/// - Returns: the value returned by `operation`
113223
/// - Throws: the error the `operation` has thrown (if any)
114224
public func withSpan<T>(
115225
_ operationName: String,
116226
ofKind kind: SpanKind = .internal,
227+
function: String = #function,
228+
file fileID: String = #fileID,
229+
line: UInt = #line,
117230
_ operation: (Span) throws -> T
118231
) 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
120240
try Baggage.$current.withValue(span.baggage) {
121241
try operation(span)
122242
}
@@ -132,14 +252,27 @@ extension Tracer {
132252
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
133253
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
134254
/// - 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.
135258
/// - Returns: the value returned by `operation`
136259
/// - Throws: the error the `operation` has thrown (if any)
137260
public func withSpan<T>(
138261
_ operationName: String,
139262
ofKind kind: SpanKind = .internal,
263+
function: String = #function,
264+
file fileID: String = #fileID,
265+
line: UInt = #line,
140266
_ operation: (Span) async throws -> T
141267
) 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+
)
143276
defer { span.end() }
144277
do {
145278
return try await Baggage.$current.withValue(span.baggage) {
@@ -162,15 +295,21 @@ extension Tracer {
162295
// 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`.
163296
/// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`.
164297
/// - 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.
165301
/// - Returns: the value returned by `operation`
166302
/// - Throws: the error the `operation` has thrown (if any)
167303
public func withSpan<T>(
168304
_ operationName: String,
169305
baggage: Baggage,
170306
ofKind kind: SpanKind = .internal,
307+
function: String = #function,
308+
file fileID: String = #fileID,
309+
line: UInt = #line,
171310
_ operation: (Span) async throws -> T
172311
) 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)
174313
defer { span.end() }
175314
do {
176315
return try await Baggage.$current.withValue(span.baggage) {

Tests/LinuxMain.swift

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class LinuxMainRunnerImpl: LinuxMainRunner {
3434
@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings")
3535
func run() {
3636
XCTMain([
37+
testCase(DynamicTracepointTracerTests.allTests),
3738
testCase(InstrumentTests.allTests),
3839
testCase(InstrumentationSystemTests.allTests),
3940
testCase(SpanTests.allTests),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Distributed Tracing open source project
4+
//
5+
// Copyright (c) 2020-2021 Apple Inc. and the Swift Distributed Tracing project
6+
// authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
//
15+
// DynamicTracepointTracerTests+XCTest.swift
16+
//
17+
import XCTest
18+
///
19+
/// NOTE: This file was generated by generate_linux_tests.rb
20+
///
21+
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
22+
///
23+
24+
extension DynamicTracepointTracerTests {
25+
26+
@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings")
27+
static var allTests : [(String, (DynamicTracepointTracerTests) -> () throws -> Void)] {
28+
return [
29+
("test_adhoc_enableBySourceLoc", test_adhoc_enableBySourceLoc),
30+
("test_adhoc_enableByFunction", test_adhoc_enableByFunction),
31+
]
32+
}
33+
}
34+

0 commit comments

Comments
 (0)