Skip to content

Commit 2fa0220

Browse files
committed
Moved initialization to it's own method
1 parent 9709d2f commit 2fa0220

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

Example/SwiftDog.xcodeproj/project.pbxproj

-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDC1AFB9204008FA782 /* Images.xcassets */; };
1515
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; };
1616
607FACEC1AFB9204008FA782 /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACEB1AFB9204008FA782 /* Tests.swift */; };
17-
E5A8012D209BB73F0095DBE6 /* datadog_config.plist in Resources */ = {isa = PBXBuildFile; fileRef = E5A8012C209BB73F0095DBE6 /* datadog_config.plist */; };
18-
E5A8012E209BB73F0095DBE6 /* datadog_config.plist in Resources */ = {isa = PBXBuildFile; fileRef = E5A8012C209BB73F0095DBE6 /* datadog_config.plist */; };
1917
E8F296322510C35C2E6B558B /* Pods_SwiftDog_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 71BA550FCD191F4E7B9A609E /* Pods_SwiftDog_Example.framework */; };
2018
/* End PBXBuildFile section */
2119

@@ -48,7 +46,6 @@
4846
A48A366564E2512B48298CFB /* Pods-SwiftDog_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftDog_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SwiftDog_Example/Pods-SwiftDog_Example.debug.xcconfig"; sourceTree = "<group>"; };
4947
BC0DA096F23D490ECD8AC2CD /* Pods-SwiftDog_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftDog_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-SwiftDog_Tests/Pods-SwiftDog_Tests.release.xcconfig"; sourceTree = "<group>"; };
5048
C1DA4432DBD410137626D5E7 /* Pods-SwiftDog_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftDog_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-SwiftDog_Example/Pods-SwiftDog_Example.release.xcconfig"; sourceTree = "<group>"; };
51-
E5A8012C209BB73F0095DBE6 /* datadog_config.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = datadog_config.plist; sourceTree = "<group>"; };
5249
FB5C666962FBB1C50F2DE957 /* Pods_SwiftDog_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftDog_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5350
/* End PBXFileReference section */
5451

@@ -86,7 +83,6 @@
8683
607FACC71AFB9204008FA782 = {
8784
isa = PBXGroup;
8885
children = (
89-
E5A8012C209BB73F0095DBE6 /* datadog_config.plist */,
9086
607FACF51AFB993E008FA782 /* Podspec Metadata */,
9187
607FACD21AFB9204008FA782 /* Example for SwiftDog */,
9288
607FACE81AFB9204008FA782 /* Tests */,
@@ -257,15 +253,13 @@
257253
607FACDB1AFB9204008FA782 /* Main.storyboard in Resources */,
258254
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */,
259255
607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */,
260-
E5A8012D209BB73F0095DBE6 /* datadog_config.plist in Resources */,
261256
);
262257
runOnlyForDeploymentPostprocessing = 0;
263258
};
264259
607FACE31AFB9204008FA782 /* Resources */ = {
265260
isa = PBXResourcesBuildPhase;
266261
buildActionMask = 2147483647;
267262
files = (
268-
E5A8012E209BB73F0095DBE6 /* datadog_config.plist in Resources */,
269263
);
270264
runOnlyForDeploymentPostprocessing = 0;
271265
};

SwiftDog/Classes/Datadog.swift

