Skip to content

Commit e5a9692

Browse files
author
Ignacio Bonafonte
authored
Merge pull request #219 from bryce-b/network-info
Added initializer for NetworkStatusInjector
2 parents 4610d6e + 00dcfe1 commit e5a9692

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ let package = Package(
6868
path: "Sources/Instrumentation/URLSession",
6969
exclude: ["README.md"]),
7070
.target(name: "NetworkStatus",
71-
dependencies: ["Reachability"],
71+
dependencies: ["Reachability", "OpenTelemetryApi"],
7272
path: "Sources/Instrumentation/NetworkStatus",
7373
linkerSettings: [.linkedFramework("CoreTelephony")]),
7474
.target(name: "SignPostIntegration",

Sources/Instrumentation/URLSession/URLSessionLogger.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,30 @@
66
import Foundation
77
import OpenTelemetryApi
88
import OpenTelemetrySdk
9-
9+
import os.log
1010
#if os(iOS)
11-
import NetworkStatus
12-
#endif //os(iOS)
11+
import NetworkStatus
12+
#endif // os(iOS)
1313

1414
class URLSessionLogger {
1515
static var runningSpans = [String: Span]()
1616
static var runningSpansQueue = DispatchQueue(label: "io.opentelemetry.URLSessionLogger")
17-
17+
#if os(iOS)
18+
static var netstatInjector: NetworkStatusInjector? = { () -> NetworkStatusInjector? in
19+
do {
20+
let netstats = try NetworkStatus()
21+
return NetworkStatusInjector(netstat: netstats)
22+
} catch {
23+
if #available(iOS 14, macOS 11, tvOS 14, *) {
24+
os_log(.error, "failed to initialize network connection status: %@", error.localizedDescription)
25+
} else {
26+
NSLog("failed to initialize network connection status: %@", error.localizedDescription)
27+
}
28+
29+
return nil
30+
}
31+
}()
32+
#endif // os(iOS)
1833
/// This methods creates a Span for a request, and optionally injects tracing headers, returns a new request if it was needed to create a new one to add the tracing headers
1934
@discardableResult static func processAndLogRequest(_ request: URLRequest, sessionTaskId: String, instrumentation: URLSessionInstrumentation, shouldInjectHeaders: Bool) -> URLRequest? {
2035
guard instrumentation.configuration.shouldInstrument?(request) ?? true else {
@@ -66,12 +81,11 @@ class URLSessionLogger {
6681
}
6782

6883
#if os(iOS)
69-
if let injector = self.netstatInjector {
70-
injector.inject(span: span)
71-
}
84+
if let injector = netstatInjector {
85+
injector.inject(span: span)
86+
}
7287
#endif
7388

74-
7589
instrumentation.configuration.createdRequest?(returnRequest ?? request, span)
7690

7791
return returnRequest ?? request

0 commit comments

Comments
 (0)