Skip to content

Commit

Permalink
Merge pull request #649 from DataDog/maxep/RUMM-1615/rum-command-publ…
Browse files Browse the repository at this point in the history
…isher

RUMM-1615 Create `RUMCommandPublisher` to publish RUM command to a subscriber
  • Loading branch information
maxep authored Oct 26, 2021
2 parents afd01f8 + cf0fb2b commit 0d6ef03
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 43 deletions.
24 changes: 12 additions & 12 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3018,14 +3018,6 @@
path = PLCrashReporterIntegration;
sourceTree = "<group>";
};
61F3C96F2535AC7600E2F8C4 /* UIKitHierarchyInspection */ = {
isa = PBXGroup;
children = (
61F3C9702535AE9400E2F8C4 /* UIKitHierarchyInspector.swift */,
);
path = UIKitHierarchyInspection;
sourceTree = "<group>";
};
61F3CD9C251106EB00C816E5 /* Scenarios */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -3076,10 +3068,7 @@
61F3CDA1251118DD00C816E5 /* Views */ = {
isa = PBXGroup;
children = (
61F3C96F2535AC7600E2F8C4 /* UIKitHierarchyInspection */,
61F3CDA62512144600C816E5 /* UIKitRUMViewsPredicate.swift */,
61F3CDA2251118FB00C816E5 /* UIKitRUMViewsHandler.swift */,
61F3CDA42511190E00C816E5 /* UIViewControllerSwizzler.swift */,
D249859B2727FF1D00B4F72D /* UIKit */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -3260,6 +3249,17 @@
path = Scrubbing;
sourceTree = "<group>";
};
D249859B2727FF1D00B4F72D /* UIKit */ = {
isa = PBXGroup;
children = (
61F3CDA62512144600C816E5 /* UIKitRUMViewsPredicate.swift */,
61F3CDA2251118FB00C816E5 /* UIKitRUMViewsHandler.swift */,
61F3CDA42511190E00C816E5 /* UIViewControllerSwizzler.swift */,
61F3C9702535AE9400E2F8C4 /* UIKitHierarchyInspector.swift */,
);
path = UIKit;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import UIKit

internal protocol UIKitRUMUserActionsHandlerType: AnyObject {
func subscribe(commandsSubscriber: RUMCommandSubscriber)
internal protocol UIKitRUMUserActionsHandlerType: RUMCommandPublisher {
func notify_sendEvent(application: UIApplication, event: UIEvent)
}

Expand All @@ -24,8 +23,8 @@ internal class UIKitRUMUserActionsHandler: UIKitRUMUserActionsHandlerType {

weak var subscriber: RUMCommandSubscriber?

func subscribe(commandsSubscriber: RUMCommandSubscriber) {
self.subscriber = commandsSubscriber
func publish(to subscriber: RUMCommandSubscriber) {
self.subscriber = subscriber
}

func notify_sendEvent(application: UIApplication, event: UIEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Foundation

internal class LongTaskObserver {
internal class LongTaskObserver: RUMCommandPublisher {
private let longTaskDurationThreshold: TimeInterval
private let dateProvider: DateProvider

Expand All @@ -15,8 +15,9 @@ internal class LongTaskObserver {
private var lastActivity: (kind: CFRunLoopActivity, date: Date)?

weak var subscriber: RUMCommandSubscriber?
func subscribe(commandsSubscriber: RUMCommandSubscriber) {
self.subscriber = commandsSubscriber

func publish(to subscriber: RUMCommandSubscriber) {
self.subscriber = subscriber
}

init(threshold: TimeInterval, dateProvider: DateProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

/// RUM Auto Instrumentation feature.
internal final class RUMAutoInstrumentation {
internal final class RUMAutoInstrumentation: RUMCommandPublisher {
static var instance: RUMAutoInstrumentation?

/// RUM Views auto instrumentation.
Expand Down Expand Up @@ -90,10 +90,10 @@ internal final class RUMAutoInstrumentation {
longTasks?.start()
}

func subscribe(commandSubscriber: RUMCommandSubscriber) {
views?.handler.subscribe(commandsSubscriber: commandSubscriber)
userActions?.handler.subscribe(commandsSubscriber: commandSubscriber)
longTasks?.subscribe(commandsSubscriber: commandSubscriber)
func publish(to subscriber: RUMCommandSubscriber) {
views?.handler.publish(to: subscriber)
userActions?.handler.publish(to: subscriber)
longTasks?.publish(to: subscriber)
}

#if DD_SDK_COMPILED_FOR_TESTING
Expand Down
18 changes: 18 additions & 0 deletions Sources/Datadog/RUM/AutoInstrumentation/RUMCommandSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@

import Foundation

/// The Command Subscriber is able to process RUM Commands.
///
/// This protocol expect a single function to receive `RUMCommand`.
internal protocol RUMCommandSubscriber: AnyObject {
/// Processes the given RUM Command.
///
/// - Parameter command: The RUM command to process.
func process(command: RUMCommand)
}

/// A Command Publisher is responsible for creating RUM Commands
/// to be processed by a `RUMCommandSubscriber`.
internal protocol RUMCommandPublisher: AnyObject {
/// Lets a `RUMCommandSubscriber` subscribe to this Publisher.
///
/// The given subscriber should be used to process any command created
/// by this publisher.
///
/// - Parameter subscriber: The RUM command subscriber.
func publish(to subscriber: RUMCommandSubscriber)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Foundation

internal typealias URLSessionRUMAttributesProvider = (URLRequest, URLResponse?, Data?, Error?) -> [AttributeKey: AttributeValue]?

internal class URLSessionRUMResourcesHandler: URLSessionInterceptionHandler {
internal class URLSessionRUMResourcesHandler: URLSessionInterceptionHandler, RUMCommandPublisher {
private let dateProvider: DateProvider
/// Attributes-providing callback.
/// It is configured by the user and should be used to associate additional RUM attributes with intercepted RUM Resource.
Expand All @@ -25,8 +25,8 @@ internal class URLSessionRUMResourcesHandler: URLSessionInterceptionHandler {

weak var subscriber: RUMCommandSubscriber?

func subscribe(commandsSubscriber: RUMCommandSubscriber) {
self.subscriber = commandsSubscriber
func publish(to subscriber: RUMCommandSubscriber) {
self.subscriber = subscriber
}

// MARK: - URLSessionInterceptionHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import UIKit

internal protocol UIKitRUMViewsHandlerType: AnyObject {
func subscribe(commandsSubscriber: RUMCommandSubscriber)
internal protocol UIKitRUMViewsHandlerType: RUMCommandPublisher {
/// Gets called on `super.viewDidAppear()`.
func notify_viewDidAppear(viewController: UIViewController, animated: Bool)
/// Gets called on `super.viewDidDisappear()`.
Expand Down Expand Up @@ -37,8 +36,8 @@ internal class UIKitRUMViewsHandler: UIKitRUMViewsHandlerType {

weak var subscriber: RUMCommandSubscriber?

func subscribe(commandsSubscriber: RUMCommandSubscriber) {
self.subscriber = commandsSubscriber
func publish(to subscriber: RUMCommandSubscriber) {
self.subscriber = subscriber
}

func notify_viewDidAppear(viewController: UIViewController, animated: Bool) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Datadog/RUMMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ public class RUMMonitor: DDRUMMonitor, RUMCommandSubscriber {
)
}
let monitor = RUMMonitor(rumFeature: rumFeature)
RUMAutoInstrumentation.instance?.subscribe(commandSubscriber: monitor)
URLSessionAutoInstrumentation.instance?.subscribe(commandSubscriber: monitor)
RUMAutoInstrumentation.instance?.publish(to: monitor)
URLSessionAutoInstrumentation.instance?.publish(to: monitor)
return monitor
} catch {
consolePrint("\(error)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

/// `URLSession` Auto Instrumentation feature.
internal final class URLSessionAutoInstrumentation {
internal final class URLSessionAutoInstrumentation: RUMCommandPublisher {
static var instance: URLSessionAutoInstrumentation?

let swizzler: URLSessionSwizzler
Expand Down Expand Up @@ -44,9 +44,9 @@ internal final class URLSessionAutoInstrumentation {
swizzler.swizzle()
}

func subscribe(commandSubscriber: RUMCommandSubscriber) {
func publish(to subscriber: RUMCommandSubscriber) {
let rumResourceHandler = interceptor.handler as? URLSessionRUMResourcesHandler
rumResourceHandler?.subscribe(commandsSubscriber: commandSubscriber)
rumResourceHandler?.publish(to: subscriber)
}

#if DD_SDK_COMPILED_FOR_TESTING
Expand Down
8 changes: 4 additions & 4 deletions Tests/DatadogTests/Datadog/Mocks/RUMFeatureMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,8 @@ class UIKitRUMViewsHandlerMock: UIKitRUMViewsHandlerType {
var notifyViewDidAppear: ((UIViewController, Bool) -> Void)?
var notifyViewDidDisappear: ((UIViewController, Bool) -> Void)?

func subscribe(commandsSubscriber: RUMCommandSubscriber) {
onSubscribe?(commandsSubscriber)
func publish(to subscriber: RUMCommandSubscriber) {
onSubscribe?(subscriber)
}

func notify_viewDidAppear(viewController: UIViewController, animated: Bool) {
Expand Down Expand Up @@ -684,8 +684,8 @@ class UIKitRUMUserActionsHandlerMock: UIKitRUMUserActionsHandlerType {
var onSubscribe: ((RUMCommandSubscriber) -> Void)?
var onSendEvent: ((UIApplication, UIEvent) -> Void)?

func subscribe(commandsSubscriber: RUMCommandSubscriber) {
onSubscribe?(commandsSubscriber)
func publish(to subscriber: RUMCommandSubscriber) {
onSubscribe?(subscriber)
}

func notify_sendEvent(application: UIApplication, event: UIEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class UIKitRUMUserActionsHandlerTests: XCTestCase {

private func createHandler(userActionsPredicate: UIKitRUMUserActionsPredicate = DefaultUIKitRUMUserActionsPredicate()) -> UIKitRUMUserActionsHandler {
let handler = UIKitRUMUserActionsHandler(dateProvider: dateProvider, userActionsPredicate: userActionsPredicate)
handler.subscribe(commandsSubscriber: commandSubscriber)
handler.publish(to: commandSubscriber)
return handler
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase {

private func createHandler(rumAttributesProvider: URLSessionRUMAttributesProvider? = nil) -> URLSessionRUMResourcesHandler {
let handler = URLSessionRUMResourcesHandler(dateProvider: dateProvider, rumAttributesProvider: rumAttributesProvider)
handler.subscribe(commandsSubscriber: commandSubscriber)
handler.publish(to: commandSubscriber)
return handler
}
private lazy var handler = createHandler(rumAttributesProvider: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class UIKitRUMViewsHandlerTests: XCTestCase {
inspector: uiKitHierarchyInspector,
notificationCenter: notificationCenter
)
handler.subscribe(commandsSubscriber: commandSubscriber)
handler.publish(to: commandSubscriber)
return handler
}()

Expand Down

0 comments on commit 0d6ef03

Please sign in to comment.