Skip to content

Commit

Permalink
Merge pull request #232 from DataDog/buranmert/RUMM-638-global-rum-at…
Browse files Browse the repository at this point in the history
…tributes

RUMM-638 Global RUM attributes
  • Loading branch information
buranmert authored Aug 31, 2020
2 parents 4a344f5 + a5480b1 commit 8088345
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 17 deletions.
19 changes: 18 additions & 1 deletion Sources/Datadog/DDRUMMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit
/// `DDRUMMonitor` allows you to record User events that can be explored and analyzed in Datadog Dashboards.
/// You can only have one active `RUMMonitor`, and should register/retrieve it from the `Global` object.
public class DDRUMMonitor {
// MARK: - Public
// MARK: - Public methods

/// Notifies that the View starts being presented to the user.
/// - Parameters:
Expand Down Expand Up @@ -124,6 +124,23 @@ public class DDRUMMonitor {
public func registerUserAction(type: RUMUserActionType, name: String, attributes: [AttributeKey: AttributeValue]? = nil) {
}

// MARK: - Attributes

/// Adds a custom attribute to all future commands sent by this monitor.
/// - Parameters:
/// - key: key for this attribute. See `AttributeKey` documentation for information about
/// nesting attribute values using dot `.` syntax.
/// - value: any value that conforms to `AttributeValue` typealias. See `AttributeValue` documentation
/// for information about nested encoding containers limitation.
public func addAttribute(forKey key: AttributeKey, value: AttributeValue) {
}

/// Removes the custom attribute from all future commands sent by this monitor.
/// Previous commands won't lose this attribute if they were created prior to this call.
/// - Parameter key: key for the attribute that will be removed.
public func removeAttribute(forKey key: AttributeKey) {
}

// MARK: - Internal

internal init() {}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
case let command as RUMStartViewCommand where command.identity === identity:
if command.isInitialView {
actionsCount += 1
sendApplicationStartAction()
sendApplicationStartAction(on: command)
}
needsViewUpdate = true
case let command as RUMStartViewCommand where command.identity !== identity:
Expand Down Expand Up @@ -199,7 +199,7 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {

// MARK: - Sending RUM Events

private func sendApplicationStartAction() {
private func sendApplicationStartAction(on command: RUMCommand) {
let eventData = RUMAction(
date: viewStartTime.timeIntervalSince1970.toInt64Milliseconds,
application: .init(id: context.rumApplicationID),
Expand All @@ -224,7 +224,7 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
)
)

let event = dependencies.eventBuilder.createRUMEvent(with: eventData, attributes: [:])
let event = dependencies.eventBuilder.createRUMEvent(with: eventData, attributes: command.attributes)
dependencies.eventOutput.write(rumEvent: event)
}

Expand Down
44 changes: 33 additions & 11 deletions Sources/Datadog/RUMMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class RUMMonitor: DDRUMMonitor {
internal let contextProvider: RUMCurrentContext
/// Time provider.
private let dateProvider: DateProvider
/// Attributes associated with every command.
private var rumAttributes: [AttributeKey: AttributeValue] = [:]
/// Queue for processing RUM commands off the main thread and providing current RUM context.
private let queue = DispatchQueue(
label: "com.datadoghq.rum-monitor",
Expand Down Expand Up @@ -125,7 +127,7 @@ public class RUMMonitor: DDRUMMonitor {
process(
command: RUMStartViewCommand(
time: dateProvider.currentDate(),
attributes: attributes ?? [:],
attributes: aggregate(attributes),
identity: viewController
)
)
Expand All @@ -135,7 +137,7 @@ public class RUMMonitor: DDRUMMonitor {
process(
command: RUMStopViewCommand(
time: dateProvider.currentDate(),
attributes: attributes ?? [:],
attributes: aggregate(attributes),
identity: viewController
)
)
Expand All @@ -152,7 +154,7 @@ public class RUMMonitor: DDRUMMonitor {
message: message,
source: source,
stack: stack,
attributes: attributes ?? [:]
attributes: aggregate(attributes)
)
)
}
Expand All @@ -163,7 +165,7 @@ public class RUMMonitor: DDRUMMonitor {
time: dateProvider.currentDate(),
error: error,
source: source,
attributes: attributes ?? [:]
attributes: aggregate(attributes)
)
)
}
Expand All @@ -173,7 +175,7 @@ public class RUMMonitor: DDRUMMonitor {
command: RUMStartResourceCommand(
resourceName: resourceName,
time: dateProvider.currentDate(),
attributes: attributes ?? [:],
attributes: aggregate(attributes),
url: url.absoluteString,
httpMethod: httpMethod
)
Expand All @@ -185,7 +187,7 @@ public class RUMMonitor: DDRUMMonitor {
command: RUMStopResourceCommand(
resourceName: resourceName,
time: dateProvider.currentDate(),
attributes: attributes ?? [:],
attributes: aggregate(attributes),
kind: kind,
httpStatusCode: httpStatusCode,
size: size
Expand All @@ -201,7 +203,7 @@ public class RUMMonitor: DDRUMMonitor {
error: error,
source: source,
httpStatusCode: httpStatusCode,
attributes: attributes ?? [:]
attributes: aggregate(attributes)
)
)
}
Expand All @@ -214,7 +216,7 @@ public class RUMMonitor: DDRUMMonitor {
message: errorMessage,
source: source,
httpStatusCode: httpStatusCode,
attributes: attributes ?? [:]
attributes: aggregate(attributes)
)
)
}
Expand All @@ -223,7 +225,7 @@ public class RUMMonitor: DDRUMMonitor {
process(
command: RUMStartUserActionCommand(
time: dateProvider.currentDate(),
attributes: attributes ?? [:],
attributes: aggregate(attributes),
actionType: type,
name: name
)
Expand All @@ -234,7 +236,7 @@ public class RUMMonitor: DDRUMMonitor {
process(
command: RUMStopUserActionCommand(
time: dateProvider.currentDate(),
attributes: attributes ?? [:],
attributes: aggregate(attributes),
actionType: type,
name: name
)
Expand All @@ -245,15 +247,35 @@ public class RUMMonitor: DDRUMMonitor {
process(
command: RUMAddUserActionCommand(
time: dateProvider.currentDate(),
attributes: attributes ?? [:],
attributes: aggregate(attributes),
actionType: type,
name: name
)
)
}

// MARK: - Attributes

override public func addAttribute(forKey key: AttributeKey, value: AttributeValue) {
queue.async {
self.rumAttributes[key] = value
}
}

override public func removeAttribute(forKey key: AttributeKey) {
queue.async {
self.rumAttributes[key] = nil
}
}

// MARK: - Private

private func aggregate(_ localAttributes: [AttributeKey: AttributeValue]?) -> [AttributeKey: AttributeValue] {
var mergedAttributes = queue.sync { return self.rumAttributes }
mergedAttributes.merge(rumCommandAttributes: localAttributes)
return mergedAttributes
}

private func process(command: RUMCommand) {
queue.async {
_ = self.applicationScope.process(command: command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class RUMViewScopeTests: XCTestCase {
XCTAssertEqual(event.model.view.url, "UIViewController")
XCTAssertValidRumUUID(event.model.action.id)
XCTAssertEqual(event.model.action.type, .applicationStart)
XCTAssertTrue(event.attributes.isEmpty, "The `application_start` event must have no attributes.")
}

func testWhenInitialViewIsStarted_itSendsViewUpdateEvent() throws {
Expand Down
Loading

0 comments on commit 8088345

Please sign in to comment.