From 11dc8035d6f144e9f97b0189861b46fd40a0b3c9 Mon Sep 17 00:00:00 2001 From: hannessolo Date: Tue, 15 Sep 2020 08:57:57 +0200 Subject: [PATCH 1/3] Add option to switch to EU server --- Sources/DataDogLog/DataDogLogHandler.swift | 7 +++++-- Sources/DataDogLog/DataDogSession.swift | 8 +++---- Sources/DataDogLog/Region.swift | 23 +++++++++++++++++++++ Tests/DataDogLogTests/DataDogLogTests.swift | 14 ++++++++++++- 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 Sources/DataDogLog/Region.swift diff --git a/Sources/DataDogLog/DataDogLogHandler.swift b/Sources/DataDogLog/DataDogLogHandler.swift index 2a2d446..e25ac37 100644 --- a/Sources/DataDogLog/DataDogLogHandler.swift +++ b/Sources/DataDogLog/DataDogLogHandler.swift @@ -10,13 +10,16 @@ public struct DataDogLogHandler: LogHandler { public var label: String public var hostname: String? internal let key: String + // Region for URL + internal let region: Region var session: Session = URLSession.shared - public init(label: String, key: String, hostname: String? = nil) { + public init(label: String, key: String, hostname: String? = nil, region: Region = .US) { self.label = label self.key = key self.hostname = hostname + self.region = region } public func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) { @@ -26,7 +29,7 @@ public struct DataDogLogHandler: LogHandler { let ddMessage = Message(level: level, message: "\(message)") let log = Log(ddsource: label, ddtags: "\(mergedMetadata.prettified.map { "\($0)" } ?? "")", hostname: self.hostname ?? "", message: "\(ddMessage)") - session.send(log, key: key) { result in + session.send(log, key: key, region: region) { result in if case .failure(let message) = result { debugPrint(message) } diff --git a/Sources/DataDogLog/DataDogSession.swift b/Sources/DataDogLog/DataDogSession.swift index a250ac7..d948f8a 100644 --- a/Sources/DataDogLog/DataDogSession.swift +++ b/Sources/DataDogLog/DataDogSession.swift @@ -7,20 +7,18 @@ import Logging typealias StatusCode = Int protocol Session { - func send(_ log: Log, key: String, handler: @escaping (Result) -> ()) + func send(_ log: Log, key: String, region: Region, handler: @escaping (Result) -> ()) } extension String: Error {} extension Optional: Error where Wrapped == String {} extension URLSession: Session { - static let url = URL(string: "https://http-intake.logs.datadoghq.com/v1/input")! - - func send(_ log: Log, key: String, handler: @escaping (Result) -> ()) { + func send(_ log: Log, key: String, region: Region, handler: @escaping (Result) -> ()) { do { let data = try JSONEncoder().encode(log) - var request = URLRequest(url: URLSession.url) + var request = URLRequest(url: region.getURL()) request.httpMethod = "POST" request.httpBody = data request.addValue("application/json", forHTTPHeaderField: "Content-Type") diff --git a/Sources/DataDogLog/Region.swift b/Sources/DataDogLog/Region.swift new file mode 100644 index 0000000..018dcca --- /dev/null +++ b/Sources/DataDogLog/Region.swift @@ -0,0 +1,23 @@ +// +// File.swift +// +// +// Created by Hannes Hertach on 15.09.20. +// + +import Foundation + +public enum Region { + case EU, US +} + +extension Region { + func getURL() -> URL { + switch (self) { + case .EU: + return URL(string: "https://http-intake.logs.datadoghq.eu/v1/input")! + case .US: + return URL(string: "https://http-intake.logs.datadoghq.com/v1/input")! + } + } +} diff --git a/Tests/DataDogLogTests/DataDogLogTests.swift b/Tests/DataDogLogTests/DataDogLogTests.swift index 87cd012..d4bf82f 100644 --- a/Tests/DataDogLogTests/DataDogLogTests.swift +++ b/Tests/DataDogLogTests/DataDogLogTests.swift @@ -7,7 +7,7 @@ class TestSession: Session { var hostname: String? var message: String? - func send(_ log: Log, key: String, handler: @escaping (Result) -> ()) { + func send(_ log: Log, key: String, region: Region, handler: @escaping (Result) -> ()) { source = log.ddsource tags = log.ddtags hostname = log.hostname @@ -37,6 +37,18 @@ final class DataDogLogTests: XCTestCase { XCTAssertEqual(expectedHostname, session.hostname, "Expected \(expectedHostname), result was \(String(describing: session.hostname))") XCTAssertEqual(expectedTags, session.tags, "Expected \(expectedTags), result was \(String(describing: session.tags))") } + + func testRegionURL() { + let euLogHandler = DataDogLogHandler(label: "test", key: "test", region: .EU) + let usLogHandler = DataDogLogHandler(label: "test", key: "test", region: .US) + let defaultLogHandler = DataDogLogHandler(label: "test", key: "test") + + XCTAssert(euLogHandler.region == .EU) + XCTAssert(euLogHandler.region.getURL().absoluteString == "https://http-intake.logs.datadoghq.eu/v1/input") + XCTAssert(usLogHandler.region == .US) + XCTAssert(usLogHandler.region.getURL().absoluteString == "https://http-intake.logs.datadoghq.com/v1/input") + XCTAssert(defaultLogHandler.region == .US) + } static var allTests = [ ("testLog", testLog), From c368d6fc47a96457349ba2cdb376f6ba8b89f27e Mon Sep 17 00:00:00 2001 From: hannessolo Date: Tue, 15 Sep 2020 09:16:11 +0200 Subject: [PATCH 2/3] Remove author from file header --- Sources/DataDogLog/Region.swift | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Sources/DataDogLog/Region.swift b/Sources/DataDogLog/Region.swift index 018dcca..dc5d2d2 100644 --- a/Sources/DataDogLog/Region.swift +++ b/Sources/DataDogLog/Region.swift @@ -1,10 +1,3 @@ -// -// File.swift -// -// -// Created by Hannes Hertach on 15.09.20. -// - import Foundation public enum Region { From 38665d04f1139cfea8cdfe7fa41be70aaec3bde9 Mon Sep 17 00:00:00 2001 From: hannessolo Date: Tue, 15 Sep 2020 09:21:27 +0200 Subject: [PATCH 3/3] Update readme to reflect changes to region selection --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index ff026c0..f268ae6 100644 --- a/README.md +++ b/README.md @@ -60,3 +60,11 @@ This call will send the following payload to Datadog: "ddtags": "callsite:testLog():39,foo:bar,request-id:abc-123" } ``` + +### Select Region + +The Datadog API uses a different URL in the EU region compared to the US. If your account was created using Datadog EU, you need to set the `region` option when initializing `DataDogLogHandler`: + +```swift +DataDogLogHandler(label: $0, key: "xxx", hostname: "hostname", region: .EU) +```