Skip to content

Commit

Permalink
Merge pull request #3 from hannessolo/feature/eu-region
Browse files Browse the repository at this point in the history
Add support for EU region
  • Loading branch information
jagreenwood authored Sep 16, 2020
2 parents 59b7799 + 38665d0 commit 9872cc2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
7 changes: 5 additions & 2 deletions Sources/DataDogLog/DataDogLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
8 changes: 3 additions & 5 deletions Sources/DataDogLog/DataDogSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ import Logging
typealias StatusCode = Int

protocol Session {
func send(_ log: Log, key: String, handler: @escaping (Result<StatusCode, Error>) -> ())
func send(_ log: Log, key: String, region: Region, handler: @escaping (Result<StatusCode, Error>) -> ())
}

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<StatusCode, Error>) -> ()) {
func send(_ log: Log, key: String, region: Region, handler: @escaping (Result<StatusCode, Error>) -> ()) {
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")
Expand Down
16 changes: 16 additions & 0 deletions Sources/DataDogLog/Region.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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")!
}
}
}
14 changes: 13 additions & 1 deletion Tests/DataDogLogTests/DataDogLogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TestSession: Session {
var hostname: String?
var message: String?

func send(_ log: Log, key: String, handler: @escaping (Result<StatusCode, Error>) -> ()) {
func send(_ log: Log, key: String, region: Region, handler: @escaping (Result<StatusCode, Error>) -> ()) {
source = log.ddsource
tags = log.ddtags
hostname = log.hostname
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 9872cc2

Please sign in to comment.