Skip to content

Commit db10407

Browse files
authored
Correct the default impl for startAnySpan for 5.7 (#112)
1 parent a08effc commit db10407

File tree

2 files changed

+142
-8
lines changed

2 files changed

+142
-8
lines changed

Sources/Tracing/TracerProtocol+Legacy.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -409,15 +409,15 @@ extension TracerProtocol {
409409
/// - function: The function name in which the span was started
410410
/// - fileID: The `fileID` where the span was started.
411411
/// - line: The file line where the span was started.
412-
public func startAnySpan(
412+
public func startAnySpan<Clock: TracerClock>(
413413
_ operationName: String,
414-
clock: some TracerClock = DefaultTracerClock(),
415-
baggage: @autoclosure () -> Baggage = .current ?? .topLevel,
416-
ofKind kind: SpanKind = .internal,
417-
function: String = #function,
418-
file fileID: String = #fileID,
419-
line: UInt = #line
420-
) -> any Span {
414+
baggage: @autoclosure () -> InstrumentationBaggage.Baggage,
415+
ofKind kind: Tracing.SpanKind,
416+
clock: Clock,
417+
function: String,
418+
file fileID: String,
419+
line: UInt
420+
) -> Tracing.Span {
421421
self.startSpan(
422422
operationName,
423423
baggage: baggage(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Distributed Tracing open source project
4+
//
5+
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
6+
// authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Instrumentation
16+
import InstrumentationBaggage
17+
import Tracing
18+
import XCTest
19+
20+
#if swift(>=5.7.0)
21+
// Specifically make sure we don't have to implement startAnySpan
22+
23+
final class SampleSwift57Tracer: TracerProtocol {
24+
private(set) var spans = [SampleSwift57Span]()
25+
var onEndSpan: (SampleSwift57Span) -> Void = { _ in }
26+
27+
func startSpan<Clock: TracerClock>(
28+
_ operationName: String,
29+
baggage: @autoclosure () -> Baggage,
30+
ofKind kind: SpanKind,
31+
clock: Clock,
32+
function: String,
33+
file fileID: String,
34+
line: UInt
35+
) -> SampleSwift57Span {
36+
let span = SampleSwift57Span(
37+
operationName: operationName,
38+
startTime: clock.now,
39+
baggage: baggage(),
40+
kind: kind,
41+
onEnd: self.onEndSpan
42+
)
43+
self.spans.append(span)
44+
return span
45+
}
46+
47+
public func forceFlush() {}
48+
49+
func extract<Carrier, Extract>(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract)
50+
where
51+
Extract: Extractor,
52+
Carrier == Extract.Carrier
53+
{}
54+
55+
func inject<Carrier, Inject>(_ baggage: Baggage, into carrier: inout Carrier, using injector: Inject)
56+
where
57+
Inject: Injector,
58+
Carrier == Inject.Carrier
59+
{}
60+
}
61+
62+
/// Only intended to be used in single-threaded SampleSwift57ing.
63+
final class SampleSwift57Span: Span {
64+
private let kind: SpanKind
65+
66+
private var status: SpanStatus?
67+
68+
public let startTime: UInt64
69+
public private(set) var endTime: UInt64?
70+
71+
private(set) var recordedErrors: [(Error, SpanAttributes)] = []
72+
73+
var operationName: String
74+
let baggage: Baggage
75+
76+
private(set) var events = [SpanEvent]() {
77+
didSet {
78+
self.isRecording = !self.events.isEmpty
79+
}
80+
}
81+
82+
private(set) var links = [SpanLink]()
83+
84+
var attributes: SpanAttributes = [:] {
85+
didSet {
86+
self.isRecording = !self.attributes.isEmpty
87+
}
88+
}
89+
90+
private(set) var isRecording = false
91+
92+
let onEnd: (SampleSwift57Span) -> Void
93+
94+
init<Instant: TracerInstantProtocol>(
95+
operationName: String,
96+
startTime: Instant,
97+
baggage: Baggage,
98+
kind: SpanKind,
99+
onEnd: @escaping (SampleSwift57Span) -> Void
100+
) {
101+
self.operationName = operationName
102+
self.startTime = startTime.millisecondsSinceEpoch
103+
self.baggage = baggage
104+
self.onEnd = onEnd
105+
self.kind = kind
106+
}
107+
108+
func setStatus(_ status: SpanStatus) {
109+
self.status = status
110+
self.isRecording = true
111+
}
112+
113+
func addLink(_ link: SpanLink) {
114+
self.links.append(link)
115+
}
116+
117+
func addEvent(_ event: SpanEvent) {
118+
self.events.append(event)
119+
}
120+
121+
func recordError(_ error: Error, attributes: SpanAttributes) {
122+
self.recordedErrors.append((error, attributes))
123+
}
124+
125+
func end<Clock: TracerClock>(clock: Clock) {
126+
self.endTime = clock.now.millisecondsSinceEpoch
127+
self.onEnd(self)
128+
}
129+
}
130+
131+
extension SampleSwift57Tracer: @unchecked Sendable {} // only intended for single threaded SampleSwift57ing
132+
extension SampleSwift57Span: @unchecked Sendable {} // only intended for single threaded SampleSwift57ing
133+
134+
#endif

0 commit comments

Comments
 (0)