Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUMM-895 Add single API to configure all endpoints #322

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Datadog/Example/AppConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ struct UITestsAppConfiguration: AppConfiguration {

// If `HTTPServerMock` endpoint is set for Logging, enable the feature and send data to mock server
if let logsEndpoint = serverMockConfiguration?.logsEndpoint {
_ = configuration.set(logsEndpoint: .custom(url: logsEndpoint.absoluteString))
_ = configuration.set(customLogsEndpoint: logsEndpoint)
} else {
_ = configuration.enableLogging(false)
}

// If `HTTPServerMock` endpoint is set for Tracing, enable the feature and send data to mock server
if let tracesEndpoint = serverMockConfiguration?.tracesEndpoint {
_ = configuration.set(tracesEndpoint: .custom(url: tracesEndpoint.absoluteString))
_ = configuration.set(customTracesEndpoint: tracesEndpoint)
} else {
_ = configuration.enableTracing(false)
}

// If `HTTPServerMock` endpoint is set for RUM, enable the feature and send data to mock server
if let rumEndpoint = serverMockConfiguration?.rumEndpoint {
_ = configuration.set(rumEndpoint: .custom(url: rumEndpoint.absoluteString))
_ = configuration.set(customRUMEndpoint: rumEndpoint)
} else {
_ = configuration.enableRUM(false)
}
Expand Down
39 changes: 33 additions & 6 deletions Sources/Datadog/Core/FeaturesConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ extension FeaturesConfiguration {
var rum: RUM?
var urlSessionAutoInstrumentation: URLSessionAutoInstrumentation?

var logsEndpoint = configuration.logsEndpoint
ncreated marked this conversation as resolved.
Show resolved Hide resolved
var tracesEndpoint = configuration.tracesEndpoint
var rumEndpoint = configuration.rumEndpoint

if let datadogEndpoint = configuration.datadogEndpoint {
// If `.set(endpoint:)` API was used, it should override the values
// set by deprecated `.set(<feature>Endpoint:)` APIs.
logsEndpoint = datadogEndpoint.logsEndpoint
tracesEndpoint = datadogEndpoint.tracesEndpoint
rumEndpoint = datadogEndpoint.rumEndpoint
}

if let customLogsEndpoint = configuration.customLogsEndpoint {
// If `.set(cusstomLogsEndpoint:)` API was used, it should override logs endpoint
logsEndpoint = .custom(url: customLogsEndpoint.absoluteString)
}

if let customTracesEndpoint = configuration.customTracesEndpoint {
// If `.set(cusstomLogsEndpoint:)` API was used, it should override traces endpoint
tracesEndpoint = .custom(url: customTracesEndpoint.absoluteString)
}

if let customRUMEndpoint = configuration.customRUMEndpoint {
// If `.set(cusstomLogsEndpoint:)` API was used, it should override RUM endpoint
rumEndpoint = .custom(url: customRUMEndpoint.absoluteString)
}

let common = Common(
applicationName: appContext.bundleName ?? appContext.bundleType.rawValue,
applicationVersion: appContext.bundleVersion ?? "0.0.0",
Expand All @@ -95,7 +122,7 @@ extension FeaturesConfiguration {
logging = Logging(
common: common,
uploadURLWithClientToken: try ifValid(
endpointURLString: configuration.logsEndpoint.url,
endpointURLString: logsEndpoint.url,
clientToken: configuration.clientToken
)
)
Expand All @@ -105,7 +132,7 @@ extension FeaturesConfiguration {
tracing = Tracing(
common: common,
uploadURLWithClientToken: try ifValid(
endpointURLString: configuration.tracesEndpoint.url,
endpointURLString: tracesEndpoint.url,
clientToken: configuration.clientToken
)
)
Expand All @@ -125,7 +152,7 @@ extension FeaturesConfiguration {
rum = RUM(
common: common,
uploadURLWithClientToken: try ifValid(
endpointURLString: configuration.rumEndpoint.url,
endpointURLString: rumEndpoint.url,
clientToken: configuration.clientToken
),
applicationID: rumApplicationID,
Expand All @@ -148,9 +175,9 @@ extension FeaturesConfiguration {
urlSessionAutoInstrumentation = URLSessionAutoInstrumentation(
userDefinedFirstPartyHosts: firstPartyHosts,
sdkInternalURLs: [
configuration.logsEndpoint.url,
configuration.tracesEndpoint.url,
configuration.rumEndpoint.url
logsEndpoint.url,
tracesEndpoint.url,
rumEndpoint.url
],
instrumentTracing: configuration.tracingEnabled,
instrumentRUM: configuration.rumEnabled
Expand Down
96 changes: 96 additions & 0 deletions Sources/Datadog/DatadogConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@ extension Datadog {

/// Datadog SDK configuration.
public struct Configuration {
public enum DatadogEndpoint {
/// US based servers.
/// Sends data to [app.datadoghq.com](https://app.datadoghq.com/).
case us
/// Europe based servers.
/// Sends data to [app.datadoghq.eu](https://app.datadoghq.eu/).
case eu
/// Gov servers.
/// Sends data to [app.ddog-gov.com](https://app.ddog-gov.com/).
case gov

internal var logsEndpoint: LogsEndpoint {
switch self {
case .us: return .us
case .eu: return .eu
case .gov: return .gov
}
}

internal var tracesEndpoint: TracesEndpoint {
switch self {
case .us: return .us
case .eu: return .eu
case .gov: return .gov
}
}

internal var rumEndpoint: RUMEndpoint {
switch self {
case .us: return .us
case .eu: return .eu
case .gov: return .gov
}
}
}

/// Determines the server for uploading logs.
public enum LogsEndpoint {
/// US based servers.
Expand Down Expand Up @@ -94,9 +130,23 @@ extension Datadog {
private(set) var loggingEnabled: Bool
private(set) var tracingEnabled: Bool
private(set) var rumEnabled: Bool

/// If `DatadogEndpoint` is set, it will override `logsEndpoint`, `tracesEndpoint` and `rumEndpoint` values.
private(set) var datadogEndpoint: DatadogEndpoint?
/// If `customLogsEndpoint` is set, it will override logs endpoint value configured with `logsEndpoint` and `DatadogEndpoint`.
private(set) var customLogsEndpoint: URL?
/// If `customTracesEndpoint` is set, it will override traces endpoint value configured with `tracesEndpoint` and `DatadogEndpoint`.
private(set) var customTracesEndpoint: URL?
/// If `customRUMEndpoint` is set, it will override rum endpoint value configured with `rumEndpoint` and `DatadogEndpoint`.
private(set) var customRUMEndpoint: URL?

/// Deprecated value
private(set) var logsEndpoint: LogsEndpoint
/// Deprecated value
private(set) var tracesEndpoint: TracesEndpoint
/// Deprecated value
private(set) var rumEndpoint: RUMEndpoint

private(set) var serviceName: String?
private(set) var firstPartyHosts: Set<String>?
private(set) var rumSessionsSamplingRate: Float
Expand Down Expand Up @@ -146,6 +196,12 @@ extension Datadog {
loggingEnabled: true,
tracingEnabled: true,
rumEnabled: rumApplicationID != nil,
// While `.set(<feature>Endpoint:)` APIs are deprecated, the `datadogEndpoint` default must be `nil`,
// so we know the clear user's intent to override deprecated values.
datadogEndpoint: nil,
customLogsEndpoint: nil,
customTracesEndpoint: nil,
customRUMEndpoint: nil,
logsEndpoint: .us,
tracesEndpoint: .us,
rumEndpoint: .us,
Expand All @@ -157,6 +213,43 @@ extension Datadog {
)
}

/// Sets the Datadog server endpoint where data is sent.
///
/// If set, it will override values set by any of these deprecated APIs:
/// * `set(logsEndpoint:)`
/// * `set(tracesEndpoint:)`
/// * `set(rumEndpoint:)`
///
/// - Parameter endpoint: server endpoint (default value is `.us`)
public func set(endpoint: DatadogEndpoint) -> Builder {
configuration.datadogEndpoint = endpoint
return self
}

/// Sets the custom server endpoint where Logs are sent.
///
/// - Parameter endpoint: server endpoint (not set by default)
public func set(customLogsEndpoint: URL) -> Builder {
configuration.customLogsEndpoint = customLogsEndpoint
return self
}

/// Sets the custom server endpoint where Spans are sent.
///
/// - Parameter customTracesEndpoint: server endpoint (not set by default)
public func set(customTracesEndpoint: URL) -> Builder {
configuration.customTracesEndpoint = customTracesEndpoint
return self
}

/// Sets the custom server endpoint where RUM events are sent.
///
/// - Parameter customRUMEndpoint: server endpoint (not set by default)
public func set(customRUMEndpoint: URL) -> Builder {
configuration.customRUMEndpoint = customRUMEndpoint
return self
}

// MARK: - Logging Configuration

/// Enables or disables the logging feature.
Expand All @@ -179,6 +272,7 @@ extension Datadog {

/// Sets the server endpoint to which logs are sent.
/// - Parameter logsEndpoint: server endpoint (default value is `LogsEndpoint.us`)
@available(*, deprecated, message: "This option is replaced by `set(endpoint:)`. Refer to the new API comment for details.")
public func set(logsEndpoint: LogsEndpoint) -> Builder {
configuration.logsEndpoint = logsEndpoint
return self
Expand All @@ -203,6 +297,7 @@ extension Datadog {

/// Sets the server endpoint to which traces are sent.
/// - Parameter tracesEndpoint: server endpoint (default value is `TracesEndpoint.us` )
@available(*, deprecated, message: "This option is replaced by `set(endpoint:)`. Refer to the new API comment for details.")
public func set(tracesEndpoint: TracesEndpoint) -> Builder {
configuration.tracesEndpoint = tracesEndpoint
return self
Expand Down Expand Up @@ -269,6 +364,7 @@ extension Datadog {

/// Sets the server endpoint to which RUM events are sent.
/// - Parameter rumEndpoint: server endpoint (default value is `RUMEndpoint.us` )
@available(*, deprecated, message: "This option is replaced by `set(endpoint:)`. Refer to the new API comment for details.")
public func set(rumEndpoint: RUMEndpoint) -> Builder {
configuration.rumEndpoint = rumEndpoint
return self
Expand Down
34 changes: 29 additions & 5 deletions Sources/DatadogObjc/DatadogConfiguration+objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
import Foundation
import class Datadog.Datadog

@objcMembers
public class DDEndpoint: NSObject {
internal let sdkEndpoint: Datadog.Configuration.DatadogEndpoint

internal init(sdkEndpoint: Datadog.Configuration.DatadogEndpoint) {
self.sdkEndpoint = sdkEndpoint
}

// MARK: - Public

public static func eu() -> DDEndpoint { .init(sdkEndpoint: .eu) }
public static func us() -> DDEndpoint { .init(sdkEndpoint: .us) }
public static func gov() -> DDEndpoint { .init(sdkEndpoint: .gov) }
}

@objcMembers
public class DDLogsEndpoint: NSObject {
internal let sdkEndpoint: Datadog.Configuration.LogsEndpoint
Expand Down Expand Up @@ -66,11 +81,6 @@ public class DDConfigurationBuilder: NSObject {

// MARK: - Public

@available(*, deprecated, renamed: "set(logsEndpoint:)")
public func set(endpoint: DDLogsEndpoint) {
set(logsEndpoint: endpoint)
}

public func enableLogging(_ enabled: Bool) {
_ = sdkBuilder.enableLogging(enabled)
}
Expand All @@ -79,10 +89,24 @@ public class DDConfigurationBuilder: NSObject {
_ = sdkBuilder.enableTracing(enabled)
}

public func set(endpoint: DDEndpoint) {
_ = sdkBuilder.set(endpoint: endpoint.sdkEndpoint)
}

public func set(customLogsEndpoint: URL) {
_ = sdkBuilder.set(customLogsEndpoint: customLogsEndpoint)
}

public func set(customTracesEndpoint: URL) {
_ = sdkBuilder.set(customTracesEndpoint: customTracesEndpoint)
}

@available(*, deprecated, message: "This option is replaced by `set(endpoint:)`. Refer to the new API comment for details.")
public func set(logsEndpoint: DDLogsEndpoint) {
_ = sdkBuilder.set(logsEndpoint: logsEndpoint.sdkEndpoint)
}

@available(*, deprecated, message: "This option is replaced by `set(endpoint:)`. Refer to the new API comment for details.")
public func set(tracesEndpoint: DDTracesEndpoint) {
_ = sdkBuilder.set(tracesEndpoint: tracesEndpoint.sdkEndpoint)
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/DatadogBenchmarkTests/BenchmarkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class BenchmarkTests: XCTestCase {
appContext: .init(),
configuration: Datadog.Configuration
.builderUsing(rumApplicationID: "rum-123", clientToken: "rum-abc", environment: "benchmarks")
.set(logsEndpoint: .custom(url: anyURL.absoluteString))
.set(tracesEndpoint: .custom(url: anyURL.absoluteString))
.set(rumEndpoint: .custom(url: anyURL.absoluteString))
.set(customLogsEndpoint: anyURL)
.set(customTracesEndpoint: anyURL)
.set(customRUMEndpoint: anyURL)
.build()
)

Expand Down
Loading