Skip to content

Commit 2d09dee

Browse files
committed
Add SpanKind enum
1 parent e3d24aa commit 2d09dee

File tree

6 files changed

+83
-9
lines changed

6 files changed

+83
-9
lines changed

Sources/Instrumentation/Tracing/NoOpTracing.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import Dispatch
1818
public struct NoOpTracingInstrument: TracingInstrument {
1919
public var currentSpan: Span? = nil
2020

21-
public func startSpan(named operationName: String, context: BaggageContext, at timestamp: DispatchTime?) -> Span {
21+
public func startSpan(
22+
named operationName: String,
23+
context: BaggageContext,
24+
ofKind kind: SpanKind,
25+
at timestamp: DispatchTime?
26+
) -> Span {
2227
NoOpSpan()
2328
}
2429

@@ -35,6 +40,7 @@ public struct NoOpTracingInstrument: TracingInstrument {
3540
public struct NoOpSpan: Span {
3641
public var operationName: String = ""
3742
public var status: SpanStatus?
43+
public let kind: SpanKind = .internal
3844

3945
public var startTimestamp: DispatchTime {
4046
.now()

Sources/Instrumentation/Tracing/Span.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public protocol Span {
2424
/// [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#span).
2525
var operationName: String { get }
2626

27+
/// The kind of this span.
28+
var kind: SpanKind { get }
29+
2730
/// The status of this span.
2831
var status: SpanStatus? { get set }
2932

@@ -246,3 +249,24 @@ public struct SpanStatus {
246249
case unauthenticated
247250
}
248251
}
252+
253+
// ==== ----------------------------------------------------------------------------------------------------------------
254+
// MARK: Span Kind
255+
256+
/// Describes the relationship between the Span, its parents, and its children in a Trace.
257+
public enum SpanKind {
258+
/// Indicates that the span covers server-side handling of a synchronous RPC or other remote request.
259+
/// This span is the child of a remote `.client` span that was expected to wait for a response.
260+
case server
261+
/// Indicates that the span describes a synchronous request to some remote service.
262+
/// This span is the parent of a remote `.server` span and waits for its response.
263+
case client
264+
/// Indicates that the span describes the parent of an asynchronous request. This parent span is expected to end before the corresponding child
265+
/// `.consumer` span, possibly even before the child span starts. In messaging scenarios with batching,
266+
/// tracing individual messages requires a new `.producer` span per message to be created.
267+
case producer
268+
/// Indicates that the span describes the child of an asynchronous `.producer` request.
269+
case consumer
270+
/// Indicates that the span represents an internal operation within an application, as opposed to an operations with remote parents or children.
271+
case `internal`
272+
}

Sources/Instrumentation/Tracing/TracingInstrument.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ public protocol TracingInstrument: Instrument {
2525
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
2626
/// - context: The `BaggageContext` within to start the new `Span`.
2727
/// - timestamp: The `DispatchTime` at which to start the new `Span`.
28-
func startSpan(named operationName: String, context: BaggageContext, at timestamp: DispatchTime?) -> Span
28+
func startSpan(
29+
named operationName: String,
30+
context: BaggageContext,
31+
ofKind kind: SpanKind,
32+
at timestamp: DispatchTime?
33+
) -> Span
2934
}
3035

3136
extension TracingInstrument {
@@ -34,7 +39,14 @@ extension TracingInstrument {
3439
/// - Parameters:
3540
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
3641
/// - context: The `BaggageContext` within to start the new `Span`.
37-
public func startSpan(named operationName: String, context: BaggageContext) -> Span {
38-
self.startSpan(named: operationName, context: context, at: nil)
42+
/// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`.
43+
/// - timestamp: The `DispatchTime` at which to start the new `Span`. Defaults to `nil`.
44+
public func startSpan(
45+
named operationName: String,
46+
context: BaggageContext,
47+
ofKind kind: SpanKind = .internal,
48+
at timestamp: DispatchTime? = nil
49+
) -> Span {
50+
self.startSpan(named: operationName, context: context, ofKind: .internal, at: nil)
3951
}
4052
}

Tests/InstrumentationTests/SpanTests.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ import XCTest
1818
final class SpanTests: XCTestCase {
1919
func testAddingEventCreatesCopy() {
2020
// TODO: We should probably replace OTSpan at some point with a NoOpSpan for testing things like this.
21-
let span = OTSpan(operationName: "test", startTimestamp: .now(), context: BaggageContext(), onEnd: { _ in })
21+
let span = OTSpan(
22+
operationName: "test",
23+
startTimestamp: .now(),
24+
context: BaggageContext(),
25+
kind: .internal,
26+
onEnd: { _ in }
27+
)
2228
XCTAssert(span.events.isEmpty)
2329

2430
let copiedSpan = span.addingEvent("test-event")

Tests/InstrumentationTests/Tracing/TracedLockTests.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,18 @@ enum TaskIDKey: BaggageContextKey {
5555
private final class TracedLockPrintlnTracer: TracingInstrument {
5656
var currentSpan: Span? // FIXME: likely not like that
5757

58-
func startSpan(named operationName: String, context: BaggageContext, at timestamp: DispatchTime?) -> Span {
59-
TracedLockPrintlnSpan(operationName: operationName, startTimestamp: timestamp ?? .now(), context: context)
58+
func startSpan(
59+
named operationName: String,
60+
context: BaggageContext,
61+
ofKind kind: SpanKind,
62+
at timestamp: DispatchTime?
63+
) -> Span {
64+
TracedLockPrintlnSpan(
65+
operationName: operationName,
66+
startTimestamp: timestamp ?? .now(),
67+
kind: kind,
68+
context: context
69+
)
6070
}
6171

6272
func inject<Carrier, Injector>(_ baggage: BaggageContext, into carrier: inout Carrier, using injector: Injector)
@@ -71,6 +81,7 @@ private final class TracedLockPrintlnTracer: TracingInstrument {
7181

7282
struct TracedLockPrintlnSpan: Span {
7383
let operationName: String
84+
let kind: SpanKind
7485

7586
var status: SpanStatus? {
7687
didSet {
@@ -100,11 +111,13 @@ private final class TracedLockPrintlnTracer: TracingInstrument {
100111
init(
101112
operationName: String,
102113
startTimestamp: DispatchTime,
114+
kind: SpanKind,
103115
context baggage: BaggageContext
104116
) {
105117
self.operationName = operationName
106118
self.startTimestamp = startTimestamp
107119
self.baggage = baggage
120+
self.kind = kind
108121

109122
print(" span [\(self.operationName): \(self.baggage[TaskIDKey.self] ?? "no-name")] @ \(self.startTimestamp): start")
110123
}

Tests/InstrumentationTests/TracingInstrumentTests.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@ final class TracingInstrumentTests: XCTestCase {
3434
final class JaegerTracer: TracingInstrument {
3535
private(set) var currentSpan: Span?
3636

37-
func startSpan(named operationName: String, context: BaggageContext, at timestamp: DispatchTime?) -> Span {
38-
let span = OTSpan(operationName: operationName, startTimestamp: timestamp ?? .now(), context: context) { span in
37+
func startSpan(
38+
named operationName: String,
39+
context: BaggageContext,
40+
ofKind kind: SpanKind,
41+
at timestamp: DispatchTime?
42+
) -> Span {
43+
let span = OTSpan(
44+
operationName: operationName,
45+
startTimestamp: timestamp ?? .now(),
46+
context: context,
47+
kind: kind
48+
) { span in
3949
span.baggage.logger.info(#"Emitting span "\#(span.operationName)" to backend"#)
4050
span.baggage.logger.info("\(span.attributes)")
4151
}
@@ -86,6 +96,7 @@ extension JaegerTracer {
8696

8797
struct OTSpan: Span {
8898
let operationName: String
99+
let kind: SpanKind
89100

90101
var status: SpanStatus? {
91102
didSet {
@@ -118,12 +129,14 @@ struct OTSpan: Span {
118129
operationName: String,
119130
startTimestamp: DispatchTime,
120131
context baggage: BaggageContext,
132+
kind: SpanKind,
121133
onEnd: @escaping (Span) -> Void
122134
) {
123135
self.operationName = operationName
124136
self.startTimestamp = startTimestamp
125137
self.baggage = baggage
126138
self.onEnd = onEnd
139+
self.kind = kind
127140
}
128141

129142
mutating func addEvent(_ event: SpanEvent) {

0 commit comments

Comments
 (0)