Skip to content

Commit

Permalink
no message (+1 squashed commit)
Browse files Browse the repository at this point in the history
Squashed commits:
[d9fe3fb] RUMM-2334 Revert init prints
  • Loading branch information
maxep committed Aug 30, 2022
1 parent 05775fa commit d275f69
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/Datadog/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ public class Logger: LoggerProtocol {
}

private func buildOrThrow(in core: DatadogCoreProtocol) throws -> LoggerProtocol {
if core is NOPDatadogCore {
throw ProgrammerError(
description: "`Datadog.initialize()` must be called prior to `Logger.builder.build()`."
)
}

guard let loggingFeature = core.v1.feature(LoggingFeature.self) else {
throw ProgrammerError(
description: "`Logger.builder.build()` produces a non-functional logger, as the logging feature is disabled."
Expand Down
5 changes: 5 additions & 0 deletions Sources/Datadog/RUMMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ public class RUMMonitor: DDRUMMonitor, RUMCommandSubscriber {
/// Initializes the Datadog RUM Monitor.
public static func initialize(in core: DatadogCoreProtocol = defaultDatadogCore) -> DDRUMMonitor {
do {
if core is NOPDatadogCore {
throw ProgrammerError(
description: "`Datadog.initialize()` must be called prior to `RUMMonitor.initialize()`."
)
}
if Global.rum is RUMMonitor {
throw ProgrammerError(
description: """
Expand Down
5 changes: 5 additions & 0 deletions Sources/Datadog/Tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public class Tracer: OTTracer {
/// - configuration: the tracer configuration obtained using `Tracer.Configuration()`.
public static func initialize(configuration: Configuration, in core: DatadogCoreProtocol = defaultDatadogCore) -> OTTracer {
do {
if core is NOPDatadogCore {
throw ProgrammerError(
description: "`Datadog.initialize()` must be called prior to `Tracer.initialize()`."
)
}
if Global.sharedTracer is Tracer {
throw ProgrammerError(
description: """
Expand Down
22 changes: 22 additions & 0 deletions Tests/DatadogTests/Datadog/LoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,28 @@ class LoggerTests: XCTestCase {

// MARK: - Usage

func testGivenDatadogNotInitialized_whenInitializingLogger_itPrintsError() {
let dd = DD.mockWith(logger: CoreLoggerMock())
defer { dd.reset() }

// given
let core = NOPDatadogCore()

// when
let logger = Logger.builder.build(in: core)

// then
XCTAssertEqual(
dd.logger.criticalLog?.message,
"Failed to build `Logger`."
)
XCTAssertEqual(
dd.logger.criticalLog?.error?.message,
"🔥 Datadog SDK usage error: `Datadog.initialize()` must be called prior to `Logger.builder.build()`."
)
XCTAssertTrue(logger.v2Logger is NOPLogger)
}

func testGivenLoggingFeatureDisabled_whenInitializingLogger_itPrintsError() {
let dd = DD.mockWith(logger: CoreLoggerMock())
defer { dd.reset() }
Expand Down
38 changes: 38 additions & 0 deletions Tests/DatadogTests/Datadog/RUMMonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,44 @@ class RUMMonitorTests: XCTestCase {

// MARK: - Usage

func testGivenDatadogNotInitialized_whenInitializingRUMMonitor_itPrintsError() {
let printFunction = PrintFunctionMock()
consolePrint = printFunction.print
defer { consolePrint = { print($0) } }

// given
let core = NOPDatadogCore()

// when
let monitor = RUMMonitor.initialize(in: core)

// then
XCTAssertEqual(
printFunction.printedMessage,
"🔥 Datadog SDK usage error: `Datadog.initialize()` must be called prior to `RUMMonitor.initialize()`."
)
XCTAssertTrue(monitor is DDNoopRUMMonitor)
}

func testGivenRUMFeatureDisabled_whenInitializingRUMMonitor_itPrintsError() {
let printFunction = PrintFunctionMock()
consolePrint = printFunction.print
defer { consolePrint = { print($0) } }

// given
XCTAssertNil(core.feature(RUMFeature.self))

// when
let monitor = RUMMonitor.initialize(in: core)

// then
XCTAssertEqual(
printFunction.printedMessage,
"🔥 Datadog SDK usage error: `RUMMonitor.initialize()` produces a non-functional monitor, as the RUM feature is disabled."
)
XCTAssertTrue(monitor is DDNoopRUMMonitor)
}

func testGivenRUMMonitorInitialized_whenInitializingAnotherTime_itPrintsError() {
let printFunction = PrintFunctionMock()
consolePrint = printFunction.print
Expand Down
19 changes: 19 additions & 0 deletions Tests/DatadogTests/Datadog/TracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,25 @@ class TracerTests: XCTestCase {

// MARK: - Usage errors

func testGivenDatadogNotInitialized_whenInitializingTracer_itPrintsError() {
let printFunction = PrintFunctionMock()
consolePrint = printFunction.print
defer { consolePrint = { print($0) } }

// given
let core = NOPDatadogCore()

// when
let tracer = Tracer.initialize(configuration: .init(), in: core)

// then
XCTAssertEqual(
printFunction.printedMessage,
"🔥 Datadog SDK usage error: `Datadog.initialize()` must be called prior to `Tracer.initialize()`."
)
XCTAssertTrue(tracer is DDNoopTracer)
}

func testGivenTracingFeatureDisabled_whenInitializingTracer_itPrintsError() {
let printFunction = PrintFunctionMock()
consolePrint = printFunction.print
Expand Down

0 comments on commit d275f69

Please sign in to comment.