Skip to content

Commit 0cebdcb

Browse files
authored
First byte and priority measurement (#53)
* Increase test timeout * Add first byte & priority * Formatting * Spelling
1 parent 3147746 commit 0cebdcb

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

Sources/NautilusTelemetry/Tracing/Redaction.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Foundation
55

66
public enum Redaction {
77

8-
// MARK: Public
8+
static public let redacted = "REDACTED"
99

1010
/// Provides a default implementation of URL redaction that hides common sensitive elements.
1111
/// - Parameter url: A URL to redact
@@ -39,7 +39,4 @@ public enum Redaction {
3939
return components.url?.absoluteString
4040
}
4141

42-
// MARK: Internal
43-
44-
static public let redacted = "REDACTED"
4542
}

Sources/NautilusTelemetry/Tracing/Span+URLSession.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ extension Span {
118118

119119
addAttribute("http.tls.duration", tlsDuration)
120120
}
121+
122+
addAttribute("http.first_byte.duration", elapsedNanoseconds(metric.fetchStartDate, metric.responseStartDate))
121123
}
122124

123-
/// Annotates the span with attributes from the task's URLRequest.
125+
/// Annotates the span with attributes from the task and its URLRequest.
124126
/// - Parameters:
125127
/// - _: the URLSession instance.
126128
/// - task: the task.
@@ -135,6 +137,12 @@ extension Span {
135137
if let request = task.currentRequest {
136138
addRequestAttributes(request, captureHeaders: captureHeaders, urlRedaction: urlRedaction)
137139
}
140+
141+
// No semantic convention available
142+
// Reported in iOS's 0.0-1.0 range, but is translated inside the network stack,
143+
// based on HTTP protocol version.
144+
// https://blog.cloudflare.com/better-http-3-prioritization-for-a-faster-web/
145+
addAttribute("http.priority", task.priority)
138146
}
139147

140148
/// Annotates the span with attributes from the task's response.

Tests/NautilusTelemetryTests/Tracing/Span+URLSessionTests.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class SpanURLSessionTests: XCTestCase {
3434
let task = urlSession.dataTask(with: urlRequest)
3535
span.urlSession(urlSession, didCreateTask: task, captureHeaders: Set(["content-type"]))
3636

37-
let attributes = try XCTUnwrap(span.attributes as? [String: String])
37+
let attributes = try XCTUnwrap(span.attributes)
3838
XCTAssertEqual(attributes["url.full"], url.absoluteString)
3939
XCTAssertEqual(attributes["http.request.header.content-type"], "application/json")
4040
}
@@ -171,7 +171,21 @@ final class SpanURLSessionTests: XCTestCase {
171171

172172
span.urlSession(urlSession, didCreateTask: task)
173173

174-
let attributes = try XCTUnwrap(span.attributes as? [String: String])
174+
let attributes = try XCTUnwrap(span.attributes)
175175
XCTAssertEqual(attributes["url.full"], "https://REDACTED:REDACTED@example.com/path")
176176
}
177+
178+
func testUrlPriority() throws {
179+
let span = tracer.startSpan(name: #function)
180+
let url = try makeURL("/")
181+
let urlRequest = URLRequest(url: url)
182+
let task = urlSession.dataTask(with: urlRequest)
183+
184+
task.priority = 1.0
185+
span.urlSession(urlSession, didCreateTask: task)
186+
187+
let attributes = try XCTUnwrap(span.attributes)
188+
XCTAssertEqual(attributes["http.priority"], task.priority)
189+
}
190+
177191
}

Tests/NautilusTelemetryTests/Utilities/FlushTimerTests.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import XCTest
1212

1313
final class FlushTimerTests: XCTestCase {
1414

15+
let timeout: TimeInterval = 10
16+
1517
func testFlushTimerInitialization() throws {
1618
let expectation = XCTestExpectation(description: "Timer handler called")
1719
var handlerCallCount = 0
@@ -24,7 +26,7 @@ final class FlushTimerTests: XCTestCase {
2426
XCTAssertEqual(timer.flushInterval, 0.1)
2527
XCTAssertNotNil(timer.flushTimer)
2628

27-
wait(for: [expectation], timeout: 1.0)
29+
wait(for: [expectation], timeout: timeout)
2830
XCTAssertGreaterThanOrEqual(handlerCallCount, 1)
2931
}
3032

@@ -40,7 +42,7 @@ final class FlushTimerTests: XCTestCase {
4042
XCTAssertEqual(timer.flushInterval, 0.1)
4143
XCTAssertNotNil(timer.flushTimer)
4244

43-
wait(for: [expectation], timeout: 1.0)
45+
wait(for: [expectation], timeout: timeout)
4446
XCTAssertGreaterThanOrEqual(handlerCallCount, 1)
4547
}
4648

@@ -58,7 +60,7 @@ final class FlushTimerTests: XCTestCase {
5860
}
5961
}
6062

61-
wait(for: [expectation1], timeout: 1.0)
63+
wait(for: [expectation1], timeout: timeout)
6264
XCTAssertEqual(handlerCallCount, 1)
6365

6466
// Check minimum enforced
@@ -78,7 +80,7 @@ final class FlushTimerTests: XCTestCase {
7880
expectation.fulfill()
7981
}
8082

81-
wait(for: [expectation], timeout: 1.0)
83+
wait(for: [expectation], timeout: timeout)
8284

8385
XCTAssertNotNil(timer) // keep timer alive
8486
}
@@ -110,7 +112,7 @@ final class FlushTimerTests: XCTestCase {
110112

111113
XCTAssertFalse(timer.suspended)
112114

113-
wait(for: [expectation2], timeout: 1.0)
115+
wait(for: [expectation2], timeout: timeout)
114116
XCTAssertEqual(handlerCallCount, 2)
115117
}
116118
}

0 commit comments

Comments
 (0)