+14-22
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,15 @@ public class Datadog: API {
88
private var timer: Timer = Timer()
99
internal let keychain = Keychain(service: "api.datadoghq.com")
1010
public static let dd = Datadog()
11-
private var previous_wifi_sent: UInt32 = 0
12-
private var previous_wifi_received: UInt32 = 0
1311
internal let host = UIDevice.current.identifierForVendor!.uuidString
1412
internal let model = UIDevice.current.model
13+
internal var use_agent = false
1514

1615
@objc private func sendData() {
1716
print("Sending metrics to the Datadog API.")
18-
if let cpu_usage = IOSAgent.current_CPU() {
19-
self.metric.send(metric: "system.cpu.user", points: Float(cpu_usage["user"]!), host: self.host, tags: [], type: .gauge)
20-
self.metric.send(metric: "system.cpu.idle", points: Float(cpu_usage["idle"]!), host: self.host, tags: [], type: .gauge)
21-
self.metric.send(metric: "system.cpu.system", points: Float(cpu_usage["system"]!), host: self.host, tags: [], type: .gauge)
17+
if use_agent {
18+
IOSAgent.send_agent_metrics()
2219
}
23-
if let mem_usage = IOSAgent.current_MEM() {
24-
self.metric.send(metric: "system.mem.used", points: mem_usage, host: self.host, tags: [], type: .gauge)
25-
}
26-
let data_usage_info = IOSAgent.getDataUsage()
27-
if previous_wifi_received != 0 || previous_wifi_sent != 0 {
28-
self.metric.send(metric: "system.net.bytes_sent", points: Float(data_usage_info.wifiReceived - self.previous_wifi_received), host: self.host, tags: [], type: Metric.MetricData.MetricType.rate(Float(interval_seconds)))
29-
self.metric.send(metric: "system.net.bytes_rcvd", points: Float(data_usage_info.wifiSent - self.previous_wifi_sent), host: self.host, tags: [], type: Metric.MetricData.MetricType.rate(Float(interval_seconds)))
30-
}
31-
self.metric.send(metric: "ios.device.battery.level", points: IOSAgent.get_battery_level())
32-
self.previous_wifi_sent = data_usage_info.wifiSent
33-
self.previous_wifi_received = data_usage_info.wifiReceived
34-
3520
do {
3621
try self.metric._send(url: base_url) { (error: Error?) in
3722
print(error!)
@@ -78,17 +63,24 @@ public class Datadog: API {
7863

7964
}
8065

81-
82-
private init() {
66+
public func initialize_api(with agent:Bool=false, default_tags:Bool=false) {
8367
do {
8468
try get_credentials_from_plist()
8569
} catch {
8670
fatalError(error.localizedDescription)
8771
}
88-
self.metric.addTags(tags: ["agent:ios", "model:\(IOSAgent.modelIdentifier())"])
89-
self.event.addTags(tags: ["agent:ios", "model:\(IOSAgent.modelIdentifier())"])
72+
self.use_agent = agent
73+
if default_tags {
74+
self.metric.addTags(tags: ["agent:ios", "model:\(IOSAgent.modelIdentifier())"])
75+
self.event.addTags(tags: ["agent:ios", "model:\(IOSAgent.modelIdentifier())"])
76+
}
9077
self.timer = Timer.scheduledTimer(timeInterval: self.interval_seconds, target: self, selector: #selector(Datadog.sendData), userInfo: nil, repeats: true)
9178
}
9279

9380

81+
private init() {
82+
83+
}
84+
85+
9486
}

SwiftDog/Classes/IOSAgent.swift

+22
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ public struct IOSAgent {
2929
private static let wwanInterfacePrefix = "pdp_ip"
3030
private static let wifiInterfacePrefix = "en"
3131

32+
private static var previous_wifi_sent: UInt32 = 0
33+
private static var previous_wifi_received: UInt32 = 0
34+
35+
internal static func send_agent_metrics() {
36+
if let cpu_usage = IOSAgent.current_CPU() {
37+
Datadog.dd.metric.send(metric: "system.cpu.user", points: Float(cpu_usage["user"]!), host: Datadog.dd.host, tags: [], type: .gauge)
38+
Datadog.dd.metric.send(metric: "system.cpu.idle", points: Float(cpu_usage["idle"]!), host: Datadog.dd.host, tags: [], type: .gauge)
39+
Datadog.dd.metric.send(metric: "system.cpu.system", points: Float(cpu_usage["system"]!), host: Datadog.dd.host, tags: [], type: .gauge)
40+
}
41+
if let mem_usage = IOSAgent.current_MEM() {
42+
Datadog.dd.metric.send(metric: "system.mem.used", points: mem_usage, host: Datadog.dd.host, tags: [], type: .gauge)
43+
}
44+
let data_usage_info = IOSAgent.getDataUsage()
45+
if IOSAgent.previous_wifi_received != 0 || IOSAgent.previous_wifi_sent != 0 {
46+
Datadog.dd.metric.send(metric: "system.net.bytes_sent", points: Float(data_usage_info.wifiReceived - self.previous_wifi_received), host: Datadog.dd.host, tags: [], type: Metric.MetricData.MetricType.rate(Float(Datadog.dd.interval_seconds)))
47+
Datadog.dd.metric.send(metric: "system.net.bytes_rcvd", points: Float(data_usage_info.wifiSent - self.previous_wifi_sent), host: Datadog.dd.host, tags: [], type: Metric.MetricData.MetricType.rate(Float(Datadog.dd.interval_seconds)))
48+
}
49+
Datadog.dd.metric.send(metric: "ios.device.battery.level", points: IOSAgent.get_battery_level())
50+
IOSAgent.previous_wifi_sent = data_usage_info.wifiSent
51+
IOSAgent.previous_wifi_received = data_usage_info.wifiReceived
52+
}
53+
3254
internal static func modelIdentifier() -> String {
3355
if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { return simulatorModelIdentifier }
3456
var sysinfo = utsname()

SwiftDog/Classes/Types.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extension Endpoint {
8080
_ = series.map { (item: EndpointDataType) in
8181
var it = item
8282
it.tags.append(contentsOf: self.tags)
83-
it.host = it.host ?? Datadog.dd.host
83+
// it.host = it.host ?? Datadog.dd.host
8484
endpoint_data.append(it)
8585
}
8686
}

0 commit comments

Comments
 (0)