SwiftDog
这是 Datadog API 的非官方 Swift 库!更多功能即将推出,但目前它支持发送指标和事件!
示例
要运行示例项目,请先克隆仓库,然后从 Example 目录运行 pod install
。
要求
为了运行这个库,您需要创建一个名为 datadog_config.plist
的文件,它包含两个键:api_key
和 app_key
安装
SwiftDog 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 即可
target 'MyApp' do
pod 'SwiftDog', '~> 0.0.1'
end
用法
当前 API 支持发送指标和事件,即将推出更多功能。
目前,尚未实现获取数据,且未来也不在计划之内。
初始化
初始化API有多种方法。
请确保您已添加 datadog_config.plist
,查看需求。
Datadog.initialize_api()
Datadog.initialize_api(default_tags: true)
Datadog.initialize_api(agent: true)
Datadog.initialize_api(agent: true, default_tags: true)
发送指标
Datadog.metric.send(metric: "ios.device.gauge", points: 1)
Datadog.metric.send(metric: "ios.test.event.sent", points: 1, type: .count(1))
Datadog.metric.send(metric: "ios.device.count", points: 1, host: nil, tags: ["device:test_device"], type: .count(1))
您还可以创建对象以直接发送!
let gauge_metric = MetricData(host: nil, tags: ["test:1"], metric_name:"test.metric1", type: MetricData.MetricType.gauge, points: [DataPoint(timestamp: TimeInterval(1525377826.2537289), value: 1)])
let rate_metric = MetricData(host: "device:fun_ios", tags: ["test:2"], metric_name:"test.metric2", type: MetricData.MetricType.rate(10), points: [DataPoint(timestamp: TimeInterval(1525377828.2537289), value: 2)])
let count_metric = MetricData(host: "device:another_device", tags: ["test:3"], metric_name:"test.metric3", type: MetricData.MetricType.count(100), points: [DataPoint(timestamp: TimeInterval(1525377820.2537289), value: 3)])
Datadog.metric.send(series: [gauge_metric, rate_metric, count_metric])
发送事件
Datadog.event.send(title: "This is a test event", text: "We can now send events from an iOS device!")
Datadog.event.send(title: "This is a test event", text: "We can now send events from an iOS device!", tags: ["method:hello"])
Datadog.event.send(title: "This is a test event", text: "We can now send events from an iOS device!", tags: ["method:hello"], date_happened: Data.currentDate())
Datadog.event.send(title: "This is a test event", text: "We can now send events from an iOS device!", tags: ["method:hello"], date_happened: Data.currentDate(), priority: .low)
Datadog.event.send(title: "This is a test event", text: "We can now send events from an iOS device!", tags: ["method:hello"], date_happened: Data.currentDate(), priority: .normal, alert_type: .error)
Datadog.event.send(title: "This is a test event", text: "We can now send events from an iOS device!", tags: ["method:hello"], date_happened: Data.currentDate(), priority: .normal, alert_type: .error, aggregation_key: "host")
Datadog.event.send(title: "This is a test event", text: "We can now send events from an iOS device!", tags: ["method:hello"], date_happened: Data.currentDate(), priority: .normal, alert_type: .error, aggregation_key: "host", source_type_name: "mobile")
与指标类似,您还可以创建并发送事件。
let e: EventData = EventData(host: "ios", tags:[], title: "test title", text: "test text", date_happened: 1525412871, priority: .normal, alert_type: .info, aggregation_key: nil, source_type_name: nil)
Datadog.event.send(series: [e])
作者
Jacob Aronoff, [email protected]
许可协议
SwiftDog 在 Apache 许可下可用。有关更多信息,请参阅 LICENSE 文件。