Skip to content

Commit 3283a47

Browse files
committed
Allow for reading and mutating a span name, similar to otel spec
**Motivation:** The feature frozen otel specification allows for renaming spans after they have been started. This often is too late to impact sampling decisions, however we still can use to to provide better names for spans as time goes on. **Modifications:** add an operationName to the Span protocol. A span has to be thread-safe already and we have mutating operations on it already so it does not change the complexity requirements on implementing a span. **Result:** Be closer to otel spec and more flexible in naming and renaming spans. Resolves #48
1 parent 499a317 commit 3283a47

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

Sources/Tracing/NoOpTracer.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct NoOpTracer: Tracer {
2929
file fileID: String,
3030
line: UInt
3131
) -> Span {
32-
NoOpSpan(baggage: baggage)
32+
NoOpSpan(operationName: operationName, baggage: baggage)
3333
}
3434

3535
public func forceFlush() {}
@@ -50,7 +50,18 @@ public struct NoOpTracer: Tracer {
5050
public let baggage: Baggage
5151
public let isRecording = false
5252

53-
public init(baggage: Baggage) {
53+
public let _operationName: String
54+
public var operationName: String {
55+
get {
56+
_operationName
57+
}
58+
set {
59+
// ignore
60+
}
61+
}
62+
63+
public init(operationName: String, baggage: Baggage) {
64+
self._operationName = operationName
5465
self.baggage = baggage
5566
}
5667

Sources/Tracing/Span.swift

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ public protocol Span: AnyObject, _SwiftTracingSendableSpan {
3030
/// The read-only `Baggage` of this `Span`, set when starting this `Span`.
3131
var baggage: Baggage { get }
3232

33+
/// Returns the name of the operation this span represents.
34+
///
35+
/// The name may be changed during the lifetime of a `Span`, this change
36+
/// may or may not impact the sampling decision and actually emitting the span,
37+
/// depending on how a backend decides to treat renames.
38+
///
39+
/// This can still be useful when, for example, we want to immediately start
40+
/// span when receiving a request but make it more precise as handling of the request proceeds.
41+
/// For example, we can start a span immediately when a request is received in a server,
42+
/// and update it to reflect the matched route, if it did match one:
43+
///
44+
/// - 1) Start span with basic path (e.g. `operationName = request.head.uri` during `withSpan`)
45+
/// - 2.1) "Route Not Found" -> Record error
46+
/// - 2.2) "Route Found" -> Rename to route (`/users/1` becomes `/users/:userID`)
47+
/// - 3) End span
48+
var operationName: String { get set }
49+
3350
/// Set the status.
3451
/// - Parameter status: The status of this `Span`.
3552
func setStatus(_ status: SpanStatus)

Tests/TracingTests/DynamicTracepointTracerTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ final class DynamicTracepointTestTracer: Tracer {
152152
{
153153
let tracepoint = TracepointID(function: function, fileID: fileID, line: line)
154154
guard self.shouldRecord(tracepoint: tracepoint) else {
155-
return NoOpTracer.NoOpSpan(baggage: baggage)
155+
return NoOpTracer.NoOpSpan(operationName: operationName, baggage: baggage)
156156
}
157157

158158
let span = TracepointSpan(
@@ -230,14 +230,14 @@ final class DynamicTracepointTestTracer: Tracer {
230230
extension DynamicTracepointTestTracer {
231231
/// Only intended to be used in single-threaded testing.
232232
final class TracepointSpan: Tracing.Span {
233-
private let operationName: String
234233
private let kind: SpanKind
235234

236235
private var status: SpanStatus?
237236

238237
private let startTime: DispatchWallTime
239238
private(set) var endTime: DispatchWallTime?
240239

240+
public var operationName: String
241241
private(set) var baggage: Baggage
242242
private(set) var isRecording: Bool = false
243243

Tests/TracingTests/TestTracer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ extension Baggage {
9696

9797
/// Only intended to be used in single-threaded testing.
9898
final class TestSpan: Span {
99-
private let operationName: String
10099
private let kind: SpanKind
101100

102101
private var status: SpanStatus?
103102

104103
private let startTime: DispatchWallTime
105104
private(set) var endTime: DispatchWallTime?
106105

106+
var operationName: String
107107
let baggage: Baggage
108108

109109
private(set) var events = [SpanEvent]() {

Tests/TracingTests/TracedLockTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ private final class TracedLockPrintlnTracer: Tracer {
9898
Carrier == Extract.Carrier {}
9999

100100
final class TracedLockPrintlnSpan: Span {
101-
private let operationName: String
102101
private let kind: SpanKind
103102

104103
private var status: SpanStatus?
105104

106105
private let startTime: DispatchWallTime
107106
private(set) var endTime: DispatchWallTime?
108107

108+
var operationName: String
109109
let baggage: Baggage
110110

111111
private var links = [SpanLink]()

0 commit comments

Comments
 (0)