Skip to content

Commit b3525cd

Browse files
Revised implementation
1 parent b972c57 commit b3525cd

File tree

7 files changed

+308
-129
lines changed

7 files changed

+308
-129
lines changed

src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class Logger {
3434
if (this.logger.warning &&
3535
(this.logLevel === LogLevelType.Verbose || this.logLevel === LogLevelType.Warning)) {
3636
this.logger.warning(msg);
37-
this.reportingLogger.warning(msg, code);
37+
this.reportingLogger?.warning(msg, code);
3838
}
3939
}
4040

src/logging/errorCodes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { valueof } from '../utils';
33
export type ErrorCodes = valueof<typeof ErrorCodes>;
44

55
export const ErrorCodes = {
6+
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
67
UNHANDLED_EXCEPTION: 'UNHANDLED_EXCEPTION',
78
} as const;

src/logging/reportingLogger.ts

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,95 @@
11
import { ErrorCodes } from "./errorCodes";
22
import { LogRequestBody, WSDKErrorSeverity } from "./logRequest";
33
import { FetchUploader, IFetchPayload } from "../uploaders";
4+
import { IStore, SDKConfig } from "../store";
45

56
// QUESTION: Should we collapse the interface with the class?
67
export interface IReportingLogger {
7-
error(msg: string, code: ErrorCodes, stackTrace?: string): void;
8-
warning(msg: string, code: ErrorCodes): void;
8+
error(msg: string, code?: ErrorCodes, stackTrace?: string): void;
9+
warning(msg: string, code?: ErrorCodes): void;
910
}
1011

