Skip to content

Commit a7426e4

Browse files
authored
Merge pull request #482 from open-telemetry/library-evolution-support
Library evolution support
2 parents 7bef81a + 79ac0e4 commit a7426e4

File tree

4 files changed

+194
-135
lines changed

4 files changed

+194
-135
lines changed

Package.resolved

Lines changed: 119 additions & 130 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

[email protected]

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ let package = Package(
4242
.package(url: "https://github.com/apple/swift-protobuf.git", from: "1.20.2"),
4343
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.4"),
4444
.package(url: "https://github.com/apple/swift-metrics.git", from: "2.1.1"),
45-
.package(url: "https://github.com/ashleymills/Reachability.swift", from: "5.1.0")
4645
],
4746
targets: [
4847
.target(name: "OpenTelemetryApi",
@@ -60,7 +59,6 @@ let package = Package(
6059
.target(name: "NetworkStatus",
6160
dependencies: [
6261
"OpenTelemetryApi",
63-
.product(name: "Reachability", package: "Reachability.swift", condition: .when(platforms: [.iOS, .macOS, .tvOS, .macCatalyst, .linux]))
6462
],
6563
path: "Sources/Instrumentation/NetworkStatus",
6664
linkerSettings: [.linkedFramework("CoreTelephony", .when(platforms: [.iOS], configuration: nil))]),
@@ -124,7 +122,6 @@ let package = Package(
124122
.testTarget(name: "NetworkStatusTests",
125123
dependencies: [
126124
"NetworkStatus",
127-
.product(name: "Reachability", package: "Reachability.swift", condition: .when(platforms: [.iOS, .macOS, .tvOS, .macCatalyst, .linux]))
128125
],
129126
path: "Tests/InstrumentationTests/NetworkStatusTests"),
130127
.testTarget(name: "OpenTelemetryApiTests",

Sources/Instrumentation/NetworkStatus/NetworkMonitor.swift

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,79 @@
66
#if !os(watchOS)
77

88
import Foundation
9+
10+
11+
#if swift(>=5.9)
12+
import Network
13+
public class NetworkMonitor : NetworkMonitorProtocol {
14+
let monitor = NWPathMonitor()
15+
var connection : Connection = .unavailable
16+
let monitorQueue = DispatchQueue(label: "OTel-Network-Monitor")
17+
let lock = NSLock()
18+
19+
deinit {
20+
monitor.cancel()
21+
}
22+
23+
public init() throws {
24+
let pathHandler = { (path: NWPath) in
25+
let availableInterfaces = path.availableInterfaces
26+
let wifiInterface = self.getWifiInterface(interfaces: availableInterfaces)
27+
let cellInterface = self.getCellInterface(interfaces: availableInterfaces)
28+
var availableInterface : Connection = .unavailable
29+
if let _ = cellInterface {
30+
availableInterface = .cellular
31+
}
32+
if let _ = wifiInterface {
33+
availableInterface = .wifi
34+
}
35+
self.lock.lock()
36+
switch path.status {
37+
case .requiresConnection, .satisfied:
38+
self.connection = availableInterface
39+
case .unsatisfied:
40+
self.connection = .unavailable
41+
@unknown default:
42+
fatalError()
43+
}
44+
self.lock.unlock()
45+
46+
}
47+
monitor.pathUpdateHandler = pathHandler
48+
monitor.start(queue: monitorQueue)
49+
}
50+
public func getConnection() -> Connection {
51+
lock.lock()
52+
defer {
53+
lock.unlock()
54+
}
55+
return connection
56+
57+
}
58+
59+
func getCellInterface(interfaces: [NWInterface]) -> NWInterface? {
60+
var foundInterface : NWInterface? = nil
61+
interfaces.forEach { interface in
62+
if interface.type == .cellular {
63+
foundInterface = interface
64+
}
65+
}
66+
return foundInterface
67+
}
68+
func getWifiInterface(interfaces: [NWInterface]) -> NWInterface? {
69+
var foundInterface : NWInterface? = nil
70+
interfaces.forEach { interface in
71+
if interface.type == .wifi {
72+
foundInterface = interface
73+
}
74+
}
75+
return foundInterface
76+
}
77+
}
78+
79+
80+
81+
#else
982
import Reachability
1083

1184
public class NetworkMonitor: NetworkMonitorProtocol {
@@ -31,5 +104,5 @@ public class NetworkMonitor: NetworkMonitorProtocol {
31104
}
32105
}
33106
}
34-
107+
#endif
35108
#endif

Tests/InstrumentationTests/NetworkStatusTests/NetworkStatusTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import CoreTelephony
88
import Foundation
99
@testable import NetworkStatus
10-
import Reachability
10+
1111
import XCTest
1212

1313
class MockNetworkMonitor: NetworkMonitorProtocol {

0 commit comments

Comments
 (0)