Skip to content

Commit 15ac1e4

Browse files
authored
Add overlapsInterval (#56)
* Add overlapsInterval * PR feedback, flaky test
1 parent 43b2c38 commit 15ac1e4

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Sources/NautilusTelemetry/Tracing/Span.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ public final class Span: TelemetryAttributesContainer, Identifiable {
115115
}
116116
}
117117

118+
public func overlapsInterval(_ startInterval: ContinuousClock.Instant, endInterval: ContinuousClock.Instant) -> Bool {
119+
precondition(startInterval <= endInterval, "startInterval must be <= endInterval")
120+
let endTime = endTime ?? .now
121+
return startTime <= endInterval && startInterval <= endTime
122+
}
123+
118124
/// Adds an attribute to the span.
119125
/// - Parameters:
120126
/// - name: a name, conforming to https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/trace/semantic_conventions

Tests/NautilusTelemetryTests/Tracing/SpanTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,29 @@ final class SpanTests: XCTestCase {
299299
XCTAssertFalse(span.isRoot)
300300
}
301301

302+
func testOverlapsInterval() {
303+
let traceId = Identifiers.generateTraceId()
304+
let t = ContinuousClock.now
305+
306+
func makeSpan(start: Duration, end: Duration) -> Span {
307+
Span(name: "test", startTime: t + start, endTime: t + end, traceId: traceId, parentId: nil)
308+
}
309+
310+
let span = makeSpan(start: .seconds(2), end: .seconds(4))
311+
312+
// Overlapping cases
313+
XCTAssertTrue(span.overlapsInterval(t + .seconds(1), endInterval: t + .seconds(5)), "interval encompasses span")
314+
XCTAssertTrue(span.overlapsInterval(t + .seconds(3), endInterval: t + .seconds(3)), "span encompasses interval")
315+
XCTAssertTrue(span.overlapsInterval(t + .seconds(1), endInterval: t + .seconds(3)), "overlap at start")
316+
XCTAssertTrue(span.overlapsInterval(t + .seconds(3), endInterval: t + .seconds(5)), "overlap at end")
317+
XCTAssertTrue(span.overlapsInterval(t + .seconds(2), endInterval: t + .seconds(4)), "exact match")
318+
XCTAssertTrue(span.overlapsInterval(t + .seconds(4), endInterval: t + .seconds(5)), "shared endpoint")
319+
320+
// Non-overlapping cases
321+
XCTAssertFalse(span.overlapsInterval(t + .seconds(0), endInterval: t + .seconds(1)), "entirely before")
322+
XCTAssertFalse(span.overlapsInterval(t + .seconds(5), endInterval: t + .seconds(6)), "entirely after")
323+
}
324+
302325
func testSpanSubscript() {
303326
let span = tracer.startSpan(name: "test")
304327
span["key1"] = "value1"

Tests/NautilusTelemetryTests/Utilities/FlushTimerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ final class FlushTimerTests: XCTestCase {
9090
let expectation2 = XCTestExpectation(description: "Timer handler called after resume")
9191
var handlerCallCount = 0
9292

93-
let timer = FlushTimer(flushInterval: 0.1, repeating: true) {
93+
let timer = FlushTimer(flushInterval: 0.2, repeating: true) {
9494
handlerCallCount += 1
9595
if handlerCallCount == 1 {
9696
expectation1.fulfill()

0 commit comments

Comments
 (0)