|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Distributed Tracing open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift Distributed Tracing project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift Distributed Tracing project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +@_spi(Locking) import Instrumentation |
| 16 | +import Tracing |
| 17 | + |
| 18 | +public struct TestSpan: Span { |
| 19 | + public let context: ServiceContext |
| 20 | + public let spanContext: TestSpanContext |
| 21 | + public let startInstant: any TracerInstant |
| 22 | + |
| 23 | + init( |
| 24 | + operationName: String, |
| 25 | + context: ServiceContext, |
| 26 | + spanContext: TestSpanContext, |
| 27 | + startInstant: any TracerInstant, |
| 28 | + onEnd: @escaping @Sendable (FinishedTestSpan) -> Void |
| 29 | + ) { |
| 30 | + self._operationName = LockedValueBox(operationName) |
| 31 | + self.context = context |
| 32 | + self.spanContext = spanContext |
| 33 | + self.startInstant = startInstant |
| 34 | + self.onEnd = onEnd |
| 35 | + } |
| 36 | + |
| 37 | + public var isRecording: Bool { |
| 38 | + _isRecording.withValue(\.self) |
| 39 | + } |
| 40 | + |
| 41 | + public var operationName: String { |
| 42 | + get { |
| 43 | + _operationName.withValue(\.self) |
| 44 | + } |
| 45 | + nonmutating set { |
| 46 | + assertIsRecording() |
| 47 | + _operationName.withValue { $0 = newValue } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public var attributes: SpanAttributes { |
| 52 | + get { |
| 53 | + _attributes.withValue(\.self) |
| 54 | + } |
| 55 | + nonmutating set { |
| 56 | + assertIsRecording() |
| 57 | + _attributes.withValue { $0 = newValue } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public var events: [SpanEvent] { |
| 62 | + _events.withValue(\.self) |
| 63 | + } |
| 64 | + |
| 65 | + public func addEvent(_ event: SpanEvent) { |
| 66 | + assertIsRecording() |
| 67 | + _events.withValue { $0.append(event) } |
| 68 | + } |
| 69 | + |
| 70 | + public var links: [SpanLink] { |
| 71 | + _links.withValue(\.self) |
| 72 | + } |
| 73 | + |
| 74 | + public func addLink(_ link: SpanLink) { |
| 75 | + assertIsRecording() |
| 76 | + _links.withValue { $0.append(link) } |
| 77 | + } |
| 78 | + |
| 79 | + public var errors: [RecordedError] { |
| 80 | + _errors.withValue(\.self) |
| 81 | + } |
| 82 | + |
| 83 | + public func recordError( |
| 84 | + _ error: any Error, |
| 85 | + attributes: SpanAttributes, |
| 86 | + at instant: @autoclosure () -> some TracerInstant |
| 87 | + ) { |
| 88 | + assertIsRecording() |
| 89 | + _errors.withValue { |
| 90 | + $0.append(RecordedError(error: error, attributes: attributes, instant: instant())) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public var status: SpanStatus? { |
| 95 | + _status.withValue(\.self) |
| 96 | + } |
| 97 | + |
| 98 | + public func setStatus(_ status: SpanStatus) { |
| 99 | + assertIsRecording() |
| 100 | + _status.withValue { $0 = status } |
| 101 | + } |
| 102 | + |
| 103 | + public func end(at instant: @autoclosure () -> some TracerInstant) { |
| 104 | + assertIsRecording() |
| 105 | + let finishedSpan = FinishedTestSpan( |
| 106 | + operationName: operationName, |
| 107 | + context: context, |
| 108 | + spanContext: spanContext, |
| 109 | + startInstant: startInstant, |
| 110 | + endInstant: instant(), |
| 111 | + attributes: attributes, |
| 112 | + events: events, |
| 113 | + links: links, |
| 114 | + errors: errors, |
| 115 | + status: status |
| 116 | + ) |
| 117 | + _isRecording.withValue { $0 = false } |
| 118 | + onEnd(finishedSpan) |
| 119 | + } |
| 120 | + |
| 121 | + public struct RecordedError: Sendable { |
| 122 | + public let error: Error |
| 123 | + public let attributes: SpanAttributes |
| 124 | + public let instant: any TracerInstant |
| 125 | + } |
| 126 | + |
| 127 | + private let _operationName: LockedValueBox<String> |
| 128 | + private let _attributes = LockedValueBox<SpanAttributes>([:]) |
| 129 | + private let _events = LockedValueBox<[SpanEvent]>([]) |
| 130 | + private let _links = LockedValueBox<[SpanLink]>([]) |
| 131 | + private let _errors = LockedValueBox<[RecordedError]>([]) |
| 132 | + private let _status = LockedValueBox<SpanStatus?>(nil) |
| 133 | + private let _isRecording = LockedValueBox<Bool>(true) |
| 134 | + private let onEnd: @Sendable (FinishedTestSpan) -> Void |
| 135 | + |
| 136 | + private func assertIsRecording( |
| 137 | + file: StaticString = #file, |
| 138 | + line: UInt = #line |
| 139 | + ) { |
| 140 | + assert( |
| 141 | + _isRecording.withValue(\.self) == true, |
| 142 | + "Attempted to mutate already ended span.", |
| 143 | + file: file, |
| 144 | + line: line |
| 145 | + ) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +public struct FinishedTestSpan: Sendable { |
| 150 | + public let operationName: String |
| 151 | + public let context: ServiceContext |
| 152 | + public let spanContext: TestSpanContext |
| 153 | + public let startInstant: any TracerInstant |
| 154 | + public let endInstant: any TracerInstant |
| 155 | + public let attributes: SpanAttributes |
| 156 | + public let events: [SpanEvent] |
| 157 | + public let links: [SpanLink] |
| 158 | + public let errors: [TestSpan.RecordedError] |
| 159 | + public let status: SpanStatus? |
| 160 | +} |
0 commit comments