Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.

Commit 1b7cc34

Browse files
authored
Merge pull request #110 from slashmo/feature/span-link-convenience
Add SpanLink parent convenience method
2 parents e18a6f6 + ae224c5 commit 1b7cc34

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

Sources/TracingInstrumentation/Span.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ extension Span {
7070
public mutating func end() {
7171
self.end(at: .now())
7272
}
73+
74+
/// Adds a `SpanLink` to the given parent `Span`.
75+
/// - Parameter parent: The parent `Span`.
76+
/// - Parameter attributes: The `SpanAttributes` describing this link. Defaults to no attributes.
77+
public mutating func addLink(_ parent: Span, attributes: SpanAttributes = [:]) {
78+
self.addLink(SpanLink(context: parent.context, attributes: attributes))
79+
}
7380
}
7481

7582
// ==== ----------------------------------------------------------------------------------------------------------------

Tests/TracingInstrumentationTests/SpanTests.swift

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,6 @@ final class SpanTests: XCTestCase {
105105
XCTAssertEqual(stringValue, "test")
106106
}
107107

108-
// func testSpanAttributesProvideSubscriptAccess() {
109-
// var attributes: SpanAttributes = [:]
110-
// XCTAssert(attributes.isEmpty)
111-
//
112-
// attributes["0"] = false
113-
// XCTAssertFalse(attributes.isEmpty)
114-
//
115-
// guard case .bool(let flag) = attributes["0"], !flag else {
116-
// XCTFail("Expected subscript getter to return the bool attribute.")
117-
// return
118-
// }
119-
// }
120-
121108
func testSpanAttributesUX() {
122109
var attributes: SpanAttributes = [:]
123110

@@ -168,6 +155,35 @@ final class SpanTests: XCTestCase {
168155
return
169156
}
170157
}
158+
159+
func testSpanParentConvenience() {
160+
var parentContext = BaggageContext()
161+
parentContext[TestBaggageContextKey.self] = "test"
162+
163+
let parent = OTSpan(
164+
operationName: "client",
165+
startTimestamp: .now(),
166+
context: parentContext,
167+
kind: .client,
168+
onEnd: { _ in }
169+
)
170+
let childContext = BaggageContext()
171+
var child = OTSpan(
172+
operationName: "server",
173+
startTimestamp: .now(),
174+
context: childContext,
175+
kind: .server,
176+
onEnd: { _ in }
177+
)
178+
179+
var attributes = SpanAttributes()
180+
attributes.sampleHttp.statusCode = 418
181+
child.addLink(parent, attributes: attributes)
182+
183+
XCTAssertEqual(child.links.count, 1)
184+
XCTAssertEqual(child.links[0].context[TestBaggageContextKey.self], "test")
185+
XCTAssertEqual(child.links[0].attributes.sampleHttp.statusCode, 418)
186+
}
171187
}
172188

173189
// ==== ----------------------------------------------------------------------------------------------------------------
@@ -223,3 +239,7 @@ public struct CustomAttributeValue: Equatable, CustomStringConvertible, SpanAttr
223239
"CustomAttributeValue()"
224240
}
225241
}
242+
243+
private struct TestBaggageContextKey: BaggageContextKey {
244+
typealias Value = String
245+
}

Tests/TracingInstrumentationTests/TracingInstrumentTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ struct OTSpan: Span {
134134
}
135135
}
136136

137-
private var links = [SpanLink]()
137+
private(set) var links = [SpanLink]()
138138

139139
var attributes: SpanAttributes = [:] {
140140
didSet {

0 commit comments

Comments
 (0)