diff --git a/Package.swift b/Package.swift index 07436192..f2a6c5c9 100644 --- a/Package.swift +++ b/Package.swift @@ -3,12 +3,6 @@ import PackageDescription let package = Package( name: "swift-distributed-tracing", - platforms: [ - .macOS(.v10_15), - .iOS(.v13), - .tvOS(.v13), - .watchOS(.v6), - ], products: [ .library(name: "Instrumentation", targets: ["Instrumentation"]), .library(name: "Tracing", targets: ["Tracing"]), diff --git a/Sources/Tracing/InstrumentationSystem+Tracing.swift b/Sources/Tracing/InstrumentationSystem+Tracing.swift index 8eafce39..669db59c 100644 --- a/Sources/Tracing/InstrumentationSystem+Tracing.swift +++ b/Sources/Tracing/InstrumentationSystem+Tracing.swift @@ -14,6 +14,7 @@ @_exported import Instrumentation +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) extension InstrumentationSystem { #if swift(>=5.7.0) /// Returns the ``Tracer`` bootstrapped as part of the `InstrumentationSystem`. diff --git a/Sources/Tracing/NoOpTracer.swift b/Sources/Tracing/NoOpTracer.swift index 152295c9..b0d7f72b 100644 --- a/Sources/Tracing/NoOpTracer.swift +++ b/Sources/Tracing/NoOpTracer.swift @@ -91,6 +91,7 @@ public struct NoOpTracer: LegacyTracer { } #if swift(>=5.7.0) +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) extension NoOpTracer: Tracer { public func startSpan( _ operationName: String, diff --git a/Sources/Tracing/Tracer.swift b/Sources/Tracing/Tracer.swift index 8b7c2a29..4413f6c8 100644 --- a/Sources/Tracing/Tracer.swift +++ b/Sources/Tracing/Tracer.swift @@ -276,6 +276,7 @@ public func withSpan( /// - operation: The operation that this span should be measuring /// - Returns: the value returned by `operation` /// - Throws: the error the `operation` has thrown (if any) +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) public func withSpan( _ operationName: String, context: @autoclosure () -> ServiceContext = .current ?? .topLevel, @@ -418,6 +419,7 @@ public func withSpan( /// - operation: The operation that this span should be measuring /// - Returns: the value returned by `operation` /// - Throws: the error the `operation` has thrown (if any) +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) public func withSpan( _ operationName: String, context: @autoclosure () -> ServiceContext = .current ?? .topLevel, diff --git a/Sources/Tracing/TracerProtocol+Legacy.swift b/Sources/Tracing/TracerProtocol+Legacy.swift index a67cfe54..b7dab16e 100644 --- a/Sources/Tracing/TracerProtocol+Legacy.swift +++ b/Sources/Tracing/TracerProtocol+Legacy.swift @@ -388,6 +388,7 @@ extension LegacyTracer { #if swift(>=5.7.0) // Provide compatibility shims of the `...AnySpan` APIs to the 5.7 requiring `Tracer`. +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) extension Tracer { /// Start a new span returning an existential ``Span`` reference. /// diff --git a/Sources/Tracing/TracerProtocol.swift b/Sources/Tracing/TracerProtocol.swift index 755de757..a00336a9 100644 --- a/Sources/Tracing/TracerProtocol.swift +++ b/Sources/Tracing/TracerProtocol.swift @@ -113,6 +113,7 @@ extension Tracer { // ==== ---------------------------------------------------------------------------------------------------------------- // MARK: Starting spans: `withSpan` +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) extension Tracer { /// Start a new ``Span`` and automatically end when the `operation` completes, /// including recording the `error` in case the operation throws. diff --git a/Sources/_TracingBenchmarks/SpanAttributesDSLBenchmark.swift b/Sources/_TracingBenchmarks/SpanAttributesDSLBenchmark.swift index c0f6b2fa..43b9d700 100644 --- a/Sources/_TracingBenchmarks/SpanAttributesDSLBenchmark.swift +++ b/Sources/_TracingBenchmarks/SpanAttributesDSLBenchmark.swift @@ -15,121 +15,141 @@ import _TracingBenchmarkTools import Tracing -public let SpanAttributesDSLBenchmarks: [BenchmarkInfo] = [ - BenchmarkInfo( - name: "SpanAttributesDSLBenchmarks.000_bench_empty", - runFunction: { _ in try! bench_empty(times: 100) }, - tags: [], - setUpFunction: { setUp() }, - tearDownFunction: tearDown - ), - BenchmarkInfo( - name: "SpanAttributesDSLBenchmarks.001_bench_makeSpan", - runFunction: { _ in try! bench_makeSpan(times: 100) }, - tags: [], - setUpFunction: { setUp() }, - tearDownFunction: tearDown - ), - BenchmarkInfo( - name: "SpanAttributesDSLBenchmarks.002_bench_startSpan_end", - runFunction: { _ in try! bench_makeSpan(times: 100) }, - tags: [], - setUpFunction: { setUp() }, - tearDownFunction: tearDown - ), - - BenchmarkInfo( - name: "SpanAttributesDSLBenchmarks.00_bench_set_String_raw", - runFunction: { _ in try! bench_set_String_raw(times: 100) }, - tags: [], - setUpFunction: { setUp() }, - tearDownFunction: tearDown - ), - BenchmarkInfo( - name: "SpanAttributesDSLBenchmarks.01_bench_set_String_dsl", - runFunction: { _ in try! bench_set_String_dsl(times: 100) }, - tags: [], - setUpFunction: { setUp() }, - tearDownFunction: tearDown - ), - - BenchmarkInfo( - name: "SpanAttributesDSLBenchmarks.02_bench_set_Int_raw", - runFunction: { _ in try! bench_set_String_raw(times: 100) }, - tags: [], - setUpFunction: { setUp() }, - tearDownFunction: tearDown - ), - BenchmarkInfo( - name: "SpanAttributesDSLBenchmarks.03_bench_set_Int_dsl", - runFunction: { _ in try! bench_set_String_dsl(times: 100) }, - tags: [], - setUpFunction: { setUp() }, - tearDownFunction: tearDown - ), -] - -private var span: (any Tracing.Span)! - -private func setUp() { - span = InstrumentationSystem.legacyTracer.startAnySpan("something", context: .topLevel) -} +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext +enum DSLBenchmarks { + public static let SpanAttributesDSLBenchmarks: [BenchmarkInfo] = [ + BenchmarkInfo( + name: "SpanAttributesDSLBenchmarks.000_bench_empty", + runFunction: { _ in try! bench_empty(times: 100) }, + tags: [], + setUpFunction: { setUp() }, + tearDownFunction: tearDown + ), + BenchmarkInfo( + name: "SpanAttributesDSLBenchmarks.001_bench_makeSpan", + runFunction: { _ in try! bench_makeSpan(times: 100) }, + tags: [], + setUpFunction: { setUp() }, + tearDownFunction: tearDown + ), + BenchmarkInfo( + name: "SpanAttributesDSLBenchmarks.002_bench_startSpan_end", + runFunction: { _ in try! bench_makeSpan(times: 100) }, + tags: [], + setUpFunction: { setUp() }, + tearDownFunction: tearDown + ), + + BenchmarkInfo( + name: "SpanAttributesDSLBenchmarks.00_bench_set_String_raw", + runFunction: { _ in try! bench_set_String_raw(times: 100) }, + tags: [], + setUpFunction: { setUp() }, + tearDownFunction: tearDown + ), + BenchmarkInfo( + name: "SpanAttributesDSLBenchmarks.01_bench_set_String_dsl", + runFunction: { _ in try! bench_set_String_dsl(times: 100) }, + tags: [], + setUpFunction: { setUp() }, + tearDownFunction: tearDown + ), + + BenchmarkInfo( + name: "SpanAttributesDSLBenchmarks.02_bench_set_Int_raw", + runFunction: { _ in try! bench_set_String_raw(times: 100) }, + tags: [], + setUpFunction: { setUp() }, + tearDownFunction: tearDown + ), + BenchmarkInfo( + name: "SpanAttributesDSLBenchmarks.03_bench_set_Int_dsl", + runFunction: { _ in try! bench_set_String_dsl(times: 100) }, + tags: [], + setUpFunction: { setUp() }, + tearDownFunction: tearDown + ), + ] + + fileprivate static var span: (any Tracing.Span)! + + fileprivate static func setUp() { + self.span = InstrumentationSystem.legacyTracer.startAnySpan("something", context: .topLevel) + } -private func tearDown() { - span = nil -} + fileprivate static func tearDown() { + self.span = nil + } -// ==== ---------------------------------------------------------------------------------------------------------------- -// MARK: make span + // ==== ---------------------------------------------------------------------------------------------------------------- + // MARK: make span -func bench_empty(times: Int) throws {} + static func bench_empty(times: Int) throws {} -func bench_makeSpan(times: Int) throws { - for _ in 0 ..< times { - let span = InstrumentationSystem.legacyTracer.startAnySpan("something", context: .topLevel) - _ = span + static func bench_makeSpan(times: Int) throws { + for _ in 0 ..< times { + let span = InstrumentationSystem.legacyTracer.startAnySpan("something", context: .topLevel) + _ = span + } } -} -func bench_startSpan_end(times: Int) throws { - for _ in 0 ..< times { - let span = InstrumentationSystem.legacyTracer.startAnySpan("something", context: .topLevel) - span.end() + static func bench_startSpan_end(times: Int) throws { + for _ in 0 ..< times { + let span = InstrumentationSystem.legacyTracer.startAnySpan("something", context: .topLevel) + span.end() + } } -} -// ==== ---------------------------------------------------------------------------------------------------------------- -// MARK: set String + // ==== ---------------------------------------------------------------------------------------------------------------- + // MARK: set String -func bench_set_String_raw(times: Int) throws { - for _ in 0 ..< times { - span.attributes["http.method"] = "POST" + static func bench_set_String_raw(times: Int) throws { + for _ in 0 ..< times { + self.span.attributes["http.method"] = "POST" + } } -} -func bench_set_String_dsl(times: Int) throws { - for _ in 0 ..< times { - span.attributes.http.method = "POST" + static func bench_set_String_dsl(times: Int) throws { + for _ in 0 ..< times { + self.span.attributes.http.method = "POST" + } } -} -// ==== ---------------------------------------------------------------------------------------------------------------- -// MARK: set Int + // ==== ---------------------------------------------------------------------------------------------------------------- + // MARK: set Int -func bench_set_Int_raw(times: Int) throws { - for _ in 0 ..< times { - span.attributes["http.status_code"] = 200 + static func bench_set_Int_raw(times: Int) throws { + for _ in 0 ..< times { + self.span.attributes["http.status_code"] = 200 + } } -} -func bench_set_Int_dsl(times: Int) throws { - for _ in 0 ..< times { - span.attributes.http.statusCode = 200 + static func bench_set_Int_dsl(times: Int) throws { + for _ in 0 ..< times { + self.span.attributes.http.statusCode = 200 + } + } + + @dynamicMemberLookup + struct HTTPAttributes: SpanAttributeNamespace { + var attributes: SpanAttributes + + init(attributes: SpanAttributes) { + self.attributes = attributes + } + + struct NestedSpanAttributes: NestedSpanAttributesProtocol { + init() {} + + var method: Key { "http.method" } + var statusCode: Key { "http.status_code" } + } } } +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext extension SpanAttributes { - var http: HTTPAttributes { + var http: DSLBenchmarks.HTTPAttributes { get { .init(attributes: self) } @@ -138,19 +158,3 @@ extension SpanAttributes { } } } - -@dynamicMemberLookup -struct HTTPAttributes: SpanAttributeNamespace { - var attributes: SpanAttributes - - init(attributes: SpanAttributes) { - self.attributes = attributes - } - - struct NestedSpanAttributes: NestedSpanAttributesProtocol { - init() {} - - var method: Key { "http.method" } - var statusCode: Key { "http.status_code" } - } -} diff --git a/Sources/_TracingBenchmarks/main.swift b/Sources/_TracingBenchmarks/main.swift index 6613a37e..73ad3c5a 100644 --- a/Sources/_TracingBenchmarks/main.swift +++ b/Sources/_TracingBenchmarks/main.swift @@ -37,6 +37,7 @@ private func registerBenchmark(_ name: String, _ function: @escaping (Int) -> Vo registerBenchmark(BenchmarkInfo(name: name, runFunction: function, tags: tags)) } -registerBenchmark(SpanAttributesDSLBenchmarks) - -main() +if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) { // for TaskLocal ServiceContext + registerBenchmark(DSLBenchmarks.SpanAttributesDSLBenchmarks) + main() +}