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-2171 Use core feature scope in Logger #881

Merged
merged 4 commits into from
Jun 9, 2022
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
17 changes: 4 additions & 13 deletions Sources/Datadog/Datadog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,7 @@ public class Datadog {
dependencies: commonDependencies
)

// First, initialize internal loggers:
let internalLoggerConfiguration = InternalLoggerConfiguration(
sdkVersion: configuration.common.sdkVersion,
applicationVersion: configuration.common.applicationVersion,
environment: configuration.common.environment,
userInfoProvider: userInfoProvider,
networkConnectionInfoProvider: networkConnectionInfoProvider,
carrierInfoProvider: carrierInfoProvider
)

userLogger = createSDKUserLogger(configuration: internalLoggerConfiguration)

// Then, initialize features:
// First, initialize features:
var telemetry: Telemetry?
var logging: LoggingFeature?
var tracing: TracingFeature?
Expand Down Expand Up @@ -300,6 +288,9 @@ public class Datadog {
core.v1.feature(RUMInstrumentation.self)?.enable()
core.v1.feature(URLSessionAutoInstrumentation.self)?.enable()

// Then, initialize internal loggers:
userLogger = createSDKUserLogger(in: core)

defaultDatadogCore = core

// After everything is set up, if the Crash Reporting feature was enabled,
Expand Down
35 changes: 35 additions & 0 deletions Sources/Datadog/DatadogCore/DatadogCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,46 @@ extension DatadogCore: DatadogV1CoreProtocol {
return v1Features[key] as? T
}

func scope<T>(for featureType: T.Type) -> V1FeatureScope? {
let key = String(describing: T.self)

guard let feature = v1Features[key] as? V1Feature else {
return nil
}

return DatadogCoreFeatureScope(
context: v1Context,
storage: feature.storage,
telemetry: telemetry
)
}

var context: DatadogV1Context? {
return v1Context
}
}

/// A v1 Feature with an associated stroage.
internal protocol V1Feature {
/// The feature's storage.
var storage: FeatureStorage { get }
}

/// This Scope complies with `V1FeatureScope` to provide context and writer to
/// v1 Features.
///
/// The execution block is currently running in `sync`, this will change once the
/// context is provided on it's own queue.
internal struct DatadogCoreFeatureScope: V1FeatureScope {
let context: DatadogV1Context
let storage: FeatureStorage
let telemetry: Telemetry?

func eventWriteContext(_ block: (DatadogV1Context, Writer) throws -> Void) {
do {
try block(context, storage.writer)
} catch {
telemetry?.error("Failed to execute feature scope", error: error)
}
}
}
44 changes: 35 additions & 9 deletions Sources/Datadog/DatadogInternal/DatadogV1CoreProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ extension DatadogCoreProtocol {
internal protocol DatadogV1CoreProtocol: DatadogCoreProtocol {
// MARK: - V1 interface

/// The SDK context created upon core initialization or `nil` if SDK was not yet initialized.
var context: DatadogV1Context? { get }

/// Telemetry monitor for this instance of the SDK or `nil` if not configured.
var telemetry: Telemetry? { get }

/// Registers a feature instance by its type description.
///
/// - Parameter instance: The feaure instance to register
Expand All @@ -32,16 +38,39 @@ internal protocol DatadogV1CoreProtocol: DatadogCoreProtocol {
/// - Returns: The feature if any.
func feature<T>(_ type: T.Type) -> T?

/// The SDK context created upon core initialization or `nil` if SDK was not yet initialized.
var context: DatadogV1Context? { get }
/// Returns a Feature scope for a given feature type.
///
/// A Feature instance of the given type must be registered, otherwise return `nil`.
///
/// - Parameters:
/// - type: The feature instance type.
/// - Returns: The feature scope if available.
func scope<T>(for featureType: T.Type) -> V1FeatureScope?
}

/// Telemetry monitor for this instance of the SDK or `nil` if not configured.
var telemetry: Telemetry? { get }
/// Feature scope in v1 provide a context and a writer to build a record event.
internal protocol V1FeatureScope {
/// Retrieve the event context and writer.
///
/// The Feature scope provides the current Datadog context and event writer
/// for the Feature to build and record events.
///
/// - Parameter block: The block to execute.
func eventWriteContext(_ block: (DatadogV1Context, Writer) throws -> Void)
}

extension NOOPDatadogCore: DatadogV1CoreProtocol {
// MARK: - V1 interface

/// Returns `nil`.
var context: DatadogV1Context? {
return nil
}

var telemetry: Telemetry? {
return nil
}

/// no-op
func register<T>(feature instance: T?) {}

Expand All @@ -50,11 +79,8 @@ extension NOOPDatadogCore: DatadogV1CoreProtocol {
return nil
}

var context: DatadogV1Context? {
return nil
}

var telemetry: Telemetry? {
/// no-op
func scope<T>(for featureType: T.Type) -> V1FeatureScope? {
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ internal struct CrashReportingWithLoggingIntegration: CrashReportingIntegration
init(loggingFeature: LoggingFeature, context: DatadogV1Context) {
self.init(
logOutput: LogFileOutput(
fileWriter: loggingFeature.storage.arbitraryAuthorizedWriter,
// The RUM Errors integration is not set for this instance of the `LogFileOutput` we don't want to
// issue additional RUM Errors for crash reports. Those are send through `CrashReportingWithRUMIntegration`.
rumErrorsIntegration: nil
fileWriter: loggingFeature.storage.arbitraryAuthorizedWriter
),
dateProvider: context.dateProvider,
dateCorrector: context.dateCorrector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ internal struct LoggingWithRUMErrorsIntegration {
)
}
}

extension LoggingWithRUMErrorsIntegration: LogOutput {
/// Writes `critical` and `error` logs to RUM.
func write(log: LogEvent) {
if log.status == .error || log.status == .critical {
addError(for: log)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ internal struct TracingWithLoggingIntegration {
logEventMapper: loggingFeature.configuration.logEventMapper
),
loggingOutput: LogFileOutput(
fileWriter: loggingFeature.storage.writer,

// The RUM Errors integration is not set for this instance of the `LogFileOutput`, as RUM Errors for
// spans are managed through more comprehensive `TracingWithRUMErrorsIntegration`.
// Having additional integration here would produce duplicated RUM Errors for span errors set through `span.log()` API.
rumErrorsIntegration: nil
fileWriter: loggingFeature.storage.writer
)
)
}
Expand Down
Loading