From 8097a1edfbdc1fdef35d8682ea6fe8734854c67d Mon Sep 17 00:00:00 2001 From: Arun Mishra <68141066+arun-mi@users.noreply.github.com> Date: Wed, 3 Jan 2024 14:16:33 +0530 Subject: [PATCH] feat:HS-186: added metadata support for logs (#88) Co-authored-by: arun.mishra --- src/LoaderController.res | 6 +- src/orca-loader/Elements.res | 2 + src/orca-loader/Hyper.res | 8 +++ src/orca-log-catcher/ErrorBoundary.res | 1 + src/orca-log-catcher/OrcaLogger.res | 79 ++++++++++++++++---------- 5 files changed, 66 insertions(+), 30 deletions(-) diff --git a/src/LoaderController.res b/src/LoaderController.res index 92e7e0c76..15e4e887f 100644 --- a/src/LoaderController.res +++ b/src/LoaderController.res @@ -211,11 +211,15 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger) => { let publishableKey = dict->getString("publishableKey", "") logger.setMerchantId(publishableKey) } + if dict->getDictIsSome("endpoint") { let endpoint = dict->getString("endpoint", "") ApiEndpoint.setApiEndPoint(endpoint) } - + if dict->getDictIsSome("analyticsMetadata") { + let metadata = dict->getJsonObjectFromDict("analyticsMetadata") + logger.setMetadata(metadata) + } if dict->getDictIsSome("paymentOptions") { let paymentOptions = dict->Utils.getDictFromObj("paymentOptions") diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index 1be3f7693..7db98a48e 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -21,6 +21,7 @@ let make = ( ~sdkSessionId, ~publishableKey, ~logger: option, + ~analyticsMetadata, ) => { let handleApplePayMessages = ref(_ => ()) let applePaySessionRef = ref(Js.Nullable.null) @@ -225,6 +226,7 @@ let make = ( ("sdkSessionId", sdkSessionId->Js.Json.string), ("sdkHandleConfirmPayment", sdkHandleConfirmPayment->Js.Json.boolean), ("parentURL", "*"->Js.Json.string), + ("analyticsMetadata", analyticsMetadata), ]->Js.Dict.fromArray let handleApplePayMounted = (event: Types.event) => { diff --git a/src/orca-loader/Hyper.res b/src/orca-loader/Hyper.res index 66dbcbd05..f9fcc987d 100644 --- a/src/orca-loader/Hyper.res +++ b/src/orca-loader/Hyper.res @@ -75,6 +75,12 @@ let make = (publishableKey, options: option, analyticsInfo: optionBelt.Option.getWithDefault(Js.Json.null) ->Utils.getDictFromJson ->Utils.getBool("isPreloadEnabled", true) + let analyticsMetadata = + options + ->Belt.Option.getWithDefault(Js.Json.null) + ->Utils.getDictFromJson + ->Utils.getDictFromObj("analytics") + ->Utils.getJsonObjectFromDict("metadata") if isPreloadEnabled { preloader() } @@ -88,6 +94,7 @@ let make = (publishableKey, options: option, analyticsInfo: option, analyticsInfo: option unit, setClientSecret: string => unit, setMerchantId: string => unit, + setMetadata: Js.Json.t => unit, } let defaultLoggerConfig = { @@ -215,6 +217,7 @@ let defaultLoggerConfig = { setLogInitiated: () => (), setMerchantId: _x => (), setSessionId: _x => (), + setMetadata: _x => (), } let logFileToObj = logFile => { @@ -374,7 +377,14 @@ let browserDetect = content => { let arrayOfNameAndVersion = Js.String2.split(Window.userAgent->browserDetect, "-") -let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantId=?, ()) => { +let make = ( + ~sessionId=?, + ~source: option=?, + ~clientSecret=?, + ~merchantId=?, + ~metadata=?, + (), +) => { let loggingLevel = switch GlobalVars.loggingLevelStr { | "DEBUG" => DEBUG | "INFO" => INFO @@ -401,6 +411,12 @@ let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantI merchantId := value } + let metadata = ref(metadata->Belt.Option.getWithDefault(Js.Json.null)) + + let setMetadata = value => { + metadata := value + } + let conditionalLogPush = (log: logFile) => { if GlobalVars.enableLogging { switch loggingLevel { @@ -532,13 +548,13 @@ let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantI let localTimestampFloat = localTimestamp->Belt.Float.fromString->Belt.Option.getWithDefault(Js.Date.now()) { - logType: logType, + logType, timestamp: localTimestamp, sessionId: sessionId.contents, source: sourceString, version: GlobalVars.repoVersion, - value: value, - internalMetadata: internalMetadata, + value, + internalMetadata, category: logCategory, paymentId: Js.String2.split(clientSecret.contents, "_secret_") ->Belt.Array.get(0) @@ -550,9 +566,10 @@ let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantI userAgent: Window.userAgent, appId: "", eventName: eventNameStr, - latency: latency, - paymentMethod: paymentMethod, - firstEvent: firstEvent, + latency, + paymentMethod, + firstEvent, + metadata: metadata.contents, } ->conditionalLogPush ->ignore @@ -584,7 +601,7 @@ let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantI let localTimestampFloat = localTimestamp->Belt.Float.fromString->Belt.Option.getWithDefault(Js.Date.now()) { - logType: logType, + logType, timestamp: localTimestamp, sessionId: sessionId.contents, source: sourceString, @@ -608,9 +625,10 @@ let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantI userAgent: Window.userAgent, appId: "", eventName: eventNameStr, - latency: latency, - paymentMethod: paymentMethod, - firstEvent: firstEvent, + latency, + paymentMethod, + firstEvent, + metadata: metadata.contents, } ->conditionalLogPush ->ignore @@ -635,13 +653,13 @@ let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantI let localTimestampFloat = localTimestamp->Belt.Float.fromString->Belt.Option.getWithDefault(Js.Date.now()) { - logType: logType, + logType, timestamp: localTimestamp, sessionId: sessionId.contents, source: sourceString, version: GlobalVars.repoVersion, - value: value, - internalMetadata: internalMetadata, + value, + internalMetadata, category: logCategory, paymentId: Js.String2.split(clientSecret.contents, "_secret_") ->Belt.Array.get(0) @@ -653,9 +671,10 @@ let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantI userAgent: Window.userAgent, appId: "", eventName: eventNameStr, - latency: latency, - paymentMethod: paymentMethod, - firstEvent: firstEvent, + latency, + paymentMethod, + firstEvent, + metadata: metadata.contents, } ->conditionalLogPush ->ignore @@ -669,7 +688,7 @@ let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantI let latency = calculateLatencyHook(~eventName, ()) { logType: INFO, - eventName: eventName, + eventName, timestamp: Js.Date.now()->Belt.Float.toString, sessionId: sessionId.contents, source: sourceString, @@ -686,9 +705,10 @@ let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantI platform: Window.platform, userAgent: Window.userAgent, appId: "", - latency: latency, + latency, paymentMethod: "", - firstEvent: firstEvent, + firstEvent, + metadata: metadata.contents, } ->conditionalLogPush ->ignore @@ -707,14 +727,15 @@ let make = (~sessionId=?, ~source: option=?, ~clientSecret=?, ~merchantI Window.addEventListener("beforeunload", handleBeforeUnload) { - setLogInfo: setLogInfo, - setLogInitiated: setLogInitiated, - setConfirmPaymentValue: setConfirmPaymentValue, - sendLogs: sendLogs, - setSessionId: setSessionId, - setClientSecret: setClientSecret, - setMerchantId: setMerchantId, - setLogApi: setLogApi, - setLogError: setLogError, + setLogInfo, + setLogInitiated, + setConfirmPaymentValue, + sendLogs, + setSessionId, + setClientSecret, + setMerchantId, + setMetadata, + setLogApi, + setLogError, } }