Skip to content

Commit

Permalink
[Diagnostics] Add backend_error_code property
Browse files Browse the repository at this point in the history
  • Loading branch information
tonidero committed Sep 3, 2024
1 parent 5acc08a commit 4b48f46
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions Sources/Diagnostics/DiagnosticsEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extension DiagnosticsEvent {
case responseTimeMillisKey
case successfulKey
case responseCodeKey
case backendErrorCodeKey
case eTagHitKey

}
Expand Down
3 changes: 3 additions & 0 deletions Sources/Diagnostics/DiagnosticsTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protocol DiagnosticsTrackerType {
responseTime: TimeInterval,
wasSuccessful: Bool,
responseCode: Int,
backendErrorCode: Int?,
resultOrigin: HTTPResponseOrigin?,
verificationResult: VerificationResult) async

Expand Down Expand Up @@ -70,6 +71,7 @@ final class DiagnosticsTracker: DiagnosticsTrackerType {
responseTime: TimeInterval,
wasSuccessful: Bool,
responseCode: Int,
backendErrorCode: Int?,
resultOrigin: HTTPResponseOrigin?,
verificationResult: VerificationResult) async {
await track(
Expand All @@ -80,6 +82,7 @@ final class DiagnosticsTracker: DiagnosticsTrackerType {
.responseTimeMillisKey: AnyEncodable(responseTime * 1000),
.successfulKey: AnyEncodable(wasSuccessful),
.responseCodeKey: AnyEncodable(responseCode),
.backendErrorCodeKey: AnyEncodable(backendErrorCode),
.eTagHitKey: AnyEncodable(resultOrigin == .cache),
.verificationResultKey: AnyEncodable(verificationResult.name)
],
Expand Down
2 changes: 2 additions & 0 deletions Sources/Diagnostics/Networking/DiagnosticsEventsRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ private extension DiagnosticsEvent.DiagnosticsPropertyKey {
return "successful"
case .responseCodeKey:
return "response_code"
case .backendErrorCodeKey:
return "backend_error_code"
case .eTagHitKey:
return "etag_hit"
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/Networking/HTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -590,17 +590,21 @@ private extension HTTPClient {
responseTime: responseTime,
wasSuccessful: true,
responseCode: httpStatusCode,
backendErrorCode: nil,
resultOrigin: response.origin,
verificationResult: verificationResult)
case let .failure(error):
var responseCode = -1
if case let .errorResponse(_, code, _) = error {
var backendErrorCode: Int?
if case let .errorResponse(errorResponse, code, _) = error {
responseCode = code.rawValue
backendErrorCode = errorResponse.code.rawValue
}
await diagnosticsTracker.trackHttpRequestPerformed(endpointName: requestPathName,
responseTime: responseTime,
wasSuccessful: false,
responseCode: responseCode,
backendErrorCode: backendErrorCode,
resultOrigin: nil,
verificationResult: .notRequested)
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/UnitTests/Diagnostics/DiagnosticsTrackerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class DiagnosticsTrackerTests: TestCase {
responseTime: 50,
wasSuccessful: true,
responseCode: 200,
backendErrorCode: 7121,
resultOrigin: .cache,
verificationResult: .verified)
let entries = await self.handler.getEntries()
Expand All @@ -123,6 +124,7 @@ class DiagnosticsTrackerTests: TestCase {
.responseTimeMillisKey: AnyEncodable(50000),
.successfulKey: AnyEncodable(true),
.responseCodeKey: AnyEncodable(200),
.backendErrorCodeKey: AnyEncodable(7121),
.eTagHitKey: AnyEncodable(true),
.verificationResultKey: AnyEncodable("VERIFIED")],
timestamp: Self.eventTimestamp1)
Expand Down
11 changes: 9 additions & 2 deletions Tests/UnitTests/Mocks/MockDiagnosticsTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,24 @@ class MockDiagnosticsTracker: DiagnosticsTrackerType {

private(set) var trackedHttpRequestPerformedParams: [
// swiftlint:disable:next large_tuple
(String, TimeInterval, Bool, Int, HTTPResponseOrigin?, VerificationResult)
(String, TimeInterval, Bool, Int, Int?, HTTPResponseOrigin?, VerificationResult)
] = []
// swiftlint:disable:next function_parameter_count
func trackHttpRequestPerformed(endpointName: String,
responseTime: TimeInterval,
wasSuccessful: Bool,
responseCode: Int,
backendErrorCode: Int?,
resultOrigin: HTTPResponseOrigin?,
verificationResult: VerificationResult) async {
self.trackedHttpRequestPerformedParams.append(
(endpointName, responseTime, wasSuccessful, responseCode, resultOrigin, verificationResult)
(endpointName,
responseTime,
wasSuccessful,
responseCode,
backendErrorCode,
resultOrigin,
verificationResult)
)
}

Expand Down
9 changes: 6 additions & 3 deletions Tests/UnitTests/Networking/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,7 @@ final class HTTPClientTests: BaseHTTPClientTests<MockETagManager> {
-1, // Any
true,
200,
nil,
.backend,
.notRequested
)))
Expand Down Expand Up @@ -1662,6 +1663,7 @@ final class HTTPClientTests: BaseHTTPClientTests<MockETagManager> {
false,
401,
nil,
nil,
.notRequested
)))
}
Expand Down Expand Up @@ -2253,8 +2255,8 @@ extension HTTPClientTests {
// swiftlint:disable large_tuple

private func matchTrackParams(
_ data: (String, TimeInterval, Bool, Int, HTTPResponseOrigin?, VerificationResult)
) -> Nimble.Predicate<(String, TimeInterval, Bool, Int, HTTPResponseOrigin?, VerificationResult)> {
_ data: (String, TimeInterval, Bool, Int, Int?, HTTPResponseOrigin?, VerificationResult)
) -> Nimble.Predicate<(String, TimeInterval, Bool, Int, Int?, HTTPResponseOrigin?, VerificationResult)> {
return .init {
let other = try $0.evaluate()
let timeInterval = other?.1 ?? -1
Expand All @@ -2263,7 +2265,8 @@ private func matchTrackParams(
other?.2 == data.2 &&
other?.3 == data.3 &&
other?.4 == data.4 &&
other?.5 == data.5)
other?.5 == data.5 &&
other?.6 == data.6)

return .init(bool: matches, message: .fail("Diagnostics tracked params do not match"))
}
Expand Down

0 comments on commit 4b48f46

Please sign in to comment.