Skip to content

Commit 13331bf

Browse files
author
Ignacio Bonafonte
committed
Add support for macCatalyst environment
1 parent 90bcf2e commit 13331bf

File tree

10 files changed

+126
-112
lines changed

10 files changed

+126
-112
lines changed

Examples/Datadog Sample/main.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import DatadogExporter
77
import Foundation
88
import OpenTelemetryApi
99
import OpenTelemetrySdk
10+
#if targetEnvironment(macCatalyst)
11+
import UIKit
12+
#endif
1013

1114
let clientToken = ""
1215
let apikey = ""
@@ -21,6 +24,12 @@ var instrumentationLibraryInfo = InstrumentationLibraryInfo(name: instrumentatio
2124
var tracer: TracerSdk
2225
tracer = OpenTelemetrySDK.instance.tracerProvider.get(instrumentationName: instrumentationLibraryName, instrumentationVersion: instrumentationLibraryVersion) as! TracerSdk
2326

27+
#if targetEnvironment(macCatalyst)
28+
let hostName = UIDevice.current.name
29+
#else
30+
let hostName = Host.current().localizedName
31+
#endif
32+
2433
let exporterConfiguration = ExporterConfiguration(
2534
serviceName: "Opentelemetry exporter Example",
2635
resource: "Opentelemetry exporter",
@@ -32,7 +41,7 @@ let exporterConfiguration = ExporterConfiguration(
3241
endpoint: Endpoint.us,
3342
uploadCondition: { true },
3443
performancePreset: .instantDataDelivery,
35-
hostName: Host.current().localizedName
44+
hostName: hostName
3645
)
3746

3847
let datadogExporter = try! DatadogExporter(config: exporterConfiguration)

Sources/Instrumentation/NetworkStatus/NetworkMonitorProtocol.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Foundation
88
public enum Connection {
99
case unavailable, wifi, cellular
1010
}
11+
1112
public protocol NetworkMonitorProtocol {
12-
func getConnection() -> Connection;
13+
func getConnection() -> Connection
1314
}

Sources/Instrumentation/NetworkStatus/NetworkStatus.swift

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,76 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#if os(iOS)
6+
#if os(iOS) && !targetEnvironment(macCatalyst)
7+
import CoreTelephony
8+
import Foundation
9+
import Network
10+
import Reachability
711

8-
import CoreTelephony
9-
import Foundation
10-
import Network
11-
import Reachability
12-
13-
public class NetworkStatus {
14-
public private(set) var networkInfo: CTTelephonyNetworkInfo
15-
public private(set) var networkMonitor: NetworkMonitorProtocol
16-
public convenience init() throws {
17-
self.init(with: try NetworkMonitor())
18-
}
12+
public class NetworkStatus {
13+
public private(set) var networkInfo: CTTelephonyNetworkInfo
14+
public private(set) var networkMonitor: NetworkMonitorProtocol
15+
public convenience init() throws {
16+
self.init(with: try NetworkMonitor())
17+
}
1918

20-
public init(with monitor: NetworkMonitorProtocol, info: CTTelephonyNetworkInfo = CTTelephonyNetworkInfo()) {
21-
networkMonitor = monitor
22-
networkInfo = info
23-
}
19+
public init(with monitor: NetworkMonitorProtocol, info: CTTelephonyNetworkInfo = CTTelephonyNetworkInfo()) {
20+
networkMonitor = monitor
21+
networkInfo = info
22+
}
2423

25-
public func status() -> (String, String?, CTCarrier?) {
26-
switch networkMonitor.getConnection() {
27-
case .wifi:
28-
return ("wifi", nil, nil)
29-
case .cellular:
30-
if #available(iOS 13.0, *) {
31-
if let serviceId = networkInfo.dataServiceIdentifier, let value = networkInfo.serviceCurrentRadioAccessTechnology?[serviceId] {
32-
return ("cell", simpleConnectionName(connectionType: value), networkInfo.serviceSubscriberCellularProviders?[networkInfo.dataServiceIdentifier!])
33-
}
34-
} else {
35-
if let radioType = networkInfo.currentRadioAccessTechnology {
36-
return ("cell", simpleConnectionName(connectionType: radioType), networkInfo.subscriberCellularProvider)
37-
}
24+
public func status() -> (String, String?, CTCarrier?) {
25+
switch networkMonitor.getConnection() {
26+
case .wifi:
27+
return ("wifi", nil, nil)
28+
case .cellular:
29+
if #available(iOS 13.0, *) {
30+
if let serviceId = networkInfo.dataServiceIdentifier, let value = networkInfo.serviceCurrentRadioAccessTechnology?[serviceId] {
31+
return ("cell", simpleConnectionName(connectionType: value), networkInfo.serviceSubscriberCellularProviders?[networkInfo.dataServiceIdentifier!])
32+
}
33+
} else {
34+
if let radioType = networkInfo.currentRadioAccessTechnology {
35+
return ("cell", simpleConnectionName(connectionType: radioType), networkInfo.subscriberCellularProvider)
3836
}
39-
return ("cell", "unknown", nil)
40-
case .unavailable:
41-
return ("unavailable", nil, nil)
4237
}
38+
return ("cell", "unknown", nil)
39+
case .unavailable:
40+
return ("unavailable", nil, nil)
4341
}
42+
}
4443

45-
func simpleConnectionName(connectionType: String) -> String {
46-
switch connectionType {
47-
case "CTRadioAccessTechnologyEdge":
48-
return "EDGE"
49-
case "CTRadioAccessTechnologyCDMA1x":
50-
return "CDMA"
51-
case "CTRadioAccessTechnologyGPRS":
52-
return "GPRS"
53-
case "CTRadioAccessTechnologyWCDMA":
54-
return "WCDMA"
55-
case "CTRadioAccessTechnologyHSDPA":
56-
return "HSDPA"
57-
case "CTRadioAccessTechnologyHSUPA":
58-
return "HSUPA"
59-
case "CTRadioAccessTechnologyCDMAEVDORev0":
60-
return "EVDO_0"
61-
case "CTRadioAccessTechnologyCDMAEVDORevA":
62-
return "EVDO_A"
63-
case "CTRadioAccessTechnologyCDMAEVDORevB":
64-
return "EVDO_B"
65-
case "CTRadioAccessTechnologyeHRPD":
66-
return "HRPD"
67-
case "CTRadioAccessTechnologyLTE":
68-
return "LTE"
69-
case "CTRadioAccessTechnologyNRNSA":
70-
return "NRNSA"
71-
case "CTRadioAccessTechnologyNR":
72-
return "NR"
73-
default:
74-
return "unknown"
75-
}
44+
func simpleConnectionName(connectionType: String) -> String {
45+
switch connectionType {
46+
case "CTRadioAccessTechnologyEdge":
47+
return "EDGE"
48+
case "CTRadioAccessTechnologyCDMA1x":
49+
return "CDMA"
50+
case "CTRadioAccessTechnologyGPRS":
51+
return "GPRS"
52+
case "CTRadioAccessTechnologyWCDMA":
53+
return "WCDMA"
54+
case "CTRadioAccessTechnologyHSDPA":
55+
return "HSDPA"
56+
case "CTRadioAccessTechnologyHSUPA":
57+
return "HSUPA"
58+
case "CTRadioAccessTechnologyCDMAEVDORev0":
59+
return "EVDO_0"
60+
case "CTRadioAccessTechnologyCDMAEVDORevA":
61+
return "EVDO_A"
62+
case "CTRadioAccessTechnologyCDMAEVDORevB":
63+
return "EVDO_B"
64+
case "CTRadioAccessTechnologyeHRPD":
65+
return "HRPD"
66+
case "CTRadioAccessTechnologyLTE":
67+
return "LTE"
68+
case "CTRadioAccessTechnologyNRNSA":
69+
return "NRNSA"
70+
case "CTRadioAccessTechnologyNR":
71+
return "NR"
72+
default:
73+
return "unknown"
7674
}
7775
}
76+
}
7877

79-
#endif // os(iOS)
78+
#endif // os(iOS) && !targetEnvironment(macCatalyst)

Sources/Instrumentation/NetworkStatus/NetworkStatusInjector.swift

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,45 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#if os(iOS)
7-
import CoreTelephony
8-
import Foundation
9-
import Network
10-
import OpenTelemetryApi
11-
public class NetworkStatusInjector {
12-
private var netstat: NetworkStatus
13-
14-
public init(netstat: NetworkStatus) {
15-
self.netstat = netstat
16-
}
6+
#if os(iOS) && !targetEnvironment(macCatalyst)
177

18-
public func inject(span: Span) {
19-
let (type, subtype, carrier) = netstat.status()
20-
span.setAttribute(key: "net.host.connection.type", value: AttributeValue.string(type))
8+
import CoreTelephony
9+
import Foundation
10+
import Network
11+
import OpenTelemetryApi
12+
public class NetworkStatusInjector {
13+
private var netstat: NetworkStatus
2114

22-
if let subtype: String = subtype {
23-
span.setAttribute(key: "net.host.connection.subtype", value: AttributeValue.string(subtype))
24-
}
15+
public init(netstat: NetworkStatus) {
16+
self.netstat = netstat
17+
}
18+
19+
public func inject(span: Span) {
20+
let (type, subtype, carrier) = netstat.status()
21+
span.setAttribute(key: "net.host.connection.type", value: AttributeValue.string(type))
22+
23+
if let subtype: String = subtype {
24+
span.setAttribute(key: "net.host.connection.subtype", value: AttributeValue.string(subtype))
25+
}
2526

26-
if let carrierInfo: CTCarrier = carrier {
27-
if let carrierName = carrierInfo.carrierName {
28-
span.setAttribute(key: "net.host.carrier.name", value: AttributeValue.string(carrierName))
29-
}
27+
if let carrierInfo: CTCarrier = carrier {
28+
if let carrierName = carrierInfo.carrierName {
29+
span.setAttribute(key: "net.host.carrier.name", value: AttributeValue.string(carrierName))
30+
}
3031

31-
if let isoCountryCode = carrierInfo.isoCountryCode {
32-
span.setAttribute(key: "net.host.carrier.icc", value: AttributeValue.string(isoCountryCode))
33-
}
32+
if let isoCountryCode = carrierInfo.isoCountryCode {
33+
span.setAttribute(key: "net.host.carrier.icc", value: AttributeValue.string(isoCountryCode))
34+
}
3435

35-
if let mobileCountryCode = carrierInfo.mobileCountryCode {
36-
span.setAttribute(key: "net.host.carrier.mcc", value: AttributeValue.string(mobileCountryCode))
37-
}
36+
if let mobileCountryCode = carrierInfo.mobileCountryCode {
37+
span.setAttribute(key: "net.host.carrier.mcc", value: AttributeValue.string(mobileCountryCode))
38+
}
3839

39-
if let mobileNetworkCode = carrierInfo.mobileNetworkCode {
40-
span.setAttribute(key: "net.host.carrier.mnc", value: AttributeValue.string(mobileNetworkCode))
41-
}
40+
if let mobileNetworkCode = carrierInfo.mobileNetworkCode {
41+
span.setAttribute(key: "net.host.carrier.mnc", value: AttributeValue.string(mobileNetworkCode))
4242
}
4343
}
4444
}
45-
#endif
45+
}
46+
47+
#endif // os(iOS) && !targetEnvironment(macCatalyst)

Sources/Instrumentation/NetworkStatus/NetworkStatusProtocol.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#if os(iOS)
7-
import Foundation
6+
#if os(iOS) && !targetEnvironment(macCatalyst)
7+
88
import CoreTelephony
9+
import Foundation
910

1011
public protocol NetworkStatusProtocol {
11-
var networkMonitor : NetworkMonitorProtocol { get }
12+
var networkMonitor: NetworkMonitorProtocol { get }
1213
func getStatus() -> (String, CTCarrier?)
1314
}
14-
#endif
15+
#endif // os(iOS) && !targetEnvironment(macCatalyst)

Sources/Instrumentation/URLSession/URLSessionLogger.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import Foundation
77
import OpenTelemetryApi
88
import OpenTelemetrySdk
99
import os.log
10-
#if os(iOS)
10+
#if os(iOS) && !targetEnvironment(macCatalyst)
11+
1112
import NetworkStatus
12-
#endif // os(iOS)
13+
#endif // os(iOS) && !targetEnvironment(macCatalyst)
1314

1415
class URLSessionLogger {
1516
static var runningSpans = [String: Span]()
1617
static var runningSpansQueue = DispatchQueue(label: "io.opentelemetry.URLSessionLogger")
17-
#if os(iOS)
18+
#if os(iOS) && !targetEnvironment(macCatalyst)
19+
1820
static var netstatInjector: NetworkStatusInjector? = { () -> NetworkStatusInjector? in
1921
do {
2022
let netstats = try NetworkStatus()
@@ -29,7 +31,8 @@ class URLSessionLogger {
2931
return nil
3032
}
3133
}()
32-
#endif // os(iOS)
34+
#endif // os(iOS) && !targetEnvironment(macCatalyst)
35+
3336
/// 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
3437
@discardableResult static func processAndLogRequest(_ request: URLRequest, sessionTaskId: String, instrumentation: URLSessionInstrumentation, shouldInjectHeaders: Bool) -> URLRequest? {
3538
guard instrumentation.configuration.shouldInstrument?(request) ?? true else {
@@ -80,7 +83,7 @@ class URLSessionLogger {
8083
returnRequest = instrumentedRequest(for: request, span: span, instrumentation: instrumentation)
8184
}
8285

83-
#if os(iOS)
86+
#if os(iOS) && !targetEnvironment(macCatalyst)
8487
if let injector = netstatInjector {
8588
injector.inject(span: span)
8689
}

Tests/ExportersTests/DatadogExporter/Helpers/DeviceMock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#if !os(macOS)
6+
#if !os(macOS) && !targetEnvironment(macCatalyst)
77
import UIKit
88

99
class UIDeviceMock: UIDevice {

Tests/ExportersTests/DatadogExporter/Utils/DeviceTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DeviceTests: XCTestCase {
1919
XCTAssertNotNil(Device.current)
2020
}
2121

22-
#if !os(macOS)
22+
#if !os(macOS) && !targetEnvironment(macCatalyst)
2323
func testWhenRunningOnMobile_itUsesUIDeviceInfo() {
2424
let uiDevice = DeviceMock(
2525
model: "model mock",
@@ -32,5 +32,5 @@ class DeviceTests: XCTestCase {
3232
XCTAssertEqual(device.osName, uiDevice.systemName)
3333
XCTAssertEqual(device.osVersion, uiDevice.systemVersion)
3434
}
35-
#endif
35+
#endif // os(iOS) && !targetEnvironment(macCatalyst)
3636
}

Tests/InstrumentationTests/NetworkStatusTests/NetworkStatusTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#if os(iOS)
6+
#if os(iOS) && !targetEnvironment(macCatalyst)
77
import CoreTelephony
88
import Foundation
99
@testable import NetworkStatus
@@ -140,4 +140,4 @@
140140
}
141141
}
142142

143-
#endif // os(iOS)
143+
#endif // os(iOS) && !targetEnvironment(macCatalyst)

Tests/InstrumentationTests/SDKResourceExtensionTests/ResourcePropagationTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class ResourcePropagationTests : XCTestCase {
1313
func testPropagation() {
1414
let defaultResource = Resource()
1515
let appProvider = ApplicationResourceProvider(source: ApplicationDataSource())
16-
let telemetryProvider = TelemetryResourceProvider(source: TelemetryDataSource())
1716
let resultResource = DefaultResources().get()
1817

1918
let defaultValue = defaultResource.attributes[ResourceAttributes.serviceName.rawValue]?.description

0 commit comments

Comments
 (0)