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

Commit c757bf6

Browse files
authored
Merge pull request #142 from slashmo/feature/span-class-requirement
Require Span types to be classes
2 parents 7579592 + 2cdbb73 commit c757bf6

File tree

7 files changed

+27
-53
lines changed

7 files changed

+27
-53
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ let package = Package(
77
.library(name: "Instrumentation", targets: ["Instrumentation"]),
88
.library(name: "Tracing", targets: ["Tracing"]),
99
.library(name: "NIOInstrumentation", targets: ["NIOInstrumentation"]),
10-
.library(name: "OpenTelemetryInstrumentationSupport", targets: ["OpenTelemetryInstrumentationSupport"])
10+
.library(name: "OpenTelemetryInstrumentationSupport", targets: ["OpenTelemetryInstrumentationSupport"]),
1111
],
1212
dependencies: [
1313
.package(
1414
url: "https://github.com/slashmo/gsoc-swift-baggage-context.git",
1515
from: "0.3.0"
1616
),
17-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.17.0")
17+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.17.0"),
1818
],
1919
targets: [
2020
// ==== --------------------------------------------------------------------------------------------------------

Sources/Tracing/NoOpTracer.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ public struct NoOpTracer: Tracer {
4545
Extractor: ExtractorProtocol,
4646
Carrier == Extractor.Carrier {}
4747

48-
public struct NoOpSpan: Span {
48+
public final class NoOpSpan: Span {
4949
public var context: BaggageContext {
5050
return .init()
5151
}
5252

53-
public mutating func setStatus(_ status: SpanStatus) {}
53+
public func setStatus(_ status: SpanStatus) {}
5454

55-
public mutating func addLink(_ link: SpanLink) {}
55+
public func addLink(_ link: SpanLink) {}
5656

57-
public mutating func addEvent(_ event: SpanEvent) {}
57+
public func addEvent(_ event: SpanEvent) {}
5858

5959
public func recordError(_ error: Error) {}
6060

@@ -69,7 +69,7 @@ public struct NoOpTracer: Tracer {
6969

7070
public let isRecording = false
7171

72-
public mutating func end(at timestamp: Timestamp) {
72+
public func end(at timestamp: Timestamp) {
7373
// ignore
7474
}
7575
}

Sources/Tracing/Span.swift

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@ import Baggage
1818
/// where `tracer` conforms to `Tracer`.
1919
///
2020
/// - SeeAlso: [OpenTelemetry Specification: Span](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#span).
21-
public protocol Span {
21+
public protocol Span: AnyObject {
2222
/// The read-only `BaggageContext` of this `Span`, set when starting this `Span`.
2323
var context: BaggageContext { get }
2424

2525
/// Set the status.
2626
/// - Parameter status: The status of this `Span`.
27-
mutating func setStatus(_ status: SpanStatus)
27+
func setStatus(_ status: SpanStatus)
2828

2929
/// Add a `SpanEvent` in place.
3030
/// - Parameter event: The `SpanEvent` to add to this `Span`.
31-
mutating func addEvent(_ event: SpanEvent)
31+
func addEvent(_ event: SpanEvent)
3232

3333
/// Record an error of the given type described by the the given message.
3434
///
3535
/// - Parameters:
3636
/// - error: The error to be recorded.
37-
mutating func recordError(_ error: Error)
37+
func recordError(_ error: Error)
3838

3939
/// The attributes describing this `Span`.
4040
var attributes: SpanAttributes { get set }
@@ -44,32 +44,23 @@ public protocol Span {
4444

4545
/// Add a `SpanLink` in place.
4646
/// - Parameter link: The `SpanLink` to add to this `Span`.
47-
mutating func addLink(_ link: SpanLink)
47+
func addLink(_ link: SpanLink)
4848

4949
/// End this `Span` at the given timestamp.
5050
/// - Parameter timestamp: The `Timestamp` at which the span ended.
51-
mutating func end(at timestamp: Timestamp)
51+
func end(at timestamp: Timestamp)
5252
}
5353

5454
extension Span {
55-
/// Create a copy of this `Span` with the given event added to the existing set of events.
56-
/// - Parameter event: The new `SpanEvent` to be added to the returned copy.
57-
/// - Returns: A copy of this `Span` with the given event added to the existing set of events.
58-
public func addingEvent(_ event: SpanEvent) -> Self {
59-
var copy = self
60-
copy.addEvent(event)
61-
return copy
62-
}
63-
6455
/// End this `Span` at the current timestamp.
65-
public mutating func end() {
56+
public func end() {
6657
self.end(at: .now())
6758
}
6859

6960
/// Adds a `SpanLink` between this `Span` and the given `Span`.
7061
/// - Parameter other: The `Span` to link to.
7162
/// - Parameter attributes: The `SpanAttributes` describing this link. Defaults to no attributes.
72-
public mutating func addLink(_ other: Span, attributes: SpanAttributes = [:]) {
63+
public func addLink(_ other: Span, attributes: SpanAttributes = [:]) {
7364
self.addLink(SpanLink(context: other.context, attributes: attributes))
7465
}
7566
}

Tests/TracingTests/SpanTests+XCTest.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ extension SpanTests {
2525
@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings")
2626
static var allTests : [(String, (SpanTests) -> () throws -> Void)] {
2727
return [
28-
("testAddingEventCreatesCopy", testAddingEventCreatesCopy),
2928
("testSpanEventIsExpressibleByStringLiteral", testSpanEventIsExpressibleByStringLiteral),
3029
("testSpanAttributeIsExpressibleByStringLiteral", testSpanAttributeIsExpressibleByStringLiteral),
3130
("testSpanAttributeIsExpressibleByStringInterpolation", testSpanAttributeIsExpressibleByStringInterpolation),

Tests/TracingTests/SpanTests.swift

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,6 @@ import Tracing
1717
import XCTest
1818

1919
final class SpanTests: XCTestCase {
20-
func testAddingEventCreatesCopy() {
21-
let span = TestSpan(
22-
operationName: "test",
23-
startTimestamp: .now(),
24-
context: BaggageContext(),
25-
kind: .internal,
26-
onEnd: { _ in }
27-
)
28-
XCTAssert(span.events.isEmpty)
29-
30-
let copiedSpan = span.addingEvent("test-event")
31-
XCTAssertEqual(copiedSpan.events[0].name, "test-event")
32-
33-
XCTAssert(span.events.isEmpty)
34-
}
35-
3620
func testSpanEventIsExpressibleByStringLiteral() {
3721
let event: SpanEvent = "test"
3822

@@ -172,7 +156,7 @@ final class SpanTests: XCTestCase {
172156
onEnd: { _ in }
173157
)
174158
let childContext = BaggageContext()
175-
var child = TestSpan(
159+
let child = TestSpan(
176160
operationName: "server",
177161
startTimestamp: .now(),
178162
context: childContext,

Tests/TracingTests/TracedLockTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private final class TracedLockPrintlnTracer: Tracer {
8888
Extractor: ExtractorProtocol,
8989
Carrier == Extractor.Carrier {}
9090

91-
struct TracedLockPrintlnSpan: Span {
91+
final class TracedLockPrintlnSpan: Span {
9292
private let operationName: String
9393
private let kind: SpanKind
9494

@@ -129,22 +129,22 @@ private final class TracedLockPrintlnTracer: Tracer {
129129
print(" span [\(self.operationName): \(self.context[TaskIDKey.self] ?? "no-name")] @ \(self.startTimestamp): start")
130130
}
131131

132-
mutating func setStatus(_ status: SpanStatus) {
132+
func setStatus(_ status: SpanStatus) {
133133
self.status = status
134134
self.isRecording = true
135135
}
136136

137-
mutating func addLink(_ link: SpanLink) {
137+
func addLink(_ link: SpanLink) {
138138
self.links.append(link)
139139
}
140140

141-
mutating func addEvent(_ event: SpanEvent) {
141+
func addEvent(_ event: SpanEvent) {
142142
self.events.append(event)
143143
}
144144

145145
func recordError(_ error: Error) {}
146146

147-
mutating func end(at timestamp: Timestamp) {
147+
func end(at timestamp: Timestamp) {
148148
self.endTimestamp = timestamp
149149
print(" span [\(self.operationName): \(self.context[TaskIDKey.self] ?? "no-name")] @ \(timestamp): end")
150150
}

Tests/TracingTests/TracerTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ extension TestTracer {
109109

110110
// MARK: - TestSpan
111111

112-
struct TestSpan: Span {
112+
final class TestSpan: Span {
113113
private let operationName: String
114114
private let kind: SpanKind
115115

@@ -152,22 +152,22 @@ struct TestSpan: Span {
152152
self.kind = kind
153153
}
154154

155-
mutating func setStatus(_ status: SpanStatus) {
155+
func setStatus(_ status: SpanStatus) {
156156
self.status = status
157157
self.isRecording = true
158158
}
159159

160-
mutating func addLink(_ link: SpanLink) {
160+
func addLink(_ link: SpanLink) {
161161
self.links.append(link)
162162
}
163163

164-
mutating func addEvent(_ event: SpanEvent) {
164+
func addEvent(_ event: SpanEvent) {
165165
self.events.append(event)
166166
}
167167

168168
func recordError(_ error: Error) {}
169169

170-
mutating func end(at timestamp: Timestamp) {
170+
func end(at timestamp: Timestamp) {
171171
self.endTimestamp = timestamp
172172
self.onEnd(self)
173173
}
@@ -218,7 +218,7 @@ struct FakeHTTPServer {
218218
var context = BaggageContext()
219219
self.instrument.extract(request.headers, into: &context, using: HTTPHeadersExtractor())
220220

221-
var span = tracer.startSpan(named: "GET \(request.path)", context: context)
221+
let span = tracer.startSpan(named: "GET \(request.path)", context: context)
222222

223223
let response = self.catchAllHandler(span.context, request, self.client)
224224
span.attributes["http.status"] = .int(response.status)

0 commit comments

Comments
 (0)