From 101340a3237394b1b47e73239edc93bf1d96a9c2 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 13 Apr 2023 19:35:43 +0900 Subject: [PATCH] end() should take an autoclosure instant, not direct instant **Motivation:** we want to avoid querying time if we don't have to **Modifications:** end() functions now take autoclosure () -> Instant allowing us to not query time unless we have to (e.g. are "recording") --- Sources/Tracing/NoOpTracer.swift | 2 +- Sources/Tracing/SpanProtocol.swift | 2 +- Tests/TracingTests/DynamicTracepointTracerTests.swift | 4 ++-- Tests/TracingTests/TestTracer.swift | 4 ++-- Tests/TracingTests/TracedLockTests.swift | 4 ++-- Tests/TracingTests/TracerTests+swift57.swift | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/Tracing/NoOpTracer.swift b/Sources/Tracing/NoOpTracer.swift index 7aad588..8be5f9e 100644 --- a/Sources/Tracing/NoOpTracer.swift +++ b/Sources/Tracing/NoOpTracer.swift @@ -84,7 +84,7 @@ public struct NoOpTracer: LegacyTracer { } } - public func end(at instant: Instant) { + public func end(at instant: @autoclosure () -> Instant) { // ignore } } diff --git a/Sources/Tracing/SpanProtocol.swift b/Sources/Tracing/SpanProtocol.swift index 410e730..b8feb1d 100644 --- a/Sources/Tracing/SpanProtocol.swift +++ b/Sources/Tracing/SpanProtocol.swift @@ -100,7 +100,7 @@ public protocol Span: _SwiftTracingSendableSpan { /// - instant: the time instant at which the span ended /// /// - SeeAlso: `Span.end()` which automatically uses the "current" time. - func end(at instant: Instant) + func end(at instant: @autoclosure () -> Instant) } extension Span { diff --git a/Tests/TracingTests/DynamicTracepointTracerTests.swift b/Tests/TracingTests/DynamicTracepointTracerTests.swift index 101296e..c0a1fa6 100644 --- a/Tests/TracingTests/DynamicTracepointTracerTests.swift +++ b/Tests/TracingTests/DynamicTracepointTracerTests.swift @@ -322,8 +322,8 @@ extension DynamicTracepointTestTracer { // nothing } - func end(at instant: Instant) { - self.endTimestampNanosSinceEpoch = instant.nanosecondsSinceEpoch + func end(at instant: @autoclosure () -> Instant) { + self.endTimestampNanosSinceEpoch = instant().nanosecondsSinceEpoch self.onEnd(self) } } diff --git a/Tests/TracingTests/TestTracer.swift b/Tests/TracingTests/TestTracer.swift index b4de6b5..a660f61 100644 --- a/Tests/TracingTests/TestTracer.swift +++ b/Tests/TracingTests/TestTracer.swift @@ -181,8 +181,8 @@ final class TestSpan: Span { self.recordedErrors.append((error, attributes)) } - func end(at instant: Instant) { - self.endTimestampNanosSinceEpoch = instant.nanosecondsSinceEpoch + func end(at instant: @autoclosure () -> Instant) { + self.endTimestampNanosSinceEpoch = instant().nanosecondsSinceEpoch self.onEnd(self) } } diff --git a/Tests/TracingTests/TracedLockTests.swift b/Tests/TracingTests/TracedLockTests.swift index 5ef9ff5..6999ac3 100644 --- a/Tests/TracingTests/TracedLockTests.swift +++ b/Tests/TracingTests/TracedLockTests.swift @@ -153,8 +153,8 @@ private final class TracedLockPrintlnTracer: LegacyTracer { func recordError(_ error: Error, attributes: SpanAttributes, at instant: @autoclosure () -> Instant) {} - func end(at instant: Instant) { - let time = instant + func end(at instant: @autoclosure () -> Instant) { + let time = instant() self.endTimeMillis = time.millisecondsSinceEpoch print(" span [\(self.operationName): \(self.baggage[TaskIDKey.self] ?? "no-name")] @ \(time): end") } diff --git a/Tests/TracingTests/TracerTests+swift57.swift b/Tests/TracingTests/TracerTests+swift57.swift index 9ab9764..104caed 100644 --- a/Tests/TracingTests/TracerTests+swift57.swift +++ b/Tests/TracingTests/TracerTests+swift57.swift @@ -122,8 +122,8 @@ final class SampleSwift57Span: Span { self.recordedErrors.append((error, attributes)) } - func end(at instant: Instant) { - self.endTimeNanoseconds = instant.nanosecondsSinceEpoch + func end(at instant: @autoclosure () -> Instant) { + self.endTimeNanoseconds = instant().nanosecondsSinceEpoch self.onEnd(self) } }