Skip to content

Commit 1c9c2b1

Browse files
authored
Merge pull request #73 from slashmo/feature/spanlink
Add SpanLink type
2 parents e860641 + c4123d5 commit 1c9c2b1

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

Sources/Instrumentation/Tracing/NoOpTracing.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public struct NoOpTracingInstrument: TracingInstrument {
4141
public var operationName: String = ""
4242
public var status: SpanStatus?
4343
public let kind: SpanKind = .internal
44+
public let links = [SpanLink]()
4445

4546
public var startTimestamp: DispatchTime {
4647
.now()
@@ -56,6 +57,8 @@ public struct NoOpTracingInstrument: TracingInstrument {
5657
[]
5758
}
5859

60+
public mutating func addLink(_ link: SpanLink) {}
61+
5962
public mutating func addEvent(_ event: SpanEvent) {}
6063

6164
public var attributes: SpanAttributes {

Sources/Instrumentation/Tracing/Span.swift

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,19 @@ public protocol Span {
4848
/// - Parameter event: The `SpanEvent` to add to this `Span`.
4949
mutating func addEvent(_ event: SpanEvent)
5050

51-
// TODO: Wrap in a struct to hide collection implementation details.
52-
5351
/// The attributes describing this `Span`.
5452
var attributes: SpanAttributes { get set }
5553

5654
/// Returns true if this `Span` is recording information like events, attributes, status, etc.
5755
var isRecording: Bool { get }
5856

57+
/// The read-only collection of linked `Span`s.
58+
var links: [SpanLink] { get }
59+
60+
/// Add a `SpanLink` in place.
61+
/// - Parameter link: The `SpanLink` to add to this `Span`.
62+
mutating func addLink(_ link: SpanLink)
63+
5964
/// End this `Span` at the given timestamp.
6065
/// - Parameter timestamp: The `DispatchTime` at which the span ended.
6166
mutating func end(at timestamp: DispatchTime)
@@ -270,3 +275,26 @@ public enum SpanKind {
270275
/// Indicates that the span represents an internal operation within an application, as opposed to an operations with remote parents or children.
271276
case `internal`
272277
}
278+
279+
// ==== ----------------------------------------------------------------------------------------------------------------
280+
// MARK: Span Link
281+
282+
/// A link to another `Span`.
283+
/// The other `Span`s information is stored in `context` and `attributes` may be used to
284+
/// further describe the link.
285+
public struct SpanLink {
286+
/// A `BaggageContext` containing identifying information about the link target `Span`.
287+
public let context: BaggageContext
288+
289+
/// `SpanAttributes` further describing the connection between the `Span`s.
290+
public let attributes: SpanAttributes
291+
292+
/// Create a new `SpanLink`.
293+
/// - Parameters:
294+
/// - context: The `BaggageContext` identifying the targeted `Span`.
295+
/// - attributes: `SpanAttributes` that further describe the link. Defaults to no attributes.
296+
public init(context: BaggageContext, attributes: SpanAttributes = [:]) {
297+
self.context = context
298+
self.attributes = attributes
299+
}
300+
}

Tests/InstrumentationTests/Tracing/TracedLockTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ private final class TracedLockPrintlnTracer: TracingInstrument {
9494

9595
let baggage: BaggageContext
9696

97+
private(set) var links = [SpanLink]()
98+
9799
private(set) var events = [SpanEvent]() {
98100
didSet {
99101
self.isRecording = !self.events.isEmpty
@@ -122,6 +124,10 @@ private final class TracedLockPrintlnTracer: TracingInstrument {
122124
print(" span [\(self.operationName): \(self.baggage[TaskIDKey.self] ?? "no-name")] @ \(self.startTimestamp): start")
123125
}
124126

127+
mutating func addLink(_ link: SpanLink) {
128+
self.links.append(link)
129+
}
130+
125131
mutating func addEvent(_ event: SpanEvent) {
126132
self.events.append(event)
127133
}

Tests/InstrumentationTests/TracingInstrumentTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ struct OTSpan: Span {
115115
}
116116
}
117117

118+
private(set) var links = [SpanLink]()
119+
118120
var attributes: SpanAttributes = [:] {
119121
didSet {
120122
self.isRecording = !self.attributes.isEmpty
@@ -139,6 +141,10 @@ struct OTSpan: Span {
139141
self.kind = kind
140142
}
141143

144+
mutating func addLink(_ link: SpanLink) {
145+
self.links.append(link)
146+
}
147+
142148
mutating func addEvent(_ event: SpanEvent) {
143149
self.events.append(event)
144150
}

0 commit comments

Comments
 (0)