1112
export class ReportingLogger implements IReportingLogger {
1213
private readonly isEnabled: boolean;
1314
private readonly reporter: string = 'mp-wsdk';
1415
private readonly integration: string = 'mp-wsdk';
1516
private readonly rateLimiter: IRateLimiter;
16-
private loggingUrl: string;
17-
private errorUrl: string;
18-
private accountId: string;
1917
private integrationName: string;
18+
private store: IStore;
2019

2120
constructor(
21+
private readonly config: SDKConfig,
2222
private readonly sdkVersion: string,
23-
rateLimiter?: IRateLimiter, // QUESTION: Do we need this in the constructor?
2423
private readonly launcherInstanceGuid?: string,
24+
rateLimiter?: IRateLimiter,
2525
) {
2626
this.isEnabled = this.isReportingEnabled();
27-
console.warn('ReportingLogger: isEnabled', this.isEnabled);
2827
this.rateLimiter = rateLimiter ?? new RateLimiter();
2928
}
3029

31-
public setLoggingUrl(url: string) {
32-
this.loggingUrl = url;
33-
}
34-
35-
public setErrorUrl(url: string) {
36-
this.errorUrl = url;
37-
}
38-
39-
public setAccountId(accountId: string) {
40-
this.accountId = accountId;
41-
}
42-
4330
public setIntegrationName(integrationName: string) {
4431
this.integrationName = integrationName;
4532
}
33+
34+
public setStore(store: IStore) {
35+
this.store = store;
36+
}
4637

47-
// TODO: Add an `info` method to the logger for `/v1/log`
38+
public info(msg: string, code?: ErrorCodes) {
39+
this.sendLog(WSDKErrorSeverity.INFO, msg, code);
40+
}
4841

49-
public error(msg: string, code: ErrorCodes, stackTrace?: string) {
50-
this.sendLog(WSDKErrorSeverity.ERROR, msg, code, stackTrace);
42+
public error(msg: string, code?: ErrorCodes, stackTrace?: string) {
43+
this.sendError(WSDKErrorSeverity.ERROR, msg, code, stackTrace);
5144
};
5245

53-
public warning(msg: string, code: ErrorCodes) {
54-
this.sendLog(WSDKErrorSeverity.WARNING, msg, code);
46+
public warning(msg: string, code?: ErrorCodes) {
47+
this.sendError(WSDKErrorSeverity.WARNING, msg, code);
5548
};
5649

57-
private getVersion(): string {
58-
return this.integrationName ?? this.sdkVersion;
59-
}
60-
61-
// QUESTION: Should we split this into `sendError` and `sendLog`?
62-
private sendLog(
63-
severity: WSDKErrorSeverity,
64-
msg: string,
65-
code: ErrorCodes,
66-
stackTrace?: string
67-
): void {
50+
private sendToServer(url: string,severity: WSDKErrorSeverity, msg: string, code?: ErrorCodes, stackTrace?: string): void {
6851
if(!this.canSendLog(severity))
6952
return;
53+
54+
const logRequest = this.getLogRequest(severity, msg, code, stackTrace);
55+
const uploader = new FetchUploader(url);
56+
const payload: IFetchPayload = {
57+
method: 'POST',
58+
headers: this.getHeaders(),
59+
body: JSON.stringify(logRequest),
60+
};
61+
uploader.upload(payload);
62+
}
7063

71-
const logRequest: LogRequestBody = {
64+
private sendLog(severity: WSDKErrorSeverity, msg: string, code?: ErrorCodes, stackTrace?: string): void {
65+
const url = this.getLoggingUrl();
66+
this.sendToServer(url, severity, msg, code, stackTrace);
67+
}
68+
private sendError(severity: WSDKErrorSeverity, msg: string, code?: ErrorCodes, stackTrace?: string): void {
69+
const url = this.getErrorUrl();
70+
this.sendToServer(url, severity, msg, code, stackTrace);
71+
}
72+
73+
private getLogRequest(severity: WSDKErrorSeverity, msg: string, code?: ErrorCodes, stackTrace?: string): LogRequestBody {
74+
return {
7275
additionalInformation: {
7376
message: msg,
7477
version: this.getVersion(),
7578
},
7679
severity: severity,
77-
code: code,
80+
code: code ?? ErrorCodes.UNKNOWN_ERROR,
7881
url: this.getUrl(),
7982
deviceInfo: this.getUserAgent(),
80-
stackTrace: stackTrace ?? 'this is my stack trace',
83+
stackTrace: stackTrace ?? '',
8184
reporter: this.reporter,
82-
integration: this.integration,
85+
integration: this.integration
8386
};
84-
85-
this.sendLogToServer(logRequest);
8687
}
8788

89+
private getVersion(): string {
90+
return this.integrationName ?? this.sdkVersion;
91+
}
92+
8893
private isReportingEnabled(): boolean {
8994
// QUESTION: Should isDebugModeEnabled take precedence over
9095
// isFeatureFlagEnabled and rokt domain present?
@@ -100,10 +105,7 @@ export class ReportingLogger implements IReportingLogger {
100105
}
101106

102107
private isFeatureFlagEnabled(): boolean {
103-
return window.
104-
mParticle?.
105-
config?.
106-
isWebSdkLoggingEnabled ?? false;
108+
return this.config.isWebSdkLoggingEnabled;
107109
}
108110

109111
private isDebugModeEnabled(): boolean {
@@ -132,8 +134,8 @@ export class ReportingLogger implements IReportingLogger {
132134
return window.navigator.userAgent;
133135
}
134136

135-
private getLoggingUrl = (): string => `https://${this.loggingUrl}`;
136-
private getErrorUrl = (): string => `https://${this.errorUrl}`;
137+
private getLoggingUrl = (): string => `https://${this.config.loggingUrl}`;
138+
private getErrorUrl = (): string => `https://${this.config.errorUrl}`;
137139

138140
private getHeaders(): IFetchPayload['headers'] {
139141
const headers: Record<string, string> = {
@@ -143,20 +145,13 @@ export class ReportingLogger implements IReportingLogger {
143145
'rokt-launcher-version': this.getVersion(),
144146
'rokt-wsdk-version': 'joint',
145147
};
148+
149+
if (this.store?.getRoktAccountId()) {
150+
headers['rokt-account-id'] = this.store.getRoktAccountId();
151+
}
152+
146153
return headers as IFetchPayload['headers'];
147154
}
148-
149-
private sendLogToServer(logRequest: LogRequestBody) {
150-
const uploadUrl = this.getErrorUrl();
151-
const uploader = new FetchUploader(uploadUrl);
152-
const payload: IFetchPayload = {
153-
method: 'POST',
154-
headers: this.getHeaders(),
155-
body: JSON.stringify(logRequest),
156-
};
157-
158-
uploader.upload(payload);
159-
};
160155
}
161156

162157
export interface IRateLimiter {

src/mp-instance.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { LocalStorageVault } from './vault';
4141
import { removeExpiredIdentityCacheDates } from './identity-utils';
4242
import IntegrationCapture from './integrationCapture';
4343
import { IPreInit, processReadyQueue } from './pre-init-utils';
44-
import { BaseEvent, MParticleWebSDK, SDKHelpersApi } from './sdkRuntimeModels';
44+
import { BaseEvent, MParticleWebSDK, SDKHelpersApi, SDKInitConfig } from './sdkRuntimeModels';
4545
import { Dictionary, SDKEventAttrs } from '@mparticle/web-sdk';
4646
import { IIdentity } from './identity.interfaces';
4747
import { IEvents } from './events.interfaces';
@@ -52,6 +52,7 @@ import ForegroundTimer from './foregroundTimeTracker';
5252
import RoktManager, { IRoktOptions } from './roktManager';
5353
import filteredMparticleUser from './filteredMparticleUser';
5454
import { IReportingLogger, ReportingLogger } from './logging/reportingLogger';
55+
import { SDKConfigManager } from './sdkConfigManager';
5556

5657
export interface IErrorLogMessage {
5758
message?: string;
@@ -1425,7 +1426,7 @@ function completeSDKInitialization(apiKey, config, mpInstance) {
14251426
// Configure Rokt Manager with user and filtered user
14261427
const roktConfig: IKitConfigs = parseConfig(config, 'Rokt', 181);
14271428
if (roktConfig) {
1428-
mpInstance._ReportingLogger.accountId = roktConfig.settings?.accountId ?? '';
1429+
mpInstance._Store.setRoktAccountId(roktConfig.settings?.accountId ?? undefined);
14291430
const { userAttributeFilters } = roktConfig;
14301431
const roktFilteredUser = filteredMparticleUser(
14311432
currentUserMPID,
@@ -1558,22 +1559,22 @@ function createIdentityCache(mpInstance) {
15581559
}
15591560

15601561
function runPreConfigFetchInitialization(mpInstance, apiKey, config) {
1561-
// QUESTION: Should Store come before ReportingLogger?
1562-
// Logger needs the store to generate the url
1563-
// But Store needs the Logger to log errors
1562+
1563+
const sdkConfig = new SDKConfigManager(config, apiKey).getSDKConfig();
15641564
mpInstance._ReportingLogger = new ReportingLogger(
1565+
sdkConfig,
15651566
Constants.sdkVersion,
1566-
undefined, // QUESTION: Do we need a RateLimiter??
1567-
mpInstance.getLauncherInstanceGuid()
1567+
mpInstance.getLauncherInstanceGuid(),
15681568
);
15691569
mpInstance.Logger = new Logger(config, mpInstance._ReportingLogger);
1570-
mpInstance._Store = new Store(config, mpInstance, apiKey);
1570+
mpInstance._Store = new Store(
1571+
{ ...config, ...sdkConfig } as SDKInitConfig,
1572+
mpInstance,
1573+
apiKey
1574+
);
15711575
window.mParticle.Store = mpInstance._Store;
1576+
mpInstance._ReportingLogger.setStore(mpInstance._Store);
15721577
mpInstance.Logger.verbose(StartingInitialization);
1573-
1574-
// TODO: Extract urls from config into a url builder
1575-
mpInstance._ReportingLogger.loggingUrl = mpInstance._Store.SDKConfig.loggingUrl;
1576-
mpInstance._ReportingLogger.errorUrl = mpInstance._Store.SDKConfig.errorUrl;
15771578

15781579
// Check to see if localStorage is available before main configuration runs
15791580
// since we will need this for the current implementation of user persistence

0 commit comments

Comments
 (0)