Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUMM-3458 Enable adding custom attributes to RUM telemetry (debug) logs #1378

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DatadogCore/Tests/Datadog/Core/_InternalProxyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class _InternalProxyTests: XCTestCase {
// Then
XCTAssertEqual(dd.telemetry.messages.count, 2)

guard case .debug(_, let receivedMessage) = dd.telemetry.messages.first else {
guard case .debug(_, let receivedMessage, _) = dd.telemetry.messages.first else {
return XCTFail("A debug should be send to `DD.telemetry`.")
}
XCTAssertEqual(receivedMessage, randomDebugMessage)
Expand Down
2 changes: 1 addition & 1 deletion DatadogCore/Tests/Datadog/InternalTelemetryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class InternalTelemetryTests: XCTestCase {

// Then
XCTAssertEqual(dd.telemetry.messages.count, 1)
guard case .debug(let receivedId, let receivedMessage) = dd.telemetry.messages.first else {
guard case .debug(let receivedId, let receivedMessage, _) = dd.telemetry.messages.first else {
return XCTFail("A debug should be send to `DD.telemetry`.")
}

Expand Down
5 changes: 5 additions & 0 deletions DatadogCore/Tests/Datadog/Mocks/RUMDataModelMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ extension RUMViewEvent: RandomMockable {
return RUMViewEvent(
dd: .init(
browserSdkVersion: nil,
configuration: nil,
documentVersion: .mockRandom(),
pageStates: nil,
replayStats: nil,
Expand Down Expand Up @@ -188,6 +189,7 @@ extension RUMResourceEvent: RandomMockable {
return RUMResourceEvent(
dd: .init(
browserSdkVersion: nil,
configuration: nil,
discarded: nil,
rulePsr: nil,
session: .init(plan: .plan1),
Expand Down Expand Up @@ -255,6 +257,7 @@ extension RUMActionEvent: RandomMockable {
)
),
browserSdkVersion: nil,
configuration: nil,
session: .init(plan: .plan1)
),
action: .init(
Expand Down Expand Up @@ -307,6 +310,7 @@ extension RUMErrorEvent: RandomMockable {
return RUMErrorEvent(
dd: .init(
browserSdkVersion: nil,
configuration: nil,
session: .init(plan: .plan1)
),
action: .init(id: .mockRandom()),
Expand Down Expand Up @@ -377,6 +381,7 @@ extension RUMLongTaskEvent: RandomMockable {
return RUMLongTaskEvent(
dd: .init(
browserSdkVersion: nil,
configuration: nil,
discarded: nil,
session: .init(plan: .plan1)
),
Expand Down
12 changes: 7 additions & 5 deletions DatadogInternal/Sources/Telemetry/Telemetry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public struct ConfigurationTelemetry: Equatable {
}

public enum TelemetryMessage {
case debug(id: String, message: String)
case debug(id: String, message: String, attributes: [String: Encodable]?)
case error(id: String, message: String, kind: String?, stack: String?)
case configuration(ConfigurationTelemetry)
}
Expand All @@ -52,8 +52,9 @@ extension Telemetry {
/// - Parameters:
/// - id: Identity of the debug log, this can be used to prevent duplicates.
/// - message: The debug message.
public func debug(id: String, message: String) {
send(telemetry: .debug(id: id, message: message))
/// - attributes: Custom attributes attached to the log (optional).
public func debug(id: String, message: String, attributes: [String: Encodable]? = nil) {
send(telemetry: .debug(id: id, message: message, attributes: attributes))
}

/// Collect execution error.
Expand Down Expand Up @@ -81,10 +82,11 @@ extension Telemetry {
///
/// - Parameters:
/// - message: The debug message.
/// - attributes: Custom attributes attached to the log (optional).
/// - file: The current file name.
/// - line: The line number in file.
public func debug(_ message: String, file: String = #file, line: Int = #line) {
debug(id: "\(file):\(line):\(message)", message: message)
public func debug(_ message: String, attributes: [String: Encodable]? = nil, file: String = #file, line: Int = #line) {
debug(id: "\(file):\(line):\(message)", message: message, attributes: attributes)
}

/// Collect execution error.
Expand Down
13 changes: 7 additions & 6 deletions DatadogInternal/Tests/Telemetry/TelemetryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class TelemetryTests: XCTestCase {
func testTelemetryDebug() {
// Given
class TelemetryTest: Telemetry {
var debug: (id: String, message: String)?
var debug: (id: String, message: String, attributes: [String: Encodable]?)?

func send(telemetry: DatadogInternal.TelemetryMessage) {
guard case .debug(let id, let message) = telemetry else {
guard case let .debug(id, message, attributes) = telemetry else {
return
}

debug = (id: id, message: message)
debug = (id: id, message: message, attributes: attributes)
}
}

Expand All @@ -32,12 +32,13 @@ class TelemetryTests: XCTestCase {

// When
#sourceLocation(file: "File.swift", line: 1)
telemetry.debug("debug message")
telemetry.debug("debug message", attributes: ["foo": "bar"])
#sourceLocation()

// Then
XCTAssertEqual(telemetry.debug?.id, "File.swift:1:debug message")
XCTAssertEqual(telemetry.debug?.message, "debug message")
XCTAssertEqual(telemetry.debug?.attributes as? [String: String], ["foo": "bar"])
}

func testTelemetryErrorFormatting() {
Expand All @@ -46,7 +47,7 @@ class TelemetryTests: XCTestCase {
var error: (id: String, message: String, kind: String?, stack: String?)?

func send(telemetry: DatadogInternal.TelemetryMessage) {
guard case .error(let id, let message, let kind, let stack) = telemetry else {
guard case let .error(id, message, kind, stack) = telemetry else {
return
}

Expand Down Expand Up @@ -202,7 +203,7 @@ class TelemetryTests: XCTestCase {
telemetry.debug("debug")

// Then
guard case .debug(_, let message) = receiver.telemetry else {
guard case .debug(_, let message, _) = receiver.telemetry else {
return XCTFail("A debug should be send to core.")
}
XCTAssertEqual(message, "debug")
Expand Down
111 changes: 110 additions & 1 deletion DatadogObjc/Sources/RUM/RUMDataModels+objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public class DDRUMActionEventDD: NSObject {
root.swiftModel.dd.browserSdkVersion
}

@objc public var configuration: DDRUMActionEventDDConfiguration? {
root.swiftModel.dd.configuration != nil ? DDRUMActionEventDDConfiguration(root: root) : nil
}

@objc public var formatVersion: NSNumber {
root.swiftModel.dd.formatVersion as NSNumber
}
Expand Down Expand Up @@ -173,6 +177,23 @@ public class DDRUMActionEventDDActionTarget: NSObject {
}
}

@objc
public class DDRUMActionEventDDConfiguration: NSObject {
internal let root: DDRUMActionEvent

internal init(root: DDRUMActionEvent) {
self.root = root
}

@objc public var sessionReplaySampleRate: NSNumber {
root.swiftModel.dd.configuration!.sessionReplaySampleRate as NSNumber
}

@objc public var sessionSampleRate: NSNumber {
root.swiftModel.dd.configuration!.sessionSampleRate as NSNumber
}
}

@objc
public class DDRUMActionEventDDSession: NSObject {
internal let root: DDRUMActionEvent
Expand Down Expand Up @@ -916,6 +937,10 @@ public class DDRUMErrorEventDD: NSObject {
root.swiftModel.dd.browserSdkVersion
}

@objc public var configuration: DDRUMErrorEventDDConfiguration? {
root.swiftModel.dd.configuration != nil ? DDRUMErrorEventDDConfiguration(root: root) : nil
}

@objc public var formatVersion: NSNumber {
root.swiftModel.dd.formatVersion as NSNumber
}
Expand All @@ -925,6 +950,23 @@ public class DDRUMErrorEventDD: NSObject {
}
}

@objc
public class DDRUMErrorEventDDConfiguration: NSObject {
internal let root: DDRUMErrorEvent

internal init(root: DDRUMErrorEvent) {
self.root = root
}

@objc public var sessionReplaySampleRate: NSNumber {
root.swiftModel.dd.configuration!.sessionReplaySampleRate as NSNumber
}

@objc public var sessionSampleRate: NSNumber {
root.swiftModel.dd.configuration!.sessionSampleRate as NSNumber
}
}

@objc
public class DDRUMErrorEventDDSession: NSObject {
internal let root: DDRUMErrorEvent
Expand Down Expand Up @@ -1886,6 +1928,10 @@ public class DDRUMLongTaskEventDD: NSObject {
root.swiftModel.dd.browserSdkVersion
}

@objc public var configuration: DDRUMLongTaskEventDDConfiguration? {
root.swiftModel.dd.configuration != nil ? DDRUMLongTaskEventDDConfiguration(root: root) : nil
}

@objc public var discarded: NSNumber? {
root.swiftModel.dd.discarded as NSNumber?
}
Expand All @@ -1899,6 +1945,23 @@ public class DDRUMLongTaskEventDD: NSObject {
}
}

@objc
public class DDRUMLongTaskEventDDConfiguration: NSObject {
internal let root: DDRUMLongTaskEvent

internal init(root: DDRUMLongTaskEvent) {
self.root = root
}

@objc public var sessionReplaySampleRate: NSNumber {
root.swiftModel.dd.configuration!.sessionReplaySampleRate as NSNumber
}

@objc public var sessionSampleRate: NSNumber {
root.swiftModel.dd.configuration!.sessionSampleRate as NSNumber
}
}

@objc
public class DDRUMLongTaskEventDDSession: NSObject {
internal let root: DDRUMLongTaskEvent
Expand Down Expand Up @@ -2503,6 +2566,10 @@ public class DDRUMResourceEventDD: NSObject {
root.swiftModel.dd.browserSdkVersion
}

@objc public var configuration: DDRUMResourceEventDDConfiguration? {
root.swiftModel.dd.configuration != nil ? DDRUMResourceEventDDConfiguration(root: root) : nil
}

@objc public var discarded: NSNumber? {
root.swiftModel.dd.discarded as NSNumber?
}
Expand All @@ -2528,6 +2595,23 @@ public class DDRUMResourceEventDD: NSObject {
}
}

@objc
public class DDRUMResourceEventDDConfiguration: NSObject {
internal let root: DDRUMResourceEvent

internal init(root: DDRUMResourceEvent) {
self.root = root
}

@objc public var sessionReplaySampleRate: NSNumber {
root.swiftModel.dd.configuration!.sessionReplaySampleRate as NSNumber
}

@objc public var sessionSampleRate: NSNumber {
root.swiftModel.dd.configuration!.sessionSampleRate as NSNumber
}
}

@objc
public class DDRUMResourceEventDDSession: NSObject {
internal let root: DDRUMResourceEvent
Expand Down Expand Up @@ -3441,6 +3525,10 @@ public class DDRUMViewEventDD: NSObject {
root.swiftModel.dd.browserSdkVersion
}

@objc public var configuration: DDRUMViewEventDDConfiguration? {
root.swiftModel.dd.configuration != nil ? DDRUMViewEventDDConfiguration(root: root) : nil
}

@objc public var documentVersion: NSNumber {
root.swiftModel.dd.documentVersion as NSNumber
}
Expand All @@ -3462,6 +3550,23 @@ public class DDRUMViewEventDD: NSObject {
}
}

@objc
public class DDRUMViewEventDDConfiguration: NSObject {
internal let root: DDRUMViewEvent

internal init(root: DDRUMViewEvent) {
self.root = root
}

@objc public var sessionReplaySampleRate: NSNumber {
root.swiftModel.dd.configuration!.sessionReplaySampleRate as NSNumber
}

@objc public var sessionSampleRate: NSNumber {
root.swiftModel.dd.configuration!.sessionSampleRate as NSNumber
}
}

@objc
public class DDRUMViewEventDDPageStates: NSObject {
internal var swiftModel: RUMViewEvent.DD.PageStates
Expand Down Expand Up @@ -4810,6 +4915,10 @@ public class DDTelemetryDebugEventTelemetry: NSObject {
@objc public var type: String? {
root.swiftModel.telemetry.type
}

@objc public var telemetryInfo: [String: Any] {
root.swiftModel.telemetry.telemetryInfo.castToObjectiveC()
}
}

@objc
Expand Down Expand Up @@ -5325,4 +5434,4 @@ public class DDTelemetryConfigurationEventView: NSObject {

// swiftlint:enable force_unwrapping

// Generated from https://github.com/DataDog/rum-events-format/tree/1c5eaa897c065e5f790a5f8aaf6fc8782d706051
// Generated from https://github.com/DataDog/rum-events-format/tree/2b1615693d269368ed91f061103ee98bfecafb00
Loading