Skip to content

Commit

Permalink
RUMM-3458 Add attributes: parameter to DEBUG telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
ncreated committed Jul 18, 2023
1 parent 0d5ac17 commit a2267db
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
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
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
11 changes: 6 additions & 5 deletions DatadogRUM/Sources/Integrations/TelemetryReceiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ internal final class TelemetryReceiver: FeatureMessageReceiver {
/// - Returns: Always `true`.
func receive(telemetry: TelemetryMessage, from core: DatadogCoreProtocol) -> Bool {
switch telemetry {
case .debug(let id, let message):
debug(id: id, message: message, in: core)
case .error(let id, let message, let kind, let stack):
case let .debug(id, message, attributes):
debug(id: id, message: message, attributes: attributes, in: core)
case let .error(id, message, kind, stack):
error(id: id, message: message, kind: kind, stack: stack, in: core)
case .configuration(let configuration):
send(configuration: configuration, in: core)
Expand All @@ -82,7 +82,8 @@ internal final class TelemetryReceiver: FeatureMessageReceiver {
/// - Parameters:
/// - id: Identity of the debug log, this can be used to prevent duplicates.
/// - message: The debug message.
func debug(id: String, message: String, in core: DatadogCoreProtocol) {
/// - attributes: Custom attributes attached to the log (optional).
func debug(id: String, message: String, attributes: [String: Encodable]?, in core: DatadogCoreProtocol) {
let date = dateProvider.now

record(event: id, in: core) { context, writer in
Expand All @@ -104,7 +105,7 @@ internal final class TelemetryReceiver: FeatureMessageReceiver {
source: .init(rawValue: context.source) ?? .ios,
telemetry: .init(
message: message,
telemetryInfo: [:]
telemetryInfo: attributes ?? [:]
),
version: context.sdkVersion,
view: viewId.map { .init(id: $0) }
Expand Down
5 changes: 3 additions & 2 deletions TestUtilities/Mocks/TelemetryMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public class TelemetryMock: Telemetry, CustomStringConvertible {
messages.append(telemetry)

switch telemetry {
case .debug(_, let message):
description.append("\n- [debug] \(message)")
case .debug(_, let message, let attributes):
let attributesString = attributes.map({ ", \($0)" }) ?? ""
description.append("\n- [debug] \(message)" + attributesString)
case .error(_, let message, let kind, let stack):
description.append("\n - [error] \(message), kind: \(kind ?? "nil"), stack: \(stack ?? "nil")")
case .configuration(let configuration):
Expand Down

0 comments on commit a2267db

Please sign in to comment.