generated from dankinsoid/iOSLibraryTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
230f7bb
commit d5f8245
Showing
11 changed files
with
167 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Foundation | ||
|
||
public extension APIClient { | ||
|
||
/// Whether to report metrics. | ||
/// - Parameter reportMetrics: A boolean value indicating whether to report metrics. | ||
/// - Returns: An instance of `APIClient` configured with the specified metrics reporting setting. | ||
func reportMetrics(_ reportMetrics: Bool) -> APIClient { | ||
configs(\.reportMetrics, reportMetrics) | ||
} | ||
} | ||
|
||
public extension APIClient.Configs { | ||
|
||
/// Whether to report metrics. | ||
var reportMetrics: Bool { | ||
get { self[\.reportMetrics] ?? true } | ||
set { self[\.reportMetrics] = newValue } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Foundation | ||
import HTTPTypes | ||
|
||
extension HTTPResponse.Status.Kind { | ||
|
||
var isError: Bool { | ||
self == .clientError || self == .serverError || self == .invalid | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Metrics | ||
import HTTPTypes | ||
import Foundation | ||
|
||
func updateTotalRequestsMetrics( | ||
for request: HTTPRequestComponents | ||
) { | ||
Counter( | ||
label: "api_client_requests_total", | ||
dimensions: dimensions(for: request) | ||
).increment() | ||
} | ||
|
||
func updateTotalResponseMetrics( | ||
for request: HTTPRequestComponents, | ||
successful: Bool | ||
) { | ||
Counter( | ||
label: "api_client_responses_total", | ||
dimensions: dimensions(for: request) + [("successful", successful.description)] | ||
).increment() | ||
if !successful { | ||
updateTotalErrorsMetrics(for: request) | ||
} | ||
} | ||
|
||
func updateTotalErrorsMetrics( | ||
for request: HTTPRequestComponents? | ||
) { | ||
Counter( | ||
label: "api_client_errors_total", | ||
dimensions: dimensions(for: request) | ||
).increment() | ||
} | ||
|
||
func updateHTTPMetrics( | ||
for request: HTTPRequestComponents?, | ||
status: HTTPResponse.Status?, | ||
duration: Double, | ||
successful: Bool | ||
) { | ||
var dimensions = dimensions(for: request) | ||
dimensions.append(("status", status?.code.description ?? "undefined")) | ||
dimensions.append(("successful", successful.description)) | ||
Timer( | ||
label: "http_client_request_duration_seconds", | ||
dimensions: dimensions, | ||
preferredDisplayUnit: .seconds | ||
) | ||
.recordSeconds(duration) | ||
} | ||
|
||
private func dimensions( | ||
for request: HTTPRequestComponents? | ||
) -> [(String, String)] { | ||
[ | ||
("method", request?.method.rawValue ?? "undefined"), | ||
("path", request?.urlComponents.path ?? "undefined") | ||
] | ||
} |