Skip to content

Commit

Permalink
RUMM-1718 PR comments addressed
Browse files Browse the repository at this point in the history
errorSourceType is extracted in RUMCommand level
instead of Scopes level
  • Loading branch information
buranmert committed Nov 5, 2021
1 parent a5905e4 commit c683734
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 59 deletions.
12 changes: 12 additions & 0 deletions Sources/Datadog/RUM/RUMMonitor/RUMCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ internal struct RUMAddCurrentViewErrorCommand: RUMCommand {
let stack: String?
/// The origin of this error.
let source: RUMInternalErrorSource
/// The platform type of the error (iOS, React Native, ...)
let errorSourceType: RUMErrorEvent.Error.SourceType

init(
time: Date,
Expand All @@ -84,6 +86,8 @@ internal struct RUMAddCurrentViewErrorCommand: RUMCommand {
self.message = message
self.type = type
self.stack = stack

self.errorSourceType = RUMErrorSourceType.extract(from: &self.attributes)
}

init(
Expand All @@ -100,6 +104,8 @@ internal struct RUMAddCurrentViewErrorCommand: RUMCommand {
self.message = dderror.message
self.type = dderror.type
self.stack = dderror.stack

self.errorSourceType = RUMErrorSourceType.extract(from: &self.attributes)
}
}

Expand Down Expand Up @@ -176,6 +182,8 @@ internal struct RUMStopResourceWithErrorCommand: RUMResourceCommand {
let errorType: String?
/// The origin of the error (network, webview, ...)
let errorSource: RUMInternalErrorSource
/// The platform type of the error (iOS, React Native, ...)
let errorSourceType: RUMErrorEvent.Error.SourceType
/// Error stacktrace.
let stack: String?
/// HTTP status code of the Ressource error.
Expand All @@ -199,6 +207,8 @@ internal struct RUMStopResourceWithErrorCommand: RUMResourceCommand {
self.httpStatusCode = httpStatusCode
// The stack will be meaningless in most cases as it will go down to the networking code:
self.stack = nil

self.errorSourceType = RUMErrorSourceType.extract(from: &self.attributes)
}

init(
Expand All @@ -220,6 +230,8 @@ internal struct RUMStopResourceWithErrorCommand: RUMResourceCommand {
self.errorType = dderror.type
// The stack will give the networking error (`NSError`) description in most cases:
self.stack = dderror.stack

self.errorSourceType = RUMErrorSourceType.extract(from: &self.attributes)
}
}

Expand Down
6 changes: 1 addition & 5 deletions Sources/Datadog/RUM/RUMMonitor/Scopes/RUMResourceScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,6 @@ internal class RUMResourceScope: RUMScope {

private func sendErrorEvent(on command: RUMStopResourceWithErrorCommand) -> Bool {
attributes.merge(rumCommandAttributes: command.attributes)
let sourceType = (attributes.removeValue(forKey: RUMAttribute.internalErrorSourceType) as? String)
.flatMap {
return RUMErrorEvent.Error.SourceType(rawValue: $0)
} ?? .ios

let eventData = RUMErrorEvent(
dd: .init(
Expand All @@ -237,7 +233,7 @@ internal class RUMResourceScope: RUMScope {
url: resourceURL
),
source: command.errorSource.toRUMDataFormat,
sourceType: sourceType,
sourceType: command.errorSourceType,
stack: command.stack,
type: command.errorType
),
Expand Down
6 changes: 1 addition & 5 deletions Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,6 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {

private func sendErrorEvent(on command: RUMAddCurrentViewErrorCommand) -> Bool {
attributes.merge(rumCommandAttributes: command.attributes)
let sourceType = (attributes.removeValue(forKey: RUMAttribute.internalErrorSourceType) as? String)
.flatMap {
return RUMErrorEvent.Error.SourceType(rawValue: $0)
} ?? .ios

let eventData = RUMErrorEvent(
dd: .init(
Expand All @@ -437,7 +433,7 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
message: command.message,
resource: nil,
source: command.source.toRUMDataFormat,
sourceType: sourceType,
sourceType: command.errorSourceType,
stack: command.stack,
type: command.type
),
Expand Down
11 changes: 11 additions & 0 deletions Sources/Datadog/RUMMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ internal extension RUMResourceType {
}
}

internal typealias RUMErrorSourceType = RUMErrorEvent.Error.SourceType

internal extension RUMErrorSourceType {
static func extract(from attributes: inout [AttributeKey: AttributeValue]) -> Self {
return (attributes.removeValue(forKey: RUMAttribute.internalErrorSourceType) as? String)
.flatMap {
return RUMErrorEvent.Error.SourceType(rawValue: $0)
} ?? .ios
}
}

/// Describes the type of a RUM Action.
public enum RUMUserActionType {
case tap
Expand Down
49 changes: 38 additions & 11 deletions Tests/DatadogTests/Datadog/RUM/RUMMonitor/RUMCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,33 @@ import XCTest
@testable import Datadog

class RUMCommandTests: XCTestCase {
struct SwiftError: Error, CustomDebugStringConvertible {
let debugDescription = "error description"
}

enum SwiftEnumeratedError: Error {
case errorLabel
}

let nsError = NSError(
domain: "custom-domain",
code: 10,
userInfo: [NSLocalizedDescriptionKey: "error description"]
)

func testWhenRUMAddCurrentViewErrorCommand_isBuildWithErrorObject() {
struct SwiftError: Error, CustomDebugStringConvertible {
let debugDescription = "error description"
}
var command = RUMAddCurrentViewErrorCommand(time: .mockAny(), error: SwiftError(), source: .source, attributes: [:])

XCTAssertEqual(command.type, "SwiftError")
XCTAssertEqual(command.message, "error description")
XCTAssertEqual(command.stack, "error description")

enum SwiftEnumeratedError: Error {
case errorLabel
}
command = RUMAddCurrentViewErrorCommand(time: .mockAny(), error: SwiftEnumeratedError.errorLabel, source: .source, attributes: [:])

XCTAssertEqual(command.type, "SwiftEnumeratedError")
XCTAssertEqual(command.message, "errorLabel")
XCTAssertEqual(command.stack, "errorLabel")

let nsError = NSError(
domain: "custom-domain",
code: 10,
userInfo: [NSLocalizedDescriptionKey: "error description"]
)
command = RUMAddCurrentViewErrorCommand(time: .mockAny(), error: nsError, source: .source, attributes: [:])

XCTAssertEqual(command.type, "custom-domain - 10")
Expand All @@ -43,4 +46,28 @@ class RUMCommandTests: XCTestCase {
"""
)
}

func testWhenRUMAddCurrentViewErrorCommand_isPassedErrorSourceTypeAttribute() {
let command1 = RUMAddCurrentViewErrorCommand(time: .mockAny(), error: SwiftError(), source: .source, attributes: [RUMAttribute.internalErrorSourceType: "react-native"])

XCTAssertEqual(command1.errorSourceType, .reactNative)
XCTAssertTrue(command1.attributes.isEmpty)

let command2 = RUMAddCurrentViewErrorCommand(time: .mockAny(), message: .mockAny(), type: .mockAny(), stack: .mockAny(), source: .source, attributes: [RUMAttribute.internalErrorSourceType: "react-native"])

XCTAssertEqual(command2.errorSourceType, .reactNative)
XCTAssertTrue(command2.attributes.isEmpty)
}

func testWhenRUMStopResourceWithErrorCommand_isPassedErrorSourceTypeAttribute() {
let command1 = RUMStopResourceWithErrorCommand(resourceKey: .mockAny(), time: .mockAny(), error: SwiftError(), source: .source, httpStatusCode: .mockAny(), attributes: [RUMAttribute.internalErrorSourceType: "react-native"])

XCTAssertEqual(command1.errorSourceType, .reactNative)
XCTAssertTrue(command1.attributes.isEmpty)

let command2 = RUMStopResourceWithErrorCommand(resourceKey: .mockAny(), time: .mockAny(), message: .mockAny(), type: .mockAny(), source: .source, httpStatusCode: .mockAny(), attributes: [RUMAttribute.internalErrorSourceType: "react-native"])

XCTAssertEqual(command2.errorSourceType, .reactNative)
XCTAssertTrue(command2.attributes.isEmpty)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -631,44 +631,6 @@ class RUMViewScopeTests: XCTestCase {
XCTAssertEqual(viewUpdate.model.view.error.count, 1, "Failed Resource should be counted as Error")
}

func testWhenErrorSourceTypeIsPassed_itSendsNonDefaultErrorSourceType() throws {
var currentTime: Date = .mockDecember15th2019At10AMUTC()
let scope = RUMViewScope(
parent: parent,
dependencies: dependencies,
identity: mockView,
path: "UIViewController",
name: "ViewName",
attributes: ["_dd.error.source_type": "react-native"],
customTimings: [:],
startTime: currentTime
)

_ = scope.process(
command: RUMStartViewCommand.mockWith(time: currentTime, attributes: ["foo": "bar"], identity: mockView, isInitialView: true)
)

currentTime.addTimeInterval(1)

_ = scope.process(
command: RUMAddCurrentViewErrorCommand.mockWithErrorMessage(time: currentTime, message: "view error", source: .source, stack: nil)
)

let error = try XCTUnwrap(output.recordedEvents(ofType: RUMEvent<RUMErrorEvent>.self).last)
XCTAssertValidRumUUID(error.model.view.id)
XCTAssertEqual(error.model.error.type, "abc")
XCTAssertEqual(error.model.error.message, "view error")
XCTAssertEqual(error.model.error.source, .source)
XCTAssertEqual(error.model.error.sourceType, .reactNative)
XCTAssertNil(error.model.error.stack)
XCTAssertNil(error.model.error.isCrash)
XCTAssertNil(error.model.error.resource)
XCTAssertNil(error.model.action)

let viewUpdate = try XCTUnwrap(output.recordedEvents(ofType: RUMEvent<RUMViewEvent>.self).last)
XCTAssertEqual(viewUpdate.model.view.error.count, 1)
}

// MARK: - Long tasks

func testWhenLongTaskIsAdded_itSendsLongTaskEventAndViewUpdateEvent() throws {
Expand Down

0 comments on commit c683734

Please sign in to comment.