Skip to content

Commit

Permalink
Fix conflicts with #1347
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Jul 10, 2023
1 parent 9f679f1 commit 524eb9e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_ type: T.Type) -> T? {
return core.feature(type)
}

func register<T>(feature instance: T?) {
let key = String(describing: T.self)
featureScopeInterceptors[key] = FeatureScopeInterceptor()

core.register(feature: instance)
}

func scope<T>(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()
Expand Down
13 changes: 0 additions & 13 deletions DatadogCore/Tests/Datadog/Mocks/RUMFeatureMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
]
}
}
7 changes: 6 additions & 1 deletion DatadogRUM/Tests/Mocks/RUMDataModelMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()),
Expand All @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions DatadogRUM/Tests/Mocks/RUMFeatureMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -990,3 +990,16 @@ internal class ValueObserverMock<Value>: 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
]
]
}
}
76 changes: 44 additions & 32 deletions DatadogSessionReplay/Sources/Recorder/RecordingCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 524eb9e

Please sign in to comment.