From bf2f8c9131f0e3f41e916bf47a8604abbb0b6380 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 6 Oct 2022 16:44:52 +0900 Subject: [PATCH 1/9] Allow dynamic tracepoint enabling, test shows poc, but this can be rather general and powerful --- Sources/Tracing/NoOpTracer.swift | 4 +- Sources/Tracing/Tracer.swift | 28 ++- Tests/LinuxMain.swift | 1 + .../DynamicTracepointTracerTests+XCTest.swift | 33 +++ .../DynamicTracepointTracerTests.swift | 207 ++++++++++++++++++ Tests/TracingTests/TestTracer.swift | 16 +- Tests/TracingTests/TracedLockTests.swift | 4 +- 7 files changed, 283 insertions(+), 10 deletions(-) create mode 100644 Tests/TracingTests/DynamicTracepointTracerTests+XCTest.swift create mode 100644 Tests/TracingTests/DynamicTracepointTracerTests.swift diff --git a/Sources/Tracing/NoOpTracer.swift b/Sources/Tracing/NoOpTracer.swift index a851094..2167ae5 100644 --- a/Sources/Tracing/NoOpTracer.swift +++ b/Sources/Tracing/NoOpTracer.swift @@ -24,7 +24,9 @@ public struct NoOpTracer: Tracer { _ operationName: String, baggage: Baggage, ofKind kind: SpanKind, - at time: DispatchWallTime + at time: DispatchWallTime, + file fileID: String, + line: UInt ) -> Span { NoOpSpan(baggage: baggage) } diff --git a/Sources/Tracing/Tracer.swift b/Sources/Tracing/Tracer.swift index 91857d7..0d67228 100644 --- a/Sources/Tracing/Tracer.swift +++ b/Sources/Tracing/Tracer.swift @@ -30,11 +30,15 @@ public protocol Tracer: Instrument { /// - baggage: The `Baggage` providing information on where to start the new ``Span``. /// - kind: The ``SpanKind`` of the new ``Span``. /// - time: The `DispatchTime` at which to start the new ``Span``. + /// - file: The `fileID` where the span was started. + /// - line: The file line where the span was started. func startSpan( _ operationName: String, baggage: Baggage, ofKind kind: SpanKind, - at time: DispatchWallTime + at time: DispatchWallTime, + file fileID: String, + line: UInt ) -> Span /// Export all ended spans to the configured backend that have not yet been exported. @@ -56,9 +60,11 @@ extension Tracer { public func startSpan( _ operationName: String, baggage: Baggage, - ofKind kind: SpanKind = .internal + ofKind kind: SpanKind = .internal, + file fileID: String = #fileID, + line: UInt = #line ) -> Span { - self.startSpan(operationName, baggage: baggage, ofKind: kind, at: .now()) + self.startSpan(operationName, baggage: baggage, ofKind: kind, at: .now(), file: fileID, line: line) } } @@ -81,9 +87,11 @@ extension Tracer { _ operationName: String, baggage: Baggage, ofKind kind: SpanKind = .internal, + file fileID: String = #fileID, + line: UInt = #line, _ operation: (Span) throws -> T ) rethrows -> T { - let span = self.startSpan(operationName, baggage: baggage, ofKind: kind) + let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, at: .now(), file: fileID, line: line) defer { span.end() } do { return try operation(span) @@ -114,9 +122,11 @@ extension Tracer { public func withSpan( _ operationName: String, ofKind kind: SpanKind = .internal, + file fileID: String = #fileID, + line: UInt = #line, _ operation: (Span) throws -> T ) rethrows -> T { - try self.withSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind) { span in + try self.withSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind, file: fileID, line: line) { span in try Baggage.$current.withValue(span.baggage) { try operation(span) } @@ -137,9 +147,11 @@ extension Tracer { public func withSpan( _ operationName: String, ofKind kind: SpanKind = .internal, + file fileID: String = #fileID, + line: UInt = #line, _ operation: (Span) async throws -> T ) async rethrows -> T { - let span = self.startSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind) + let span = self.startSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind, file: fileID, line: line) defer { span.end() } do { return try await Baggage.$current.withValue(span.baggage) { @@ -168,9 +180,11 @@ extension Tracer { _ operationName: String, baggage: Baggage, ofKind kind: SpanKind = .internal, + file fileID: String = #fileID, + line: UInt = #line, _ operation: (Span) async throws -> T ) async rethrows -> T { - let span = self.startSpan(operationName, baggage: baggage, ofKind: kind) + let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, file: fileID, line: line) defer { span.end() } do { return try await Baggage.$current.withValue(span.baggage) { diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 88768ba..c8bf729 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -34,6 +34,7 @@ class LinuxMainRunnerImpl: LinuxMainRunner { @available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings") func run() { XCTMain([ + testCase(DynamicTracepointTracerTests.allTests), testCase(InstrumentTests.allTests), testCase(InstrumentationSystemTests.allTests), testCase(SpanTests.allTests), diff --git a/Tests/TracingTests/DynamicTracepointTracerTests+XCTest.swift b/Tests/TracingTests/DynamicTracepointTracerTests+XCTest.swift new file mode 100644 index 0000000..ce718ca --- /dev/null +++ b/Tests/TracingTests/DynamicTracepointTracerTests+XCTest.swift @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Distributed Tracing open source project +// +// Copyright (c) 2020-2021 Apple Inc. and the Swift Distributed Tracing project +// authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// +// +// DynamicTracepointTracerTests+XCTest.swift +// +import XCTest +/// +/// NOTE: This file was generated by generate_linux_tests.rb +/// +/// Do NOT edit this file directly as it will be regenerated automatically when needed. +/// + +extension DynamicTracepointTracerTests { + + @available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings") + static var allTests : [(String, (DynamicTracepointTracerTests) -> () throws -> Void)] { + return [ + ("test_adhoc_enableAdHoc", test_adhoc_enableAdHoc), + ] + } +} + diff --git a/Tests/TracingTests/DynamicTracepointTracerTests.swift b/Tests/TracingTests/DynamicTracepointTracerTests.swift new file mode 100644 index 0000000..e153b38 --- /dev/null +++ b/Tests/TracingTests/DynamicTracepointTracerTests.swift @@ -0,0 +1,207 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Distributed Tracing open source project +// +// Copyright (c) 2020-2021 Apple Inc. and the Swift Distributed Tracing project +// authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +@testable import Instrumentation +import InstrumentationBaggage +import Tracing +import XCTest + +final class DynamicTracepointTracerTests: XCTestCase { + override class func tearDown() { + super.tearDown() + InstrumentationSystem.bootstrapInternal(nil) + } + + func test_adhoc_enableAdHoc() { + let tracer = DynamicTracepointTestTracer() + + InstrumentationSystem.bootstrapInternal(tracer) + defer { + InstrumentationSystem.bootstrapInternal(NoOpTracer()) + } + + + let fileID = #fileID + let line: UInt = 7777 // trick number, see withSpan below. + tracer.enableTracepoint(fileID: fileID, line: line) + // Imagine this is set via some "ops command", e.g. ` trace enable Sample.swift:1234` + // Effectively enabling tracepoints is similar to tracer bullets, tho bullets are generally "one off", + // but here we could attach a trace-rate, so e.g.: control ` trace enable Sample:1234 .2` to set 20% sampling rate etc. + + tracer.withSpan("dont") { span in + // don't capture this span... + } + tracer.withSpan("yes", file: #fileID, line: 7777) { span in + // do capture this span, and all child spans of it! + tracer.withSpan("yes-inner", file: #fileID, line: 8888) { inner in + // since the parent of this span was captured, this shall be captured as well + } + } + + XCTAssertEqual(tracer.spans.count, 2) + for span in tracer.spans { + XCTAssertEqual(span.baggage.traceID, "trace-id-fake-\(fileID)-\(line)") + } + XCTAssertEqual(tracer.spans[0].baggage.spanID, "span-id-fake-\(fileID)-\(line)") + XCTAssertEqual(tracer.spans[1].baggage.spanID, "span-id-fake-\(fileID)-8888") + } + +} + +final class DynamicTracepointTestTracer: Tracer { + private(set) var activeTracepoints: Set = [] + struct TracepointID: Hashable { + let fileID: String + let line: UInt + } + + private(set) var spans: [TracepointSpan] = [] + var onEndSpan: (Span) -> Void = { _ in + } + + func startSpan(_ operationName: String, + baggage: InstrumentationBaggage.Baggage, + ofKind kind: Tracing.SpanKind, + at time: DispatchWallTime, + file fileID: String, + line: UInt) -> Tracing.Span { + + let tracepoint = TracepointID(fileID: fileID, line: line) + guard shouldRecord(tracepoint: tracepoint) else { + return NoOpTracer.NoOpSpan(baggage: baggage) + } + + let span = TracepointSpan( + operationName: operationName, + startTime: time, + baggage: baggage, + kind: kind, + file: fileID, + line: line, + onEnd: onEndSpan + ) + self.spans.append(span) + return span + } + + private func shouldRecord(tracepoint: TracepointID) -> Bool { + if isActive(tracepoint: tracepoint) { + // this tracepoint was specifically activated! + return true + } + + // else, perhaps there is already an active span, if so, attach to it + guard let baggage = Baggage.current else { // TODO: we could make this such that we only ever once pick-up 🧳 + return false + } + + guard baggage.traceID != nil else { + // no span is active, return the baggage though + return false + } + + // there is some active trace already, so we should record as well + // TODO: this logic may need to become smarter + return true + } + + func isActive(tracepoint: TracepointID) -> Bool { + activeTracepoints.contains(tracepoint) + } + + @discardableResult + func enableTracepoint(fileID: String, line: UInt) -> Bool { + return self.activeTracepoints.insert(TracepointID(fileID: fileID, line: line)).inserted + } + + func forceFlush() { + } + + func extract(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract) where Extract: Extractor, Extract.Carrier == Carrier { + let traceID = extractor.extract(key: "trace-id", from: carrier) ?? UUID().uuidString + baggage.traceID = traceID + } + + func inject(_ baggage: Baggage, into carrier: inout Carrier, using injector: Inject) where Inject: Injector, Inject.Carrier == Carrier { + guard let traceID = baggage.traceID else { + return + } + injector.inject(traceID, forKey: "trace-id", into: &carrier) + } +} + +extension DynamicTracepointTestTracer { + final class TracepointSpan: Tracing.Span { + private let operationName: String + private let kind: SpanKind + + private var status: SpanStatus? + + private let startTime: DispatchWallTime + private(set) var endTime: DispatchWallTime? + + private(set) var baggage: Baggage + private(set) var isRecording: Bool = false + + let onEnd: (Span) -> Void + + init(operationName: String, + startTime: DispatchWallTime, + baggage: Baggage, + kind: SpanKind, + file fileID: String, + line: UInt, + onEnd: @escaping (Span) -> Void + ) { + self.operationName = operationName + self.startTime = startTime + self.baggage = baggage + self.onEnd = onEnd + self.kind = kind + + // inherit or make a new traceID: + if baggage.traceID == nil { + self.baggage.traceID = "trace-id-fake-\(fileID)-\(line)" + } + + // always make up a new spanID: + self.baggage.spanID = "span-id-fake-\(fileID)-\(line)" + } + + var attributes: Tracing.SpanAttributes = [:] + + func setStatus(_ status: Tracing.SpanStatus) { + // nothing + } + + func addEvent(_ event: Tracing.SpanEvent) { + // nothing + } + + func recordError(_ error: Error) { + print("") + } + + func addLink(_ link: SpanLink) { + // nothing + } + + func end(at time: DispatchWallTime) { + self.endTime = time + self.onEnd(self) + } + + } + +} \ No newline at end of file diff --git a/Tests/TracingTests/TestTracer.swift b/Tests/TracingTests/TestTracer.swift index ba7a8bb..47412b1 100644 --- a/Tests/TracingTests/TestTracer.swift +++ b/Tests/TracingTests/TestTracer.swift @@ -26,7 +26,9 @@ final class TestTracer: Tracer { _ operationName: String, baggage: Baggage, ofKind kind: SpanKind, - at time: DispatchWallTime + at time: DispatchWallTime, + file fileID: String, + line: UInt ) -> Span { let span = TestSpan( operationName: operationName, @@ -64,6 +66,9 @@ extension TestTracer { enum TraceIDKey: BaggageKey { typealias Value = String } + enum SpanIDKey: BaggageKey { + typealias Value = String + } } extension Baggage { @@ -75,6 +80,15 @@ extension Baggage { self[TestTracer.TraceIDKey.self] = newValue } } + + var spanID: String? { + get { + self[TestTracer.SpanIDKey.self] + } + set { + self[TestTracer.SpanIDKey.self] = newValue + } + } } final class TestSpan: Span { diff --git a/Tests/TracingTests/TracedLockTests.swift b/Tests/TracingTests/TracedLockTests.swift index 07e1f07..88a8099 100644 --- a/Tests/TracingTests/TracedLockTests.swift +++ b/Tests/TracingTests/TracedLockTests.swift @@ -63,7 +63,9 @@ private final class TracedLockPrintlnTracer: Tracer { _ operationName: String, baggage: Baggage, ofKind kind: SpanKind, - at time: DispatchWallTime + at time: DispatchWallTime, + file fileID: String, + line: UInt ) -> Span { TracedLockPrintlnSpan( operationName: operationName, From b42d1db3eca09ef0471de3cd5069260cbe63ec9c Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 6 Oct 2022 16:46:34 +0900 Subject: [PATCH 2/9] formatting --- .../DynamicTracepointTracerTests.swift | 45 +++++++++---------- Tests/TracingTests/TestTracer.swift | 1 + 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/Tests/TracingTests/DynamicTracepointTracerTests.swift b/Tests/TracingTests/DynamicTracepointTracerTests.swift index e153b38..d0e8dd6 100644 --- a/Tests/TracingTests/DynamicTracepointTracerTests.swift +++ b/Tests/TracingTests/DynamicTracepointTracerTests.swift @@ -31,7 +31,6 @@ final class DynamicTracepointTracerTests: XCTestCase { InstrumentationSystem.bootstrapInternal(NoOpTracer()) } - let fileID = #fileID let line: UInt = 7777 // trick number, see withSpan below. tracer.enableTracepoint(fileID: fileID, line: line) @@ -39,12 +38,12 @@ final class DynamicTracepointTracerTests: XCTestCase { // Effectively enabling tracepoints is similar to tracer bullets, tho bullets are generally "one off", // but here we could attach a trace-rate, so e.g.: control ` trace enable Sample:1234 .2` to set 20% sampling rate etc. - tracer.withSpan("dont") { span in + tracer.withSpan("dont") { _ in // don't capture this span... } - tracer.withSpan("yes", file: #fileID, line: 7777) { span in + tracer.withSpan("yes", file: #fileID, line: 7777) { _ in // do capture this span, and all child spans of it! - tracer.withSpan("yes-inner", file: #fileID, line: 8888) { inner in + tracer.withSpan("yes-inner", file: #fileID, line: 8888) { _ in // since the parent of this span was captured, this shall be captured as well } } @@ -56,7 +55,6 @@ final class DynamicTracepointTracerTests: XCTestCase { XCTAssertEqual(tracer.spans[0].baggage.spanID, "span-id-fake-\(fileID)-\(line)") XCTAssertEqual(tracer.spans[1].baggage.spanID, "span-id-fake-\(fileID)-8888") } - } final class DynamicTracepointTestTracer: Tracer { @@ -75,28 +73,28 @@ final class DynamicTracepointTestTracer: Tracer { ofKind kind: Tracing.SpanKind, at time: DispatchWallTime, file fileID: String, - line: UInt) -> Tracing.Span { - + line: UInt) -> Tracing.Span + { let tracepoint = TracepointID(fileID: fileID, line: line) - guard shouldRecord(tracepoint: tracepoint) else { + guard self.shouldRecord(tracepoint: tracepoint) else { return NoOpTracer.NoOpSpan(baggage: baggage) } let span = TracepointSpan( - operationName: operationName, - startTime: time, - baggage: baggage, - kind: kind, - file: fileID, - line: line, - onEnd: onEndSpan + operationName: operationName, + startTime: time, + baggage: baggage, + kind: kind, + file: fileID, + line: line, + onEnd: onEndSpan ) self.spans.append(span) return span } private func shouldRecord(tracepoint: TracepointID) -> Bool { - if isActive(tracepoint: tracepoint) { + if self.isActive(tracepoint: tracepoint) { // this tracepoint was specifically activated! return true } @@ -117,16 +115,15 @@ final class DynamicTracepointTestTracer: Tracer { } func isActive(tracepoint: TracepointID) -> Bool { - activeTracepoints.contains(tracepoint) + self.activeTracepoints.contains(tracepoint) } @discardableResult func enableTracepoint(fileID: String, line: UInt) -> Bool { - return self.activeTracepoints.insert(TracepointID(fileID: fileID, line: line)).inserted + self.activeTracepoints.insert(TracepointID(fileID: fileID, line: line)).inserted } - func forceFlush() { - } + func forceFlush() {} func extract(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract) where Extract: Extractor, Extract.Carrier == Carrier { let traceID = extractor.extract(key: "trace-id", from: carrier) ?? UUID().uuidString @@ -162,8 +159,8 @@ extension DynamicTracepointTestTracer { kind: SpanKind, file fileID: String, line: UInt, - onEnd: @escaping (Span) -> Void - ) { + onEnd: @escaping (Span) -> Void) + { self.operationName = operationName self.startTime = startTime self.baggage = baggage @@ -201,7 +198,5 @@ extension DynamicTracepointTestTracer { self.endTime = time self.onEnd(self) } - } - -} \ No newline at end of file +} diff --git a/Tests/TracingTests/TestTracer.swift b/Tests/TracingTests/TestTracer.swift index 47412b1..59946b9 100644 --- a/Tests/TracingTests/TestTracer.swift +++ b/Tests/TracingTests/TestTracer.swift @@ -66,6 +66,7 @@ extension TestTracer { enum TraceIDKey: BaggageKey { typealias Value = String } + enum SpanIDKey: BaggageKey { typealias Value = String } From 180340139c0775c09cc0bb7559becec8f06f590e Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 6 Oct 2022 20:38:00 +0900 Subject: [PATCH 3/9] also implement function based matching --- Sources/Tracing/NoOpTracer.swift | 1 + Sources/Tracing/Tracer.swift | 38 +++++- .../DynamicTracepointTracerTests+XCTest.swift | 3 +- .../DynamicTracepointTracerTests.swift | 127 ++++++++++++++---- Tests/TracingTests/TestTracer.swift | 1 + Tests/TracingTests/TracedLockTests.swift | 1 + 6 files changed, 138 insertions(+), 33 deletions(-) diff --git a/Sources/Tracing/NoOpTracer.swift b/Sources/Tracing/NoOpTracer.swift index 2167ae5..97bfc35 100644 --- a/Sources/Tracing/NoOpTracer.swift +++ b/Sources/Tracing/NoOpTracer.swift @@ -25,6 +25,7 @@ public struct NoOpTracer: Tracer { baggage: Baggage, ofKind kind: SpanKind, at time: DispatchWallTime, + function: String, file fileID: String, line: UInt ) -> Span { diff --git a/Sources/Tracing/Tracer.swift b/Sources/Tracing/Tracer.swift index 0d67228..c77d09a 100644 --- a/Sources/Tracing/Tracer.swift +++ b/Sources/Tracing/Tracer.swift @@ -30,13 +30,15 @@ public protocol Tracer: Instrument { /// - baggage: The `Baggage` providing information on where to start the new ``Span``. /// - kind: The ``SpanKind`` of the new ``Span``. /// - time: The `DispatchTime` at which to start the new ``Span``. - /// - file: The `fileID` where the span was started. + /// - function: The function name in which the span was started + /// - fileID: The `fileID` where the span was started. /// - line: The file line where the span was started. func startSpan( _ operationName: String, baggage: Baggage, ofKind kind: SpanKind, at time: DispatchWallTime, + function: String, file fileID: String, line: UInt ) -> Span @@ -57,14 +59,19 @@ extension Tracer { /// - operationName: The name of the operation being traced. This may be a handler function, database call, ... /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``. /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. + /// - function: The function name in which the span was started + /// - fileID: The `fileID` where the span was started. + /// - line: The file line where the span was started. public func startSpan( _ operationName: String, baggage: Baggage, ofKind kind: SpanKind = .internal, + function: String = #function, file fileID: String = #fileID, line: UInt = #line ) -> Span { - self.startSpan(operationName, baggage: baggage, ofKind: kind, at: .now(), file: fileID, line: line) + self.startSpan(operationName, baggage: baggage, ofKind: kind, at: .now(), + function: function, file: fileID, line: line) } } @@ -81,17 +88,22 @@ extension Tracer { /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``. /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. /// - operation: operation to wrap in a span start/end and execute immediately + /// - function: The function name in which the span was started + /// - fileID: The `fileID` where the span was started. + /// - line: The file line where the span was started. /// - Returns: the value returned by `operation` /// - Throws: the error the `operation` has thrown (if any) public func withSpan( _ operationName: String, baggage: Baggage, ofKind kind: SpanKind = .internal, + function: String = #function, file fileID: String = #fileID, line: UInt = #line, _ operation: (Span) throws -> T ) rethrows -> T { - let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, at: .now(), file: fileID, line: line) + let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, at: .now(), + function: function, file: fileID, line: line) defer { span.end() } do { return try operation(span) @@ -117,16 +129,21 @@ extension Tracer { /// - operationName: The name of the operation being traced. This may be a handler function, database call, ... /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. /// - operation: operation to wrap in a span start/end and execute immediately + /// - function: The function name in which the span was started + /// - fileID: The `fileID` where the span was started. + /// - line: The file line where the span was started. /// - Returns: the value returned by `operation` /// - Throws: the error the `operation` has thrown (if any) public func withSpan( _ operationName: String, ofKind kind: SpanKind = .internal, + function: String = #function, file fileID: String = #fileID, line: UInt = #line, _ operation: (Span) throws -> T ) rethrows -> T { - try self.withSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind, file: fileID, line: line) { span in + try self.withSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind, + function: function, file: fileID, line: line) { span in try Baggage.$current.withValue(span.baggage) { try operation(span) } @@ -142,16 +159,21 @@ extension Tracer { /// - operationName: The name of the operation being traced. This may be a handler function, database call, ... /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. /// - operation: operation to wrap in a span start/end and execute immediately + /// - function: The function name in which the span was started + /// - fileID: The `fileID` where the span was started. + /// - line: The file line where the span was started. /// - Returns: the value returned by `operation` /// - Throws: the error the `operation` has thrown (if any) public func withSpan( _ operationName: String, ofKind kind: SpanKind = .internal, + function: String = #function, file fileID: String = #fileID, line: UInt = #line, _ operation: (Span) async throws -> T ) async rethrows -> T { - let span = self.startSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind, file: fileID, line: line) + let span = self.startSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind, + function: function, file: fileID, line: line) defer { span.end() } do { return try await Baggage.$current.withValue(span.baggage) { @@ -174,17 +196,21 @@ extension Tracer { // task local and modified before passing into this function. The baggage will be made the current task-local baggage for the duration of the `operation`. /// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`. /// - operation: operation to wrap in a span start/end and execute immediately + /// - function: The function name in which the span was started + /// - fileID: The `fileID` where the span was started. + /// - line: The file line where the span was started. /// - Returns: the value returned by `operation` /// - Throws: the error the `operation` has thrown (if any) public func withSpan( _ operationName: String, baggage: Baggage, ofKind kind: SpanKind = .internal, + function: String = #function, file fileID: String = #fileID, line: UInt = #line, _ operation: (Span) async throws -> T ) async rethrows -> T { - let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, file: fileID, line: line) + let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, function: function, file: fileID, line: line) defer { span.end() } do { return try await Baggage.$current.withValue(span.baggage) { diff --git a/Tests/TracingTests/DynamicTracepointTracerTests+XCTest.swift b/Tests/TracingTests/DynamicTracepointTracerTests+XCTest.swift index ce718ca..8c3a061 100644 --- a/Tests/TracingTests/DynamicTracepointTracerTests+XCTest.swift +++ b/Tests/TracingTests/DynamicTracepointTracerTests+XCTest.swift @@ -26,7 +26,8 @@ extension DynamicTracepointTracerTests { @available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings") static var allTests : [(String, (DynamicTracepointTracerTests) -> () throws -> Void)] { return [ - ("test_adhoc_enableAdHoc", test_adhoc_enableAdHoc), + ("test_adhoc_enableBySourceLoc", test_adhoc_enableBySourceLoc), + ("test_adhoc_enableByFunction", test_adhoc_enableByFunction), ] } } diff --git a/Tests/TracingTests/DynamicTracepointTracerTests.swift b/Tests/TracingTests/DynamicTracepointTracerTests.swift index d0e8dd6..56b2bef 100644 --- a/Tests/TracingTests/DynamicTracepointTracerTests.swift +++ b/Tests/TracingTests/DynamicTracepointTracerTests.swift @@ -23,7 +23,7 @@ final class DynamicTracepointTracerTests: XCTestCase { InstrumentationSystem.bootstrapInternal(nil) } - func test_adhoc_enableAdHoc() { + func test_adhoc_enableBySourceLoc() { let tracer = DynamicTracepointTestTracer() InstrumentationSystem.bootstrapInternal(tracer) @@ -32,8 +32,9 @@ final class DynamicTracepointTracerTests: XCTestCase { } let fileID = #fileID - let line: UInt = 7777 // trick number, see withSpan below. - tracer.enableTracepoint(fileID: fileID, line: line) + let fakeLine: UInt = 77 // trick number, see withSpan below. + let fakeNextLine: UInt = fakeLine + 11 + tracer.enableTracepoint(fileID: fileID, line: fakeLine) // Imagine this is set via some "ops command", e.g. ` trace enable Sample.swift:1234` // Effectively enabling tracepoints is similar to tracer bullets, tho bullets are generally "one off", // but here we could attach a trace-rate, so e.g.: control ` trace enable Sample:1234 .2` to set 20% sampling rate etc. @@ -41,27 +42,91 @@ final class DynamicTracepointTracerTests: XCTestCase { tracer.withSpan("dont") { _ in // don't capture this span... } - tracer.withSpan("yes", file: #fileID, line: 7777) { _ in + tracer.withSpan("yes", line: fakeLine) { _ in // do capture this span, and all child spans of it! - tracer.withSpan("yes-inner", file: #fileID, line: 8888) { _ in + tracer.withSpan("yes-inner", line: fakeNextLine) { _ in // since the parent of this span was captured, this shall be captured as well } } XCTAssertEqual(tracer.spans.count, 2) for span in tracer.spans { - XCTAssertEqual(span.baggage.traceID, "trace-id-fake-\(fileID)-\(line)") + XCTAssertEqual(span.baggage.traceID, "trace-id-fake-\(fileID)-\(fakeLine)") + } + XCTAssertEqual(tracer.spans[0].baggage.spanID, "span-id-fake-\(fileID)-\(fakeLine)") + XCTAssertEqual(tracer.spans[1].baggage.spanID, "span-id-fake-\(fileID)-\(fakeNextLine)") + } + + func test_adhoc_enableByFunction() { + let tracer = DynamicTracepointTestTracer() + + InstrumentationSystem.bootstrapInternal(tracer) + defer { + InstrumentationSystem.bootstrapInternal(NoOpTracer()) + } + + let fileID = #fileID + tracer.enableTracepoint(function: "traceMeLogic(fakeLine:)") + + let fakeLine: UInt = 66 + let fakeNextLine: UInt = fakeLine + 11 + + logic(fakeLine: 55) + traceMeLogic(fakeLine: fakeLine) + + XCTAssertEqual(tracer.spans.count, 2) + for span in tracer.spans { + XCTAssertEqual(span.baggage.traceID, "trace-id-fake-\(fileID)-\(fakeLine)") + } + XCTAssertEqual(tracer.spans[0].baggage.spanID, "span-id-fake-\(fileID)-\(fakeLine)") + XCTAssertEqual(tracer.spans[1].baggage.spanID, "span-id-fake-\(fileID)-\(fakeNextLine)") + } + + func logic(fakeLine: UInt) { + InstrumentationSystem.tracer.withSpan("\(#function)-dont", line: fakeLine) { _ in + + } + } + func traceMeLogic(fakeLine: UInt) { + InstrumentationSystem.tracer.withSpan("\(#function)-yes", line: fakeLine) { _ in + InstrumentationSystem.tracer.withSpan("\(#function)-yes-inside", line: fakeLine + 11) { _ in + // inside + } } - XCTAssertEqual(tracer.spans[0].baggage.spanID, "span-id-fake-\(fileID)-\(line)") - XCTAssertEqual(tracer.spans[1].baggage.spanID, "span-id-fake-\(fileID)-8888") } } final class DynamicTracepointTestTracer: Tracer { private(set) var activeTracepoints: Set = [] + struct TracepointID: Hashable { - let fileID: String - let line: UInt + let function: String? + let fileID: String? + let line: UInt? + + func matches(tracepoint: TracepointID) -> Bool { + var match = true + if let fun = self.function { + match = match && fun == tracepoint.function + if !match { // short-circuit further checks + return false + } + } + if let fid = self.fileID { + match = match && fid == tracepoint.fileID + if !match { // short-circuit further checks + return false + } + } + if let l = self.line { + match = match && l == tracepoint.line + if !match { // short-circuit further checks + return false + } + } + + return match + } } private(set) var spans: [TracepointSpan] = [] @@ -72,22 +137,22 @@ final class DynamicTracepointTestTracer: Tracer { baggage: InstrumentationBaggage.Baggage, ofKind kind: Tracing.SpanKind, at time: DispatchWallTime, + function: String, file fileID: String, - line: UInt) -> Tracing.Span - { - let tracepoint = TracepointID(fileID: fileID, line: line) + line: UInt) -> Tracing.Span { + let tracepoint = TracepointID(function: function, fileID: fileID, line: line) guard self.shouldRecord(tracepoint: tracepoint) else { return NoOpTracer.NoOpSpan(baggage: baggage) } let span = TracepointSpan( - operationName: operationName, - startTime: time, - baggage: baggage, - kind: kind, - file: fileID, - line: line, - onEnd: onEndSpan + operationName: operationName, + startTime: time, + baggage: baggage, + kind: kind, + file: fileID, + line: line, + onEnd: onEndSpan ) self.spans.append(span) return span @@ -115,15 +180,26 @@ final class DynamicTracepointTestTracer: Tracer { } func isActive(tracepoint: TracepointID) -> Bool { - self.activeTracepoints.contains(tracepoint) + for activeTracepoint in self.activeTracepoints { + if activeTracepoint.matches(tracepoint: tracepoint) { + return true + } + } + return false } @discardableResult - func enableTracepoint(fileID: String, line: UInt) -> Bool { - self.activeTracepoints.insert(TracepointID(fileID: fileID, line: line)).inserted + func enableTracepoint(fileID: String, line: UInt? = nil) -> Bool { + self.activeTracepoints.insert(TracepointID(function: nil, fileID: fileID, line: line)).inserted } - func forceFlush() {} + @discardableResult + func enableTracepoint(function: String, fileID: String? = nil, line: UInt? = nil) -> Bool { + self.activeTracepoints.insert(TracepointID(function: function, fileID: fileID, line: line)).inserted + } + + func forceFlush() { + } func extract(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract) where Extract: Extractor, Extract.Carrier == Carrier { let traceID = extractor.extract(key: "trace-id", from: carrier) ?? UUID().uuidString @@ -159,8 +235,7 @@ extension DynamicTracepointTestTracer { kind: SpanKind, file fileID: String, line: UInt, - onEnd: @escaping (Span) -> Void) - { + onEnd: @escaping (Span) -> Void) { self.operationName = operationName self.startTime = startTime self.baggage = baggage diff --git a/Tests/TracingTests/TestTracer.swift b/Tests/TracingTests/TestTracer.swift index 59946b9..e9ec3a3 100644 --- a/Tests/TracingTests/TestTracer.swift +++ b/Tests/TracingTests/TestTracer.swift @@ -27,6 +27,7 @@ final class TestTracer: Tracer { baggage: Baggage, ofKind kind: SpanKind, at time: DispatchWallTime, + function: String, file fileID: String, line: UInt ) -> Span { diff --git a/Tests/TracingTests/TracedLockTests.swift b/Tests/TracingTests/TracedLockTests.swift index 88a8099..81ad3d9 100644 --- a/Tests/TracingTests/TracedLockTests.swift +++ b/Tests/TracingTests/TracedLockTests.swift @@ -64,6 +64,7 @@ private final class TracedLockPrintlnTracer: Tracer { baggage: Baggage, ofKind kind: SpanKind, at time: DispatchWallTime, + function: String, file fileID: String, line: UInt ) -> Span { From 12d73265d5d489a74c24b99138c6e9ed6a0575c7 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 6 Oct 2022 20:38:21 +0900 Subject: [PATCH 4/9] formatting --- Sources/Tracing/Tracer.swift | 42 +++++++++++++++---- .../DynamicTracepointTracerTests.swift | 31 +++++++------- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/Sources/Tracing/Tracer.swift b/Sources/Tracing/Tracer.swift index c77d09a..1bfdeb4 100644 --- a/Sources/Tracing/Tracer.swift +++ b/Sources/Tracing/Tracer.swift @@ -70,8 +70,15 @@ extension Tracer { file fileID: String = #fileID, line: UInt = #line ) -> Span { - self.startSpan(operationName, baggage: baggage, ofKind: kind, at: .now(), - function: function, file: fileID, line: line) + self.startSpan( + operationName, + baggage: baggage, + ofKind: kind, + at: .now(), + function: function, + file: fileID, + line: line + ) } } @@ -102,8 +109,15 @@ extension Tracer { line: UInt = #line, _ operation: (Span) throws -> T ) rethrows -> T { - let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, at: .now(), - function: function, file: fileID, line: line) + let span = self.startSpan( + operationName, + baggage: baggage, + ofKind: kind, + at: .now(), + function: function, + file: fileID, + line: line + ) defer { span.end() } do { return try operation(span) @@ -142,8 +156,14 @@ extension Tracer { line: UInt = #line, _ operation: (Span) throws -> T ) rethrows -> T { - try self.withSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind, - function: function, file: fileID, line: line) { span in + try self.withSpan( + operationName, + baggage: .current ?? .topLevel, + ofKind: kind, + function: function, + file: fileID, + line: line + ) { span in try Baggage.$current.withValue(span.baggage) { try operation(span) } @@ -172,8 +192,14 @@ extension Tracer { line: UInt = #line, _ operation: (Span) async throws -> T ) async rethrows -> T { - let span = self.startSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind, - function: function, file: fileID, line: line) + let span = self.startSpan( + operationName, + baggage: .current ?? .topLevel, + ofKind: kind, + function: function, + file: fileID, + line: line + ) defer { span.end() } do { return try await Baggage.$current.withValue(span.baggage) { diff --git a/Tests/TracingTests/DynamicTracepointTracerTests.swift b/Tests/TracingTests/DynamicTracepointTracerTests.swift index 56b2bef..acefbb6 100644 --- a/Tests/TracingTests/DynamicTracepointTracerTests.swift +++ b/Tests/TracingTests/DynamicTracepointTracerTests.swift @@ -71,8 +71,8 @@ final class DynamicTracepointTracerTests: XCTestCase { let fakeLine: UInt = 66 let fakeNextLine: UInt = fakeLine + 11 - logic(fakeLine: 55) - traceMeLogic(fakeLine: fakeLine) + self.logic(fakeLine: 55) + self.traceMeLogic(fakeLine: fakeLine) XCTAssertEqual(tracer.spans.count, 2) for span in tracer.spans { @@ -84,9 +84,9 @@ final class DynamicTracepointTracerTests: XCTestCase { func logic(fakeLine: UInt) { InstrumentationSystem.tracer.withSpan("\(#function)-dont", line: fakeLine) { _ in - } } + func traceMeLogic(fakeLine: UInt) { InstrumentationSystem.tracer.withSpan("\(#function)-yes", line: fakeLine) { _ in InstrumentationSystem.tracer.withSpan("\(#function)-yes-inside", line: fakeLine + 11) { _ in @@ -125,7 +125,7 @@ final class DynamicTracepointTestTracer: Tracer { } } - return match + return match } } @@ -139,20 +139,21 @@ final class DynamicTracepointTestTracer: Tracer { at time: DispatchWallTime, function: String, file fileID: String, - line: UInt) -> Tracing.Span { + line: UInt) -> Tracing.Span + { let tracepoint = TracepointID(function: function, fileID: fileID, line: line) guard self.shouldRecord(tracepoint: tracepoint) else { return NoOpTracer.NoOpSpan(baggage: baggage) } let span = TracepointSpan( - operationName: operationName, - startTime: time, - baggage: baggage, - kind: kind, - file: fileID, - line: line, - onEnd: onEndSpan + operationName: operationName, + startTime: time, + baggage: baggage, + kind: kind, + file: fileID, + line: line, + onEnd: onEndSpan ) self.spans.append(span) return span @@ -198,8 +199,7 @@ final class DynamicTracepointTestTracer: Tracer { self.activeTracepoints.insert(TracepointID(function: function, fileID: fileID, line: line)).inserted } - func forceFlush() { - } + func forceFlush() {} func extract(_ carrier: Carrier, into baggage: inout Baggage, using extractor: Extract) where Extract: Extractor, Extract.Carrier == Carrier { let traceID = extractor.extract(key: "trace-id", from: carrier) ?? UUID().uuidString @@ -235,7 +235,8 @@ extension DynamicTracepointTestTracer { kind: SpanKind, file fileID: String, line: UInt, - onEnd: @escaping (Span) -> Void) { + onEnd: @escaping (Span) -> Void) + { self.operationName = operationName self.startTime = startTime self.baggage = baggage From 8ed9307b5bc162a6ec417449c1803d08870aedee Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Mon, 7 Nov 2022 17:01:42 +0900 Subject: [PATCH 5/9] compat fixes --- Package.swift | 2 +- Sources/Tracing/Tracer.swift | 82 ++++++++++++++++++- .../DynamicTracepointTracerTests.swift | 6 ++ 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index e3ceb7c..2366ce3 100644 --- a/Package.swift +++ b/Package.swift @@ -8,7 +8,7 @@ let package = Package( .library(name: "Tracing", targets: ["Tracing"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")), + .package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")), .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"), ], targets: [ diff --git a/Sources/Tracing/Tracer.swift b/Sources/Tracing/Tracer.swift index 1bfdeb4..680a0b8 100644 --- a/Sources/Tracing/Tracer.swift +++ b/Sources/Tracing/Tracer.swift @@ -53,7 +53,10 @@ public protocol Tracer: Instrument { } extension Tracer { - /// Start a new ``Span`` with the given `Baggage` starting at `DispatchWallTime.now()`. + + + #if swift(>=5.3.0) + /// Start a new ``Span`` with the given `Baggage` starting "now". /// /// - Parameters: /// - operationName: The name of the operation being traced. This may be a handler function, database call, ... @@ -80,12 +83,43 @@ extension Tracer { line: line ) } + #else + /// Start a new ``Span`` with the given `Baggage` starting "now". + /// + /// - Parameters: + /// - operationName: The name of the operation being traced. This may be a handler function, database call, ... + /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``. + /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. + /// - function: The function name in which the span was started + /// - file: The `file` where the span was started. + /// - line: The file line where the span was started. + public func startSpan( + _ operationName: String, + baggage: Baggage, + ofKind kind: SpanKind = .internal, + function: String = #function, + file: String = #file, + line: UInt = #line + ) -> Span { + self.startSpan( + operationName, + baggage: baggage, + ofKind: kind, + at: .now(), + function: function, + file: file, + line: line + ) + } + #endif + } // ==== ---------------------------------------------------------------------------------------------------------------- // MARK: Starting spans: `withSpan` extension Tracer { + #if swift(>=5.3.0) /// Execute a specific task within a newly created ``Span``. /// /// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns. @@ -126,6 +160,48 @@ extension Tracer { throw error // rethrow } } + #else + /// Execute a specific task within a newly created ``Span``. + /// + /// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns. + /// + /// - Parameters: + /// - operationName: The name of the operation being traced. This may be a handler function, database call, ... + /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``. + /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. + /// - operation: operation to wrap in a span start/end and execute immediately + /// - function: The function name in which the span was started + /// - file: The `#file` where the span was started. + /// - line: The file line where the span was started. + /// - Returns: the value returned by `operation` + /// - Throws: the error the `operation` has thrown (if any) + public func withSpan( + _ operationName: String, + baggage: Baggage, + ofKind kind: SpanKind = .internal, + function: String = #function, + file: String = #file, + line: UInt = #line, + _ operation: (Span) throws -> T + ) rethrows -> T { + let span = self.startSpan( + operationName, + baggage: baggage, + ofKind: kind, + at: .now(), + function: function, + file: file, + line: line + ) + defer { span.end() } + do { + return try operation(span) + } catch { + span.recordError(error) + throw error // rethrow + } + } + #endif } // ==== ---------------------------------------------------------------------------------------------------------------- @@ -202,7 +278,7 @@ extension Tracer { ) defer { span.end() } do { - return try await Baggage.$current.withValue(span.baggage) { + return try await Baggage.withValue(span.baggage) { try await operation(span) } } catch { @@ -239,7 +315,7 @@ extension Tracer { let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, function: function, file: fileID, line: line) defer { span.end() } do { - return try await Baggage.$current.withValue(span.baggage) { + return try await Baggage.withValue(span.baggage) { try await operation(span) } } catch { diff --git a/Tests/TracingTests/DynamicTracepointTracerTests.swift b/Tests/TracingTests/DynamicTracepointTracerTests.swift index acefbb6..b9e25cc 100644 --- a/Tests/TracingTests/DynamicTracepointTracerTests.swift +++ b/Tests/TracingTests/DynamicTracepointTracerTests.swift @@ -24,6 +24,7 @@ final class DynamicTracepointTracerTests: XCTestCase { } func test_adhoc_enableBySourceLoc() { + #if swift(>=5.5) let tracer = DynamicTracepointTestTracer() InstrumentationSystem.bootstrapInternal(tracer) @@ -55,9 +56,11 @@ final class DynamicTracepointTracerTests: XCTestCase { } XCTAssertEqual(tracer.spans[0].baggage.spanID, "span-id-fake-\(fileID)-\(fakeLine)") XCTAssertEqual(tracer.spans[1].baggage.spanID, "span-id-fake-\(fileID)-\(fakeNextLine)") + #endif } func test_adhoc_enableByFunction() { + #if swift(>=5.5) let tracer = DynamicTracepointTestTracer() InstrumentationSystem.bootstrapInternal(tracer) @@ -80,6 +83,7 @@ final class DynamicTracepointTracerTests: XCTestCase { } XCTAssertEqual(tracer.spans[0].baggage.spanID, "span-id-fake-\(fileID)-\(fakeLine)") XCTAssertEqual(tracer.spans[1].baggage.spanID, "span-id-fake-\(fileID)-\(fakeNextLine)") + #endif } func logic(fakeLine: UInt) { @@ -88,11 +92,13 @@ final class DynamicTracepointTracerTests: XCTestCase { } func traceMeLogic(fakeLine: UInt) { + #if swift(>=5.5) InstrumentationSystem.tracer.withSpan("\(#function)-yes", line: fakeLine) { _ in InstrumentationSystem.tracer.withSpan("\(#function)-yes-inside", line: fakeLine + 11) { _ in // inside } } + #endif } } From 1775c76f018ea22c07142a2f47956a17380041c2 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Mon, 7 Nov 2022 17:24:33 +0900 Subject: [PATCH 6/9] formatting --- Sources/Tracing/Tracer.swift | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Sources/Tracing/Tracer.swift b/Sources/Tracing/Tracer.swift index 680a0b8..a9fc422 100644 --- a/Sources/Tracing/Tracer.swift +++ b/Sources/Tracing/Tracer.swift @@ -53,8 +53,6 @@ public protocol Tracer: Instrument { } extension Tracer { - - #if swift(>=5.3.0) /// Start a new ``Span`` with the given `Baggage` starting "now". /// @@ -94,12 +92,12 @@ extension Tracer { /// - file: The `file` where the span was started. /// - line: The file line where the span was started. public func startSpan( - _ operationName: String, - baggage: Baggage, - ofKind kind: SpanKind = .internal, - function: String = #function, - file: String = #file, - line: UInt = #line + _ operationName: String, + baggage: Baggage, + ofKind kind: SpanKind = .internal, + function: String = #function, + file: String = #file, + line: UInt = #line ) -> Span { self.startSpan( operationName, @@ -112,7 +110,6 @@ extension Tracer { ) } #endif - } // ==== ---------------------------------------------------------------------------------------------------------------- From 7ea80f7c6ae3f7bf772102aaf45b74ff697514c4 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Mon, 7 Nov 2022 17:25:48 +0900 Subject: [PATCH 7/9] fix --- Sources/Tracing/Tracer.swift | 4 ++-- Tests/TracingTests/DynamicTracepointTracerTests.swift | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Sources/Tracing/Tracer.swift b/Sources/Tracing/Tracer.swift index a9fc422..dfa7b2a 100644 --- a/Sources/Tracing/Tracer.swift +++ b/Sources/Tracing/Tracer.swift @@ -275,7 +275,7 @@ extension Tracer { ) defer { span.end() } do { - return try await Baggage.withValue(span.baggage) { + return try await Baggage.$current.withValue(span.baggage) { try await operation(span) } } catch { @@ -312,7 +312,7 @@ extension Tracer { let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, function: function, file: fileID, line: line) defer { span.end() } do { - return try await Baggage.withValue(span.baggage) { + return try await Baggage.$current.withValue(span.baggage) { try await operation(span) } } catch { diff --git a/Tests/TracingTests/DynamicTracepointTracerTests.swift b/Tests/TracingTests/DynamicTracepointTracerTests.swift index b9e25cc..34b3b73 100644 --- a/Tests/TracingTests/DynamicTracepointTracerTests.swift +++ b/Tests/TracingTests/DynamicTracepointTracerTests.swift @@ -87,8 +87,10 @@ final class DynamicTracepointTracerTests: XCTestCase { } func logic(fakeLine: UInt) { + #if swift(>=5.5) InstrumentationSystem.tracer.withSpan("\(#function)-dont", line: fakeLine) { _ in } + #endif } func traceMeLogic(fakeLine: UInt) { @@ -166,6 +168,7 @@ final class DynamicTracepointTestTracer: Tracer { } private func shouldRecord(tracepoint: TracepointID) -> Bool { + #if swift(>=5.5) && canImport(_Concurrency) if self.isActive(tracepoint: tracepoint) { // this tracepoint was specifically activated! return true @@ -184,6 +187,9 @@ final class DynamicTracepointTestTracer: Tracer { // there is some active trace already, so we should record as well // TODO: this logic may need to become smarter return true + #else + return false + #endif } func isActive(tracepoint: TracepointID) -> Bool { From 24dd2000390397540e54bf1bca392983b88f4521 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Mon, 7 Nov 2022 17:37:05 +0900 Subject: [PATCH 8/9] bump dependencies in all files --- Package@swift-5.2.swift | 2 +- Package@swift-5.3.swift | 2 +- Package@swift-5.4.swift | 2 +- Package@swift-5.5.swift | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Package@swift-5.2.swift b/Package@swift-5.2.swift index 954727a..111d4ff 100644 --- a/Package@swift-5.2.swift +++ b/Package@swift-5.2.swift @@ -8,7 +8,7 @@ let package = Package( .library(name: "Tracing", targets: ["Tracing"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")), + .package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")), ], targets: [ // ==== -------------------------------------------------------------------------------------------------------- diff --git a/Package@swift-5.3.swift b/Package@swift-5.3.swift index 954727a..111d4ff 100644 --- a/Package@swift-5.3.swift +++ b/Package@swift-5.3.swift @@ -8,7 +8,7 @@ let package = Package( .library(name: "Tracing", targets: ["Tracing"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")), + .package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")), ], targets: [ // ==== -------------------------------------------------------------------------------------------------------- diff --git a/Package@swift-5.4.swift b/Package@swift-5.4.swift index 954727a..111d4ff 100644 --- a/Package@swift-5.4.swift +++ b/Package@swift-5.4.swift @@ -8,7 +8,7 @@ let package = Package( .library(name: "Tracing", targets: ["Tracing"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")), + .package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")), ], targets: [ // ==== -------------------------------------------------------------------------------------------------------- diff --git a/Package@swift-5.5.swift b/Package@swift-5.5.swift index 954727a..111d4ff 100644 --- a/Package@swift-5.5.swift +++ b/Package@swift-5.5.swift @@ -8,7 +8,7 @@ let package = Package( .library(name: "Tracing", targets: ["Tracing"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")), + .package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")), ], targets: [ // ==== -------------------------------------------------------------------------------------------------------- From f96ed04c1bacdaa027a0768d41617d93893216ff Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Mon, 7 Nov 2022 19:06:56 +0900 Subject: [PATCH 9/9] Apply suggestions from code review Co-authored-by: Moritz Lang --- Sources/Tracing/Tracer.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Sources/Tracing/Tracer.swift b/Sources/Tracing/Tracer.swift index dfa7b2a..f0b3cb4 100644 --- a/Sources/Tracing/Tracer.swift +++ b/Sources/Tracing/Tracer.swift @@ -60,7 +60,7 @@ extension Tracer { /// - operationName: The name of the operation being traced. This may be a handler function, database call, ... /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``. /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. - /// - function: The function name in which the span was started + /// - function: The function name in which the span was started. /// - fileID: The `fileID` where the span was started. /// - line: The file line where the span was started. public func startSpan( @@ -88,7 +88,7 @@ extension Tracer { /// - operationName: The name of the operation being traced. This may be a handler function, database call, ... /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``. /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. - /// - function: The function name in which the span was started + /// - function: The function name in which the span was started. /// - file: The `file` where the span was started. /// - line: The file line where the span was started. public func startSpan( @@ -126,7 +126,7 @@ extension Tracer { /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``. /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. /// - operation: operation to wrap in a span start/end and execute immediately - /// - function: The function name in which the span was started + /// - function: The function name in which the span was started. /// - fileID: The `fileID` where the span was started. /// - line: The file line where the span was started. /// - Returns: the value returned by `operation` @@ -167,7 +167,7 @@ extension Tracer { /// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``. /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. /// - operation: operation to wrap in a span start/end and execute immediately - /// - function: The function name in which the span was started + /// - function: The function name in which the span was started. /// - file: The `#file` where the span was started. /// - line: The file line where the span was started. /// - Returns: the value returned by `operation` @@ -216,7 +216,7 @@ extension Tracer { /// - operationName: The name of the operation being traced. This may be a handler function, database call, ... /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. /// - operation: operation to wrap in a span start/end and execute immediately - /// - function: The function name in which the span was started + /// - function: The function name in which the span was started. /// - fileID: The `fileID` where the span was started. /// - line: The file line where the span was started. /// - Returns: the value returned by `operation` @@ -252,7 +252,7 @@ extension Tracer { /// - operationName: The name of the operation being traced. This may be a handler function, database call, ... /// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``. /// - operation: operation to wrap in a span start/end and execute immediately - /// - function: The function name in which the span was started + /// - function: The function name in which the span was started. /// - fileID: The `fileID` where the span was started. /// - line: The file line where the span was started. /// - Returns: the value returned by `operation` @@ -295,7 +295,7 @@ extension Tracer { // task local and modified before passing into this function. The baggage will be made the current task-local baggage for the duration of the `operation`. /// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`. /// - operation: operation to wrap in a span start/end and execute immediately - /// - function: The function name in which the span was started + /// - function: The function name in which the span was started. /// - fileID: The `fileID` where the span was started. /// - line: The file line where the span was started. /// - Returns: the value returned by `operation`