Skip to content

Commit

Permalink
Fix iOS support
Browse files Browse the repository at this point in the history
  • Loading branch information
lunij committed Dec 21, 2022
1 parent 6fc9920 commit 2d618fa
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Examples/Datadog Sample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import DatadogExporter
import Foundation
import OpenTelemetryApi
import OpenTelemetrySdk
#if targetEnvironment(macCatalyst)
#if !os(macOS)
import UIKit
#endif

Expand All @@ -26,10 +26,10 @@ var instrumentationScopeInfo = InstrumentationScopeInfo(name: instrumentationSco
var tracer: TracerSdk
tracer = OpenTelemetrySDK.instance.tracerProvider.get(instrumentationName: instrumentationScopeName, instrumentationVersion: instrumentationScopeVersion) as! TracerSdk

#if targetEnvironment(macCatalyst)
let hostName = UIDevice.current.name
#else
#if os(macOS)
let hostName = Host.current().localizedName
#else
let hostName = UIDevice.current.name
#endif

let exporterConfiguration = ExporterConfiguration(
Expand Down
2 changes: 1 addition & 1 deletion Examples/Simple Exporter/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ let spanExporter = MultiSpanExporter(spanExporters: [jaegerExporter, stdoutExpor
let spanProcessor = SimpleSpanProcessor(spanExporter: spanExporter)
OpenTelemetrySDK.instance.tracerProvider.addSpanProcessor(spanProcessor)

if #available(macOS 10.14, *) {
if #available(iOS 12.0, macOS 10.14, *) {
OpenTelemetrySDK.instance.tracerProvider.addSpanProcessor(SignPostIntegration())
}

Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
"version": "0.5.2"
}
},
{
"package": "Reachability",
"repositoryURL": "https://github.com/ashleymills/Reachability.swift",
"state": {
"branch": null,
"revision": "c01bbdf2d633cf049ae1ed1a68a2020a8bda32e2",
"version": "5.1.0"
}
},
{
"package": "swift-atomics",
"repositoryURL": "https://github.com/apple/swift-atomics.git",
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ let package = Package(
.package(name: "swift-nio", url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
.package(name: "grpc-swift", url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0"),
.package(name: "swift-metrics", url: "https://github.com/apple/swift-metrics.git", from: "2.1.1"),
.package(name: "Reachability.swift", url: "https://github.com/ashleymills/Reachability.swift", from: "5.1.0")
],
targets: [
.target(name: "OpenTelemetryApi",
Expand Down Expand Up @@ -98,7 +99,7 @@ let package = Package(
dependencies: ["OpenTelemetrySdk"],
path: "Sources/Exporters/Persistence"),
.testTarget(name: "NetworkStatusTests",
dependencies: ["NetworkStatus"],
dependencies: ["NetworkStatus", .product(name: "Reachability", package: "Reachability.swift")],
path: "Tests/InstrumentationTests/NetworkStatusTests"),
.testTarget(name: "OpenTelemetryApiTests",
dependencies: ["OpenTelemetryApi"],
Expand Down
15 changes: 14 additions & 1 deletion Tests/ExportersTests/DatadogExporter/Helpers/DeviceMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,29 @@ class UIDeviceMock: UIDevice {
init(
model: String = .mockAny(),
systemName: String = .mockAny(),
systemVersion: String = .mockAny()
systemVersion: String = .mockAny(),
isBatteryMonitoringEnabled: Bool = .mockAny(),
batteryState: UIDevice.BatteryState = .unknown,
batteryLevel: Float = 0
) {
self._model = model
self._systemName = systemName
self._systemVersion = systemVersion
self._isBatteryMonitoringEnabled = isBatteryMonitoringEnabled
self._batteryState = batteryState
self._batteryLevel = batteryLevel
}

override var model: String { _model }
override var systemName: String { _systemName }
override var systemVersion: String { "mock system version" }
override var batteryState: UIDevice.BatteryState { _batteryState }
override var batteryLevel: Float { _batteryLevel }

override var isBatteryMonitoringEnabled: Bool {
get { _isBatteryMonitoringEnabled }
set { _isBatteryMonitoringEnabled = newValue }
}
}

#endif
10 changes: 6 additions & 4 deletions Tests/ExportersTests/DatadogExporter/Utils/DeviceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import XCTest

#if !os(macOS)
#if os(iOS)
import UIKit
#else
import Foundation
Expand All @@ -19,9 +19,9 @@ class DeviceTests: XCTestCase {
XCTAssertNotNil(Device.current)
}

#if !os(macOS) && !targetEnvironment(macCatalyst)
#if os(iOS) && !targetEnvironment(macCatalyst)
func testWhenRunningOnMobile_itUsesUIDeviceInfo() {
let uiDevice = DeviceMock(
let uiDevice = UIDeviceMock(
model: "model mock",
systemName: "system name mock",
systemVersion: "system version mock"
Expand All @@ -32,5 +32,7 @@ class DeviceTests: XCTestCase {
XCTAssertEqual(device.osName, uiDevice.systemName)
XCTAssertEqual(device.osVersion, uiDevice.systemVersion)
}
#endif // os(iOS) && !targetEnvironment(macCatalyst)

class ProcessInfoMock: ProcessInfo {}
#endif
}

0 comments on commit 2d618fa

Please sign in to comment.