Skip to content

Commit

Permalink
Merge pull request #1358 from DataDog/maxep/RUMM-3387/align-public-api
Browse files Browse the repository at this point in the history
RUMM3387 Align v2 Public API
  • Loading branch information
maxep authored Jul 5, 2023
2 parents 083682f + adfc9d1 commit 4726fe2
Show file tree
Hide file tree
Showing 57 changed files with 312 additions and 257 deletions.
2 changes: 1 addition & 1 deletion Datadog/Example/Debugging/DebugLoggingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class DebugLoggingViewController: UIViewController {
return Logger.create(
with: Logger.Configuration(
name: "stress-logger-\(index)",
sendNetworkInfo: true
networkInfoEnabled: true
)
)
}
Expand Down
6 changes: 3 additions & 3 deletions Datadog/Example/ExampleAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ExampleAppDelegate: UIResponder, UIApplicationDelegate {
logger = Logger.create(
with: Logger.Configuration(
name: "logger-name",
sendNetworkInfo: true,
networkInfoEnabled: true,
consoleLogFormat: .shortWith(prefix: "[iOS App] ")
)
)
Expand Down Expand Up @@ -73,7 +73,7 @@ class ExampleAppDelegate: UIResponder, UIApplicationDelegate {
// Enable Trace
Trace.enable(
with: Trace.Configuration(
sendNetworkInfo: true,
networkInfoEnabled: true,
customEndpoint: Environment.readCustomTraceURL()
)
)
Expand All @@ -82,7 +82,7 @@ class ExampleAppDelegate: UIResponder, UIApplicationDelegate {
RUM.enable(
with: RUM.Configuration(
applicationID: Environment.readRUMApplicationID(),
backgroundEventsTracking: true,
trackBackgroundEvents: true,
customEndpoint: Environment.readCustomRUMURL(),
telemetrySampleRate: 100
)
Expand Down
77 changes: 63 additions & 14 deletions DatadogCore/Sources/Datadog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ import DatadogInternal

/// An entry point to Datadog SDK.
///
/// Initialize the core instance of the Datadog SDK prior to enabling any Features.
/// Initialize the core instance of the Datadog SDK prior to enabling any Product.
///
/// Datadog.initialize(
/// with: Datadog.Configuration(clientToken: "<client token>", env: "<environment>"),
/// trackingConsent: .pending
/// )
/// ```swift
/// Datadog.initialize(
/// with: Datadog.Configuration(clientToken: "<client token>", env: "<environment>"),
/// trackingConsent: .pending
/// )
/// ```
///
/// Once Datadog SDK is initialized, you can enable Features, such as RUM:
/// Once Datadog SDK is initialized, you can enable products, such as RUM:
///
/// RUM.enable(
/// with: RUM.Configuration(applicationID: "<application>")
/// )
/// ```swift
/// RUM.enable(
/// with: RUM.Configuration(applicationID: "<application>")
/// )
/// ```
///
public struct Datadog {
/// Configuration of Datadog SDK.
Expand Down Expand Up @@ -173,8 +177,10 @@ public struct Datadog {
public static var verbosityLevel: CoreLoggerLevel? = nil

/// Returns `true` if the Datadog SDK is already initialized, `false` otherwise.
public static var isInitialized: Bool {
return CoreRegistry.default is DatadogCore
///
/// - Parameter name: The name of the SDK instance to verify.
public static func isInitialized(instanceName name: String = CoreRegistry.defaultInstanceName) -> Bool {
CoreRegistry.instance(named: name) is DatadogCore
}

/// Returns the Datadog SDK instance for the given name.
Expand All @@ -186,7 +192,9 @@ public struct Datadog {
}

/// Sets current user information.
///
/// Those will be added to logs, traces and RUM events automatically.
///
/// - Parameters:
/// - id: User ID, if any
/// - name: Name representing the user, if any
Expand All @@ -212,6 +220,7 @@ public struct Datadog {
///
/// This extra info will be added to already existing extra info that is added
/// to logs traces and RUM events automatically.
///
/// - Parameters:
/// - extraInfo: User's additionall custom attributes
public static func addUserExtraInfo(
Expand All @@ -237,37 +246,75 @@ public struct Datadog {

/// Initializes the Datadog SDK.
///
/// You **must** initialize the core instance of the Datadog SDK prior to enabling any Product.
///
/// ```swift
/// Datadog.initialize(
/// with: Datadog.Configuration(clientToken: "<client token>", env: "<environment>"),
/// trackingConsent: .pending
/// )
/// ```
///
/// Once Datadog SDK is initialized, you can enable products, such as RUM:
///
/// ```swift
/// RUM.enable(
/// with: RUM.Configuration(applicationID: "<application>")
/// )
/// ```
/// It is possible to initialize multiple instances of the SDK, associating them with a name.
/// Many methods of the SDK can optionally take a SDK instance as an argument. If not provided,
/// the call will be associated with the default (nameless) SDK instance.
///
/// To use a secondary instance of the SDK, provide a name to the ``initialize`` method
/// and use the returned instance to enable products:
///
/// ```swift
/// let core = Datadog.initialize(
/// with: Datadog.Configuration(clientToken: "<client token>", env: "<environment>"),
/// trackingConsent: .pending,
/// instanceName: "my-instance"
/// )
///
/// RUM.enable(
/// with: RUM.Configuration(applicationID: "<application>"),
/// in: core
/// )
/// ```
///
/// - Parameters:
/// - configuration: the SDK configuration.
/// - trackingConsent: the initial state of the Data Tracking Consent given by the user of the app.
/// - instanceName: The core instance name. This value will be used for data persistency and should be
/// stable between application runs.
@discardableResult
public static func initialize(
with configuration: Configuration,
trackingConsent: TrackingConsent,
instanceName: String = CoreRegistry.defaultInstanceName
) {
) -> DatadogCoreProtocol {
// TODO: RUMM-511 remove this warning
#if targetEnvironment(macCatalyst)
consolePrint("⚠️ Catalyst is not officially supported by Datadog SDK: some features may NOT be functional!")
#endif

do {
try initializeOrThrow(
return try initializeOrThrow(
with: configuration,
trackingConsent: trackingConsent,
instanceName: instanceName
)
} catch {
consolePrint("\(error)")
return NOPDatadogCore()
}
}

private static func initializeOrThrow(
with configuration: Configuration,
trackingConsent: TrackingConsent,
instanceName: String
) throws {
) throws -> DatadogCoreProtocol {
if CoreRegistry.default is DatadogCore {
throw ProgrammerError(description: "SDK is already initialized.")
}
Expand Down Expand Up @@ -351,6 +398,8 @@ public struct Datadog {
)

DD.telemetry = telemetry

return core
}

private static func deleteV1Folders(in core: DatadogCore) {
Expand Down
8 changes: 4 additions & 4 deletions DatadogCore/Tests/Datadog/DatadogConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class DatadogConfigurationTests: XCTestCase {
override func setUp() {
super.setUp()

XCTAssertFalse(Datadog.isInitialized)
XCTAssertFalse(Datadog.isInitialized())
printFunction = PrintFunctionMock()
consolePrint = printFunction.print
}

override func tearDown() {
consolePrint = { print($0) }
printFunction = nil
XCTAssertFalse(Datadog.isInitialized)
XCTAssertFalse(Datadog.isInitialized())
super.tearDown()
}

Expand Down Expand Up @@ -142,7 +142,7 @@ class DatadogConfigurationTests: XCTestCase {
trackingConsent: .mockRandom()
)

XCTAssertTrue(Datadog.isInitialized)
XCTAssertTrue(Datadog.isInitialized())
Datadog.flushAndDeinitialize()
}

Expand All @@ -158,7 +158,7 @@ class DatadogConfigurationTests: XCTestCase {
printFunction.printedMessage,
"🔥 Datadog SDK usage error: `clientToken` cannot be empty."
)
XCTAssertFalse(Datadog.isInitialized)
XCTAssertFalse(Datadog.isInitialized())
}

func testGivenValidConfiguration_whenInitializedMoreThanOnce_itPrintsError() {
Expand Down
8 changes: 4 additions & 4 deletions DatadogCore/Tests/Datadog/DatadogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class DatadogTests: XCTestCase {
override func setUp() {
super.setUp()

XCTAssertFalse(Datadog.isInitialized)
XCTAssertFalse(Datadog.isInitialized())
printFunction = PrintFunctionMock()
consolePrint = printFunction.print
}

override func tearDown() {
consolePrint = { print($0) }
printFunction = nil
XCTAssertFalse(Datadog.isInitialized)
XCTAssertFalse(Datadog.isInitialized())
super.tearDown()
}

Expand Down Expand Up @@ -143,7 +143,7 @@ class DatadogTests: XCTestCase {
with: defaultConfig,
trackingConsent: .mockRandom()
)
XCTAssertTrue(Datadog.isInitialized)
XCTAssertTrue(Datadog.isInitialized())
Datadog.flushAndDeinitialize()
}

Expand All @@ -159,7 +159,7 @@ class DatadogTests: XCTestCase {
printFunction.printedMessage,
"🔥 Datadog SDK usage error: `clientToken` cannot be empty."
)
XCTAssertFalse(Datadog.isInitialized)
XCTAssertFalse(Datadog.isInitialized())
}

func testGivenValidConfiguration_whenInitializedMoreThanOnce_itPrintsError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TracingWithLoggingIntegrationTests: XCTestCase {
core.expectation = expectation(description: "Send log")

// Given
let integration = TracingWithLoggingIntegration(core: core, service: .mockAny(), sendNetworkInfo: .mockAny())
let integration = TracingWithLoggingIntegration(core: core, service: .mockAny(), networkInfoEnabled: .mockAny())

// When
integration.writeLog(
Expand Down Expand Up @@ -67,7 +67,7 @@ class TracingWithLoggingIntegrationTests: XCTestCase {
core.expectation?.expectedFulfillmentCount = 3

// Given
let integration = TracingWithLoggingIntegration(core: core, service: .mockAny(), sendNetworkInfo: .mockAny())
let integration = TracingWithLoggingIntegration(core: core, service: .mockAny(), networkInfoEnabled: .mockAny())

// When
integration.writeLog(
Expand Down Expand Up @@ -106,7 +106,7 @@ class TracingWithLoggingIntegrationTests: XCTestCase {
core.expectation = expectation(description: "Send log")

// Given
let integration = TracingWithLoggingIntegration(core: core, service: .mockAny(), sendNetworkInfo: .mockAny())
let integration = TracingWithLoggingIntegration(core: core, service: .mockAny(), networkInfoEnabled: .mockAny())

// When
integration.writeLog(
Expand Down
6 changes: 3 additions & 3 deletions DatadogCore/Tests/Datadog/LoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class LoggerTests: XCTestCase {
with: Logger.Configuration(
service: "custom-service-name",
name: "custom-logger-name",
sendNetworkInfo: true,
networkInfoEnabled: true,
consoleLogFormat: .short
),
in: core
Expand Down Expand Up @@ -318,7 +318,7 @@ class LoggerTests: XCTestCase {
let feature: LogsFeature = .mockAny()
try core.register(feature: feature)

let logger = Logger.create(with: Logger.Configuration(sendNetworkInfo: true), in: core)
let logger = Logger.create(with: Logger.Configuration(networkInfoEnabled: true), in: core)

// simulate entering cellular service range
core.context.carrierInfo = .mockWith(
Expand Down Expand Up @@ -353,7 +353,7 @@ class LoggerTests: XCTestCase {
let feature: LogsFeature = .mockAny()
try core.register(feature: feature)

let logger = Logger.create(with: Logger.Configuration(sendNetworkInfo: true), in: core)
let logger = Logger.create(with: Logger.Configuration(networkInfoEnabled: true), in: core)

// simulate reachable network
core.context.networkConnectionInfo = .mockWith(
Expand Down
4 changes: 2 additions & 2 deletions DatadogCore/Tests/Datadog/Mocks/LogsMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ extension LogEventBuilder: AnyMockable {
public static func mockWith(
service: String = .mockAny(),
loggerName: String = .mockAny(),
sendNetworkInfo: Bool = .mockAny(),
networkInfoEnabled: Bool = .mockAny(),
eventMapper: LogEventMapper? = nil,
deviceInfo: DeviceInfo = .mockAny()
) -> LogEventBuilder {
return LogEventBuilder(
service: service,
loggerName: loggerName,
sendNetworkInfo: sendNetworkInfo,
networkInfoEnabled: networkInfoEnabled,
eventMapper: eventMapper
)
}
Expand Down
20 changes: 10 additions & 10 deletions DatadogCore/Tests/Datadog/Mocks/RUMFeatureMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ extension CrashReportReceiver: AnyMockable {
applicationID: String = .mockAny(),
dateProvider: DateProvider = SystemDateProvider(),
sessionSampler: Sampler = .mockKeepAll(),
backgroundEventTrackingEnabled: Bool = true,
trackBackgroundEvents: Bool = true,
uuidGenerator: RUMUUIDGenerator = DefaultRUMUUIDGenerator(),
ciTest: RUMCITest? = nil
) -> Self {
.init(
applicationID: applicationID,
dateProvider: dateProvider,
sessionSampler: sessionSampler,
backgroundEventTrackingEnabled: backgroundEventTrackingEnabled,
trackBackgroundEvents: trackBackgroundEvents,
uuidGenerator: uuidGenerator,
ciTest: ciTest
)
Expand Down Expand Up @@ -672,8 +672,8 @@ extension RUMScopeDependencies {
core: DatadogCoreProtocol = NOPDatadogCore(),
rumApplicationID: String = .mockAny(),
sessionSampler: Sampler = .mockKeepAll(),
backgroundEventTrackingEnabled: Bool = .mockAny(),
frustrationTrackingEnabled: Bool = true,
trackBackgroundEvents: Bool = .mockAny(),
trackFrustrations: Bool = true,
firstPartyHosts: FirstPartyHosts = .init([:]),
eventBuilder: RUMEventBuilder = RUMEventBuilder(eventsMapper: .mockNoOp()),
rumUUIDGenerator: RUMUUIDGenerator = DefaultRUMUUIDGenerator(),
Expand All @@ -686,8 +686,8 @@ extension RUMScopeDependencies {
core: core,
rumApplicationID: rumApplicationID,
sessionSampler: sessionSampler,
backgroundEventTrackingEnabled: backgroundEventTrackingEnabled,
frustrationTrackingEnabled: frustrationTrackingEnabled,
trackBackgroundEvents: trackBackgroundEvents,
trackFrustrations: trackFrustrations,
firstPartyHosts: firstPartyHosts,
eventBuilder: eventBuilder,
rumUUIDGenerator: rumUUIDGenerator,
Expand All @@ -702,8 +702,8 @@ extension RUMScopeDependencies {
func replacing(
rumApplicationID: String? = nil,
sessionSampler: Sampler? = nil,
backgroundEventTrackingEnabled: Bool? = nil,
frustrationTrackingEnabled: Bool? = nil,
trackBackgroundEvents: Bool? = nil,
trackFrustrations: Bool? = nil,
firstPartyHosts: FirstPartyHosts? = nil,
eventBuilder: RUMEventBuilder? = nil,
rumUUIDGenerator: RUMUUIDGenerator? = nil,
Expand All @@ -716,8 +716,8 @@ extension RUMScopeDependencies {
core: self.core,
rumApplicationID: rumApplicationID ?? self.rumApplicationID,
sessionSampler: sessionSampler ?? self.sessionSampler,
backgroundEventTrackingEnabled: backgroundEventTrackingEnabled ?? self.backgroundEventTrackingEnabled,
frustrationTrackingEnabled: frustrationTrackingEnabled ?? self.frustrationTrackingEnabled,
trackBackgroundEvents: trackBackgroundEvents ?? self.trackBackgroundEvents,
trackFrustrations: trackFrustrations ?? self.trackFrustrations,
firstPartyHosts: firstPartyHosts ?? self.firstPartyHosts,
eventBuilder: eventBuilder ?? self.eventBuilder,
rumUUIDGenerator: rumUUIDGenerator ?? self.rumUUIDGenerator,
Expand Down
Loading

0 comments on commit 4726fe2

Please sign in to comment.