diff --git a/DatadogCore/Tests/Datadog/Mocks/DatadogInternal/DatadogCoreProxy.swift b/DatadogCore/Tests/Datadog/Mocks/DatadogInternal/DatadogCoreProxy.swift index 8b080aa2e2..72ceda95ce 100644 --- a/DatadogCore/Tests/Datadog/Mocks/DatadogInternal/DatadogCoreProxy.swift +++ b/DatadogCore/Tests/Datadog/Mocks/DatadogInternal/DatadogCoreProxy.swift @@ -86,31 +86,11 @@ internal class DatadogCoreProxy: DatadogCoreProtocol { core.update(feature: feature, attributes: attributes) } - func send(message: FeatureMessag, else fallback: @escaping () -> Void) { + func send(message: FeatureMessage, else fallback: @escaping () -> Void) { core.send(message: message, else: fallback) } } -extension DatadogCoreProxy: DatadogV1CoreProtocol { - func feature(_ type: T.Type) -> T? { - return core.feature(type) - } - - func register(feature instance: T?) { - let key = String(describing: T.self) - featureScopeInterceptors[key] = FeatureScopeInterceptor() - - core.register(feature: instance) - } - - func scope(for featureType: T.Type) -> FeatureScope? { - return core.scope(for: featureType).map { scope in - let key = String(describing: T.self) - return FeatureScopeProxy(proxy: scope, interceptor: featureScopeInterceptors[key]!) - } - } -} - extension DatadogCoreProxy { func flush() { core.flush() diff --git a/DatadogCore/Tests/Datadog/Mocks/RUMFeatureMocks.swift b/DatadogCore/Tests/Datadog/Mocks/RUMFeatureMocks.swift index 9719ca87c8..7d663f3653 100644 --- a/DatadogCore/Tests/Datadog/Mocks/RUMFeatureMocks.swift +++ b/DatadogCore/Tests/Datadog/Mocks/RUMFeatureMocks.swift @@ -1008,16 +1008,3 @@ class ContinuousVitalReaderMock: ContinuousVitalReader { } } } - -// MARK: - Dependency on Session Replay - -extension Dictionary where Key == String, Value == FeatureBaggage { - static func mockSessionReplayAttributes(hasReplay: Bool?, recordsCountByViewID: [String: Int64]? = nil) -> Self { - return [ - SessionReplayDependency.srBaggageKey: [ - SessionReplayDependency.hasReplay: hasReplay, - SessionReplayDependency.recordsCountByViewID: recordsCountByViewID - ] - ] - } -} diff --git a/DatadogRUM/Tests/Mocks/RUMDataModelMocks.swift b/DatadogRUM/Tests/Mocks/RUMDataModelMocks.swift index 6a6b797949..dafc247d0c 100644 --- a/DatadogRUM/Tests/Mocks/RUMDataModelMocks.swift +++ b/DatadogRUM/Tests/Mocks/RUMDataModelMocks.swift @@ -86,6 +86,7 @@ extension RUMDevice.RUMDeviceType: RandomMockable { extension RUMOperatingSystem: RandomMockable { public static func mockRandom() -> RUMOperatingSystem { return .init( + build: nil, name: .mockRandom(length: 5), version: .mockRandom(among: .decimalDigits, length: 2), versionMajor: .mockRandom(among: .decimalDigits, length: 1) @@ -108,6 +109,8 @@ extension RUMViewEvent: RandomMockable { dd: .init( browserSdkVersion: nil, documentVersion: .mockRandom(), + pageStates: nil, + replayStats: nil, session: .init(plan: .plan1) ), application: .init(id: .mockRandom()), @@ -118,12 +121,14 @@ extension RUMViewEvent: RandomMockable { device: .mockRandom(), display: nil, os: .mockRandom(), + privacy: nil, service: .mockRandom(), session: .init( hasReplay: nil, id: .mockRandom(), isActive: true, - startReason: .appStart, + sampledForReplay: nil, + startPrecondition: .appLaunch, type: .user ), source: .ios, diff --git a/DatadogRUM/Tests/Mocks/RUMFeatureMocks.swift b/DatadogRUM/Tests/Mocks/RUMFeatureMocks.swift index b12d78efbb..36ed035125 100644 --- a/DatadogRUM/Tests/Mocks/RUMFeatureMocks.swift +++ b/DatadogRUM/Tests/Mocks/RUMFeatureMocks.swift @@ -990,3 +990,16 @@ internal class ValueObserverMock: ValueObserver { onValueChange?(oldValue, newValue) } } + +// MARK: - Dependency on Session Replay + +extension Dictionary where Key == String, Value == FeatureBaggage { + static func mockSessionReplayAttributes(hasReplay: Bool?, recordsCountByViewID: [String: Int64]? = nil) -> Self { + return [ + SessionReplayDependency.srBaggageKey: [ + SessionReplayDependency.hasReplay: hasReplay, + SessionReplayDependency.recordsCountByViewID: recordsCountByViewID + ] + ] + } +} diff --git a/DatadogSessionReplay/Sources/Recorder/RecordingCoordinator.swift b/DatadogSessionReplay/Sources/Recorder/RecordingCoordinator.swift index a548130742..da21e3c2ff 100644 --- a/DatadogSessionReplay/Sources/Recorder/RecordingCoordinator.swift +++ b/DatadogSessionReplay/Sources/Recorder/RecordingCoordinator.swift @@ -5,60 +5,72 @@ */ import Foundation -import Datadog +import DatadogInternal /// Object is responsible for getting the RUM context, randomising the sampling rate, /// starting/stopping the recording scheduler as needed and propagating `has_replay` to other features. internal class RecordingCoordinator { - private let recorder: Recording + let recorder: Recording + let scheduler: Scheduler + let sampler: Sampler + let privacy: PrivacyLevel + let srContextPublisher: SRContextPublisher private var currentRUMContext: RUMContext? = nil private var isSampled = false init( scheduler: Scheduler, - privacy: SessionReplayPrivacy, + privacy: PrivacyLevel, rumContextObserver: RUMContextObserver, srContextPublisher: SRContextPublisher, recorder: Recording, sampler: Sampler ) { self.recorder = recorder - srContextPublisher.setHasReplay(false) + self.scheduler = scheduler + self.sampler = sampler + self.privacy = privacy + self.srContextPublisher = srContextPublisher - scheduler.schedule { [weak self] in - guard let rumContext = self?.currentRUMContext, - let viewID = rumContext.ids.viewID else { - return - } - let recorderContext = Recorder.Context( - privacy: privacy, - applicationID: rumContext.ids.applicationID, - sessionID: rumContext.ids.sessionID, - viewID: viewID, - viewServerTimeOffset: rumContext.viewServerTimeOffset - ) - self?.recorder.captureNextRecord(recorderContext) - } + srContextPublisher.setHasReplay(false) + scheduler.schedule { [weak self] in self?.captureNextRecord() } scheduler.start() - rumContextObserver.observe(on: scheduler.queue) { [weak self] rumContext in - if self?.currentRUMContext?.ids.sessionID != rumContext?.ids.sessionID || self?.currentRUMContext == nil { - self?.isSampled = sampler.sample() - } + rumContextObserver.observe(on: scheduler.queue) { [weak self] in self?.onRUMContextChanged(rumContext: $0) } + } - self?.currentRUMContext = rumContext + private func onRUMContextChanged(rumContext: RUMContext?) { + if currentRUMContext?.ids.sessionID != rumContext?.ids.sessionID || currentRUMContext == nil { + isSampled = sampler.sample() + } - if self?.isSampled == true { - scheduler.start() - } else { - scheduler.stop() - } + currentRUMContext = rumContext + + if isSampled { + scheduler.start() + } else { + scheduler.stop() + } + + srContextPublisher.setHasReplay( + isSampled == true && currentRUMContext?.ids.viewID != nil + ) + } - srContextPublisher.setHasReplay( - self?.isSampled == true && self?.currentRUMContext?.ids.viewID != nil - ) + private func captureNextRecord() { + guard let rumContext = currentRUMContext, + let viewID = rumContext.ids.viewID else { + return } + let recorderContext = Recorder.Context( + privacy: privacy, + applicationID: rumContext.ids.applicationID, + sessionID: rumContext.ids.sessionID, + viewID: viewID, + viewServerTimeOffset: rumContext.viewServerTimeOffset + ) + recorder.captureNextRecord(recorderContext) } -} \ No newline at end of file +} diff --git a/TestUtilities/Mocks/CoreMocks/FeatureRegistrationCoreMock.swift b/TestUtilities/Mocks/CoreMocks/FeatureRegistrationCoreMock.swift index 5191511444..ee7682bc5e 100644 --- a/TestUtilities/Mocks/CoreMocks/FeatureRegistrationCoreMock.swift +++ b/TestUtilities/Mocks/CoreMocks/FeatureRegistrationCoreMock.swift @@ -55,6 +55,10 @@ public class FeatureRegistrationCoreMock: DatadogCoreProtocol { // not supported - use different type of core mock if you need this } + public func update(feature: String, attributes: @escaping () -> FeatureBaggage) { + // not supported - use different type of core mock if you need thi + } + public func send(message: DatadogInternal.FeatureMessage, else fallback: @escaping () -> Void) { // not supported - use different type of core mock if you need this }