Skip to content

Commit

Permalink
RUM-4311: Add Logs event mapper to ObjC API
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnm committed Aug 21, 2024
1 parent 93fd18c commit e63ed62
Show file tree
Hide file tree
Showing 6 changed files with 629 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,8 @@
E2AA55E82C32C6D9002FEF28 /* ApplicationNotifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2AA55E62C32C6D9002FEF28 /* ApplicationNotifications.swift */; };
E2AA55EA2C32C76A002FEF28 /* WatchKitExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2AA55E92C32C76A002FEF28 /* WatchKitExtensions.swift */; };
E2AA55EC2C32C78B002FEF28 /* WatchKitExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2AA55E92C32C76A002FEF28 /* WatchKitExtensions.swift */; };
F6E106542C75E0D000716DC6 /* LogsDataModels+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6E106532C75E0D000716DC6 /* LogsDataModels+objc.swift */; };
F6E106552C75E0D000716DC6 /* LogsDataModels+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6E106532C75E0D000716DC6 /* LogsDataModels+objc.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -3051,6 +3053,7 @@
E2AA55E62C32C6D9002FEF28 /* ApplicationNotifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApplicationNotifications.swift; sourceTree = "<group>"; };
E2AA55E92C32C76A002FEF28 /* WatchKitExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WatchKitExtensions.swift; sourceTree = "<group>"; };
F637AED12697404200516F32 /* UIKitRUMUserActionsPredicate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKitRUMUserActionsPredicate.swift; sourceTree = "<group>"; };
F6E106532C75E0D000716DC6 /* LogsDataModels+objc.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "LogsDataModels+objc.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -6279,6 +6282,7 @@
isa = PBXGroup;
children = (
61133C0C2423983800786299 /* Logs+objc.swift */,
F6E106532C75E0D000716DC6 /* LogsDataModels+objc.swift */,
);
path = Logs;
sourceTree = "<group>";
Expand Down Expand Up @@ -8278,6 +8282,7 @@
9E55407C25812D1C00F6E3AD /* RUM+objc.swift in Sources */,
D2A434AA2A8E40A20028E329 /* SessionReplay+objc.swift in Sources */,
615A4A8D24A356A000233986 /* OTSpanContext+objc.swift in Sources */,
F6E106542C75E0D000716DC6 /* LogsDataModels+objc.swift in Sources */,
61133C112423983800786299 /* DatadogConfiguration+objc.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -9528,6 +9533,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F6E106552C75E0D000716DC6 /* LogsDataModels+objc.swift in Sources */,
D2CB6F9927C5217A00A62B57 /* Casting.swift in Sources */,
D2CB6F9A27C5217A00A62B57 /* RUMDataModels+objc.swift in Sources */,
D2CB6F9B27C5217A00A62B57 /* DDSpanContext+objc.swift in Sources */,
Expand Down
18 changes: 18 additions & 0 deletions DatadogCore/Tests/DatadogObjc/DDLogsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ class DDLogsTests: XCTestCase {
XCTAssertEqual(objcConfig.configuration.remoteSampleRate, 50)
XCTAssertNotNil(objcConfig.configuration.consoleLogFormat)
}

func testEventMapping() throws {
let logsConfiguration = DDLogsConfiguration()
logsConfiguration.setEventMapper { logEvent in
logEvent.message = "custom-log-message"
logEvent.attributes.userAttributes["custom-attribute"] = "custom-value"
return logEvent
}
DDLogs.enable(with: logsConfiguration)

let objcLogger = DDLogger.create()

objcLogger.debug("message")

let logMatchers = try core.waitAndReturnLogMatchers()
logMatchers[0].assertMessage(equals: "custom-log-message")
logMatchers[0].assertAttributes(equal: ["custom-attribute": "custom-value"])
}
}
// swiftlint:enable multiline_arguments_brackets
// swiftlint:enable compiler_protocol_init
12 changes: 12 additions & 0 deletions DatadogObjc/Sources/Logs/Logs+objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public class DDLogsConfiguration: NSObject {
customEndpoint: customEndpoint
)
}

/// Sets the custom mapper for `DDLogEvent`. This can be used to modify logs before they are send to Datadog.
///
/// The implementation should obtain a mutable version of the `DDLogEvent`, modify it and return it. Returning `nil` will result
/// with dropping the Log event entirely, so it won't be send to Datadog.
@objc
public func setEventMapper(_ mapper: @escaping (DDLogEvent) -> DDLogEvent?) {
configuration.eventMapper = { swiftEvent in
let objcEvent = DDLogEvent(swiftModel: swiftEvent)
return mapper(objcEvent)?.swiftModel
}
}
}

@objc
Expand Down
Loading

0 comments on commit e63ed62

Please sign in to comment.