Skip to content

Commit

Permalink
RUMM-1791 PR comments addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
buranmert committed Dec 28, 2021
1 parent 5b03879 commit e10f099
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public extension WKUserContentController {
userLogsWriter: loggingFeature.storage.writer,
internalLogsWriter: InternalMonitoringFeature.instance?.logsStorage.writer,
dateCorrector: loggingFeature.dateCorrector,
rumContextProvider: contextProvider
rumContextProvider: contextProvider,
applicationVersion: loggingFeature.configuration.common.applicationVersion,
environment: loggingFeature.configuration.common.environment
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ internal class WebLogEventConsumer: WebEventConsumer {
private let internalLogsWriter: Writer?
private let dateCorrector: DateCorrectorType
private let rumContextProvider: RUMContextProvider?
private let applicationVersion: String
private let environment: String

private let jsonDecoder = JSONDecoder()

private lazy var ddTags: String = {
let versionKey = LogEventEncoder.StaticCodingKeys.applicationVersion.rawValue
let versionValue = LoggingFeature.instance?.configuration.common.applicationVersion ?? ""
let versionValue = applicationVersion
let envKey = LogEventEncoder.StaticCodingKeys.environment.rawValue
let envValue = LoggingFeature.instance?.configuration.common.environment ?? ""
let envValue = environment

return "\(versionKey):\(versionValue),\(envKey):\(envValue)"
}()
Expand All @@ -36,12 +39,16 @@ internal class WebLogEventConsumer: WebEventConsumer {
userLogsWriter: Writer,
internalLogsWriter: Writer?,
dateCorrector: DateCorrectorType,
rumContextProvider: RUMContextProvider?
rumContextProvider: RUMContextProvider?,
applicationVersion: String,
environment: String
) {
self.userLogsWriter = userLogsWriter
self.internalLogsWriter = internalLogsWriter
self.dateCorrector = dateCorrector
self.rumContextProvider = rumContextProvider
self.applicationVersion = applicationVersion
self.environment = environment
}

func consume(event: JSON, eventType: String) throws {
Expand Down Expand Up @@ -71,6 +78,8 @@ internal class WebLogEventConsumer: WebEventConsumer {
userLogsWriter.write(value: encodableEvent)
} else if eventType == Constants.internalLogEventType {
internalLogsWriter?.write(value: encodableEvent)
} else {
userLogger.error("🔥 Invalid Web Event Type: \(eventType)")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ class WebLogEventConsumerTests: XCTestCase {
let mockSessionID = UUID(uuidString: "e9796469-c2a1-43d6-b0f6-65c47d33cf5f")!
mockContextProvider.context.sessionID = RUMUUID(rawValue: mockSessionID)
mockDateCorrector.correctionOffset = 123
let applicationVersion = String.mockRandom()
let environment = String.mockRandom()
let eventConsumer = WebLogEventConsumer(
userLogsWriter: mockUserLogsWriter,
internalLogsWriter: mockInternalLogsWriter,
dateCorrector: mockDateCorrector,
rumContextProvider: mockContextProvider
rumContextProvider: mockContextProvider,
applicationVersion: applicationVersion,
environment: environment
)

let webLogEvent: JSON = [
Expand All @@ -39,7 +43,7 @@ class WebLogEventConsumerTests: XCTestCase {
"application_id": "123456",
"session_id": mockSessionID.uuidString.lowercased(),
"status": "error",
"ddtags": "version:,env:",
"ddtags": "version:\(applicationVersion),env:\(environment)",
"view": ["referrer": "", "url": "https://datadoghq.dev/browser-sdk-test-playground"]
]

Expand All @@ -57,11 +61,15 @@ class WebLogEventConsumerTests: XCTestCase {
let mockSessionID = UUID(uuidString: "e9796469-c2a1-43d6-b0f6-65c47d33cf5f")!
mockContextProvider.context.sessionID = RUMUUID(rawValue: mockSessionID)
mockDateCorrector.correctionOffset = 123
let applicationVersion = String.mockRandom()
let environment = String.mockRandom()
let eventConsumer = WebLogEventConsumer(
userLogsWriter: mockUserLogsWriter,
internalLogsWriter: mockInternalLogsWriter,
dateCorrector: mockDateCorrector,
rumContextProvider: mockContextProvider
rumContextProvider: mockContextProvider,
applicationVersion: applicationVersion,
environment: environment
)

let webLogEvent: JSON = [
Expand All @@ -79,7 +87,7 @@ class WebLogEventConsumerTests: XCTestCase {
"application_id": "123456",
"session_id": mockSessionID.uuidString.lowercased(),
"status": "error",
"ddtags": "version:,env:",
"ddtags": "version:\(applicationVersion),env:\(environment)",
"view": ["referrer": "", "url": "https://datadoghq.dev/browser-sdk-test-playground"]
]

Expand All @@ -94,11 +102,18 @@ class WebLogEventConsumerTests: XCTestCase {
}

func testWhenInvalidEventTypePassed_itIgnoresEvent() throws {
let previousUserLogger = userLogger
defer { userLogger = previousUserLogger }
let userLoggerOutput = LogOutputMock()
userLogger = .mockWith(logOutput: userLoggerOutput)

let eventConsumer = WebLogEventConsumer(
userLogsWriter: mockUserLogsWriter,
internalLogsWriter: mockInternalLogsWriter,
dateCorrector: mockDateCorrector,
rumContextProvider: mockContextProvider
rumContextProvider: mockContextProvider,
applicationVersion: .mockRandom(),
environment: .mockRandom()
)

let webLogEvent: JSON = [
Expand All @@ -111,14 +126,19 @@ class WebLogEventConsumerTests: XCTestCase {

XCTAssertNil(mockUserLogsWriter.dataWritten)
XCTAssertNil(mockInternalLogsWriter.dataWritten)
XCTAssertEqual(userLoggerOutput.recordedLog?.message, "🔥 Invalid Web Event Type: invalid_log")
}

func testWhenContextIsUnavailable_itPassesEventAsIs() throws {
let applicationVersion = String.mockRandom()
let environment = String.mockRandom()
let eventConsumer = WebLogEventConsumer(
userLogsWriter: mockUserLogsWriter,
internalLogsWriter: mockInternalLogsWriter,
dateCorrector: mockDateCorrector,
rumContextProvider: nil
rumContextProvider: nil,
applicationVersion: applicationVersion,
environment: environment
)

let webLogEvent: JSON = [
Expand All @@ -130,7 +150,7 @@ class WebLogEventConsumerTests: XCTestCase {
"view": ["referrer": "", "url": "https://datadoghq.dev/browser-sdk-test-playground"]
]
var expectedWebLogEvent: JSON = webLogEvent
expectedWebLogEvent["ddtags"] = "version:,env:"
expectedWebLogEvent["ddtags"] = "version:\(applicationVersion),env:\(environment)"

try eventConsumer.consume(event: webLogEvent, eventType: "log")

Expand Down

0 comments on commit e10f099

Please sign in to comment.