|
| 1 | +import { ErrorCodes, LogRequestBody, WSDKErrorSeverity } from "./types"; |
| 2 | +import { FetchUploader, IFetchPayload } from "../uploaders"; |
| 3 | +import { IStore, SDKConfig } from "../store"; |
| 4 | +import { SDKInitConfig } from "../sdkRuntimeModels"; |
| 5 | +import Constants from "../constants"; |
| 6 | + |
| 7 | +// Header key constants |
| 8 | +const HEADER_ACCEPT = 'Accept' as const; |
| 9 | +const HEADER_CONTENT_TYPE = 'Content-Type' as const; |
| 10 | +const HEADER_ROKT_LAUNCHER_VERSION = 'rokt-launcher-version' as const; |
| 11 | +const HEADER_ROKT_LAUNCHER_INSTANCE_GUID = 'rokt-launcher-instance-guid' as const; |
| 12 | +const HEADER_ROKT_WSDK_VERSION = 'rokt-wsdk-version' as const; |
| 13 | + |
| 14 | +interface IReportingLoggerPayload extends IFetchPayload { |
| 15 | + headers: IFetchPayload['headers'] & { |
| 16 | + [HEADER_ROKT_LAUNCHER_INSTANCE_GUID]?: string; |
| 17 | + [HEADER_ROKT_LAUNCHER_VERSION]: string; |
| 18 | + [HEADER_ROKT_WSDK_VERSION]: string; |
| 19 | + }; |
| 20 | + body: string; |
| 21 | +} |
| 22 | + |
| 23 | +export class ReportingLogger { |
| 24 | + private readonly isEnabled: boolean; |
| 25 | + private readonly reporter: string = 'mp-wsdk'; |
| 26 | + private readonly rateLimiter: IRateLimiter; |
| 27 | + private store: IStore | null; |
| 28 | + private readonly loggingUrl: string; |
| 29 | + private readonly errorUrl: string; |
| 30 | + private readonly isLoggingEnabled: boolean; |
| 31 | + |
| 32 | + constructor( |
| 33 | + config: SDKConfig | SDKInitConfig | any, |
| 34 | + private readonly sdkVersion: string, |
| 35 | + store?: IStore, |
| 36 | + private readonly launcherInstanceGuid?: string, |
| 37 | + rateLimiter?: IRateLimiter, |
| 38 | + ) { |
| 39 | + this.loggingUrl = `https://${config.loggingUrl || Constants.DefaultBaseUrls.loggingUrl}`; |
| 40 | + this.errorUrl = `https://${config.errorUrl || Constants.DefaultBaseUrls.errorUrl}`; |
| 41 | + this.isLoggingEnabled = config.isLoggingEnabled || false; |
| 42 | + this.store = store ?? null; |
| 43 | + this.isEnabled = this.isReportingEnabled(); |
| 44 | + this.rateLimiter = rateLimiter ?? new RateLimiter(); |
| 45 | + } |
| 46 | + |
| 47 | + public setStore(store: IStore): void { |
| 48 | + this.store = store; |
| 49 | + } |
| 50 | + |
| 51 | + public info(msg: string, code?: ErrorCodes) { |
| 52 | + this.sendLog(WSDKErrorSeverity.INFO, msg, code); |
| 53 | + } |
| 54 | + |
| 55 | + public error(msg: string, code?: ErrorCodes, stackTrace?: string) { |
| 56 | + this.sendError(WSDKErrorSeverity.ERROR, msg, code, stackTrace); |
| 57 | + } |
| 58 | + |
| 59 | + public warning(msg: string, code?: ErrorCodes) { |
| 60 | + this.sendError(WSDKErrorSeverity.WARNING, msg, code); |
| 61 | + } |
| 62 | + |
| 63 | + private sendToServer(url: string, severity: WSDKErrorSeverity, msg: string, code?: ErrorCodes, stackTrace?: string): void { |
| 64 | + if (!this.canSendLog(severity)) |
| 65 | + return; |
| 66 | + |
| 67 | + try { |
| 68 | + const logRequest = this.buildLogRequest(severity, msg, code, stackTrace); |
| 69 | + const uploader = new FetchUploader(url); |
| 70 | + const payload: IReportingLoggerPayload = { |
| 71 | + method: 'POST', |
| 72 | + headers: this.getHeaders(), |
| 73 | + body: JSON.stringify(logRequest), |
| 74 | + }; |
| 75 | + uploader.upload(payload).catch((error) => { |
| 76 | + console.error('ReportingLogger: Failed to send log', error); |
| 77 | + }); |
| 78 | + } catch (error) { |
| 79 | + console.error('ReportingLogger: Failed to send log', error); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private sendLog(severity: WSDKErrorSeverity, msg: string, code?: ErrorCodes, stackTrace?: string): void { |
| 84 | + this.sendToServer(this.loggingUrl, severity, msg, code, stackTrace); |
| 85 | + } |
| 86 | + |
| 87 | + private sendError(severity: WSDKErrorSeverity, msg: string, code?: ErrorCodes, stackTrace?: string): void { |
| 88 | + this.sendToServer(this.errorUrl, severity, msg, code, stackTrace); |
| 89 | + } |
| 90 | + |
| 91 | + private buildLogRequest(severity: WSDKErrorSeverity, msg: string, code?: ErrorCodes, stackTrace?: string): LogRequestBody { |
| 92 | + return { |
| 93 | + additionalInformation: { |
| 94 | + message: msg, |
| 95 | + version: this.getVersion(), |
| 96 | + }, |
| 97 | + severity: severity, |
| 98 | + code: code ?? ErrorCodes.UNKNOWN_ERROR, |
| 99 | + url: this.getUrl(), |
| 100 | + deviceInfo: this.getUserAgent(), |
| 101 | + stackTrace: stackTrace, |
| 102 | + reporter: this.reporter, |
| 103 | + // Integration will be set to integrationName once the kit connects via RoktManager.attachKit() |
| 104 | + integration: this.store?.getIntegrationName() ?? 'mp-wsdk' |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + private getVersion(): string { |
| 109 | + return this.store?.getIntegrationName?.() ?? `mParticle_wsdkv_${this.sdkVersion}`; |
| 110 | + } |
| 111 | + |
| 112 | + private isReportingEnabled(): boolean { |
| 113 | + return this.isDebugModeEnabled() || |
| 114 | + (this.isRoktDomainPresent() && this.isFeatureFlagEnabled()); |
| 115 | + } |
| 116 | + |
| 117 | + private isRoktDomainPresent(): boolean { |
| 118 | + return typeof window !== 'undefined' && Boolean(window['ROKT_DOMAIN']); |
| 119 | + } |
| 120 | + |
| 121 | + private isFeatureFlagEnabled = (): boolean => this.isLoggingEnabled; |
| 122 | + |
| 123 | + private isDebugModeEnabled(): boolean { |
| 124 | + return ( |
| 125 | + typeof window !== 'undefined' && |
| 126 | + (window. |
| 127 | + location?. |
| 128 | + search?. |
| 129 | + toLowerCase()?. |
| 130 | + includes('mp_enable_logging=true') ?? false) |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + private canSendLog(severity: WSDKErrorSeverity): boolean { |
| 135 | + return this.isEnabled && !this.isRateLimited(severity); |
| 136 | + } |
| 137 | + |
| 138 | + private isRateLimited(severity: WSDKErrorSeverity): boolean { |
| 139 | + return this.rateLimiter.incrementAndCheck(severity); |
| 140 | + } |
| 141 | + |
| 142 | + private getUrl(): string | undefined { |
| 143 | + return typeof window !== 'undefined' ? window.location?.href : undefined; |
| 144 | + } |
| 145 | + |
| 146 | + private getUserAgent(): string | undefined { |
| 147 | + return typeof window !== 'undefined' ? window.navigator?.userAgent : undefined; |
| 148 | + } |
| 149 | + |
| 150 | + private getHeaders(): IReportingLoggerPayload['headers'] { |
| 151 | + const headers: IReportingLoggerPayload['headers'] = { |
| 152 | + [HEADER_ACCEPT]: 'text/plain;charset=UTF-8', |
| 153 | + [HEADER_CONTENT_TYPE]: 'application/json', |
| 154 | + [HEADER_ROKT_LAUNCHER_VERSION]: this.getVersion(), |
| 155 | + [HEADER_ROKT_WSDK_VERSION]: 'joint', |
| 156 | + }; |
| 157 | + |
| 158 | + if (this.launcherInstanceGuid) { |
| 159 | + headers[HEADER_ROKT_LAUNCHER_INSTANCE_GUID] = this.launcherInstanceGuid; |
| 160 | + } |
| 161 | + |
| 162 | + const accountId = this.store?.getRoktAccountId?.(); |
| 163 | + if (accountId) { |
| 164 | + headers['rokt-account-id'] = accountId; |
| 165 | + } |
| 166 | + |
| 167 | + return headers; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +export interface IRateLimiter { |
| 172 | + incrementAndCheck(severity: WSDKErrorSeverity): boolean; |
| 173 | +} |
| 174 | + |
| 175 | +export class RateLimiter implements IRateLimiter { |
| 176 | + private readonly rateLimits: Map<WSDKErrorSeverity, number> = new Map([ |
| 177 | + [WSDKErrorSeverity.ERROR, 10], |
| 178 | + [WSDKErrorSeverity.WARNING, 10], |
| 179 | + [WSDKErrorSeverity.INFO, 10], |
| 180 | + ]); |
| 181 | + private logCount: Map<WSDKErrorSeverity, number> = new Map(); |
| 182 | + |
| 183 | + public incrementAndCheck(severity: WSDKErrorSeverity): boolean { |
| 184 | + const count = this.logCount.get(severity) || 0; |
| 185 | + const limit = this.rateLimits.get(severity) || 10; |
| 186 | + |
| 187 | + const newCount = count + 1; |
| 188 | + this.logCount.set(severity, newCount); |
| 189 | + |
| 190 | + return newCount > limit; |
| 191 | + } |
| 192 | +} |
0 commit comments