Skip to content

Commit 8458c2f

Browse files
Add Event Loss Logging Using Custom Logger Interface (#467)
Diff for adding Custom Logger Interface statsig-io/private-js-client-sdk#466 This Diff added more event loss logging using the custom interface
1 parent 56aab82 commit 8458c2f

File tree

5 files changed

+28
-36
lines changed

5 files changed

+28
-36
lines changed

src/StatsigClient.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,13 @@ export default class StatsigClient implements IHasStatsigInternal, IStatsig {
771771
}
772772
if (typeof eventName !== 'string' || eventName.length === 0) {
773773
OutputLogger.error(
774-
'Event not logged. No valid eventName passed.',
774+
'statsigSDK> Event not logged. No valid eventName passed.',
775775
);
776776
return;
777777
}
778778
if (this.shouldTrimParam(eventName, MAX_VALUE_SIZE)) {
779779
OutputLogger.info(
780-
'eventName is too long, trimming to ' +
780+
'statsigSDK> eventName is too long, trimming to ' +
781781
MAX_VALUE_SIZE +
782782
' characters.',
783783
);
@@ -788,12 +788,12 @@ export default class StatsigClient implements IHasStatsigInternal, IStatsig {
788788
this.shouldTrimParam(value, MAX_VALUE_SIZE)
789789
) {
790790
OutputLogger.info(
791-
'value is too long, trimming to ' + MAX_VALUE_SIZE + '.',
791+
'statsigSDK> value is too long, trimming to ' + MAX_VALUE_SIZE + '.',
792792
);
793793
value = value.substring(0, MAX_VALUE_SIZE);
794794
}
795795
if (this.shouldTrimParam(metadata, MAX_OBJ_SIZE)) {
796-
OutputLogger.info('metadata is too big. Dropping the metadata.');
796+
OutputLogger.info('statsigSDK> metadata is too big. Dropping the metadata.');
797797
metadata = { error: 'not logged due to size too large' };
798798
}
799799
const event = new LogEvent(eventName);
@@ -1322,20 +1322,20 @@ export default class StatsigClient implements IHasStatsigInternal, IStatsig {
13221322
}
13231323
if (this.shouldTrimParam(user.userID ?? null, MAX_VALUE_SIZE)) {
13241324
OutputLogger.info(
1325-
'User ID is too large, trimming to ' + MAX_VALUE_SIZE + 'characters',
1325+
'statsigSDK> User ID is too large, trimming to ' + MAX_VALUE_SIZE + 'characters',
13261326
);
13271327
user.userID = user.userID?.toString().substring(0, MAX_VALUE_SIZE);
13281328
}
13291329
if (this.shouldTrimParam(user, MAX_OBJ_SIZE)) {
13301330
user.custom = {};
13311331
if (this.shouldTrimParam(user, MAX_OBJ_SIZE)) {
13321332
OutputLogger.info(
1333-
'User object is too large, only keeping the user ID.',
1333+
'statsigSDK> User object is too large, only keeping the user ID.',
13341334
);
13351335
user = { userID: user.userID };
13361336
} else {
13371337
OutputLogger.info(
1338-
'User object is too large, dropping the custom property.',
1338+
'statsigSDK> User object is too large, dropping the custom property.',
13391339
);
13401340
}
13411341
}
@@ -1366,7 +1366,7 @@ export default class StatsigClient implements IHasStatsigInternal, IStatsig {
13661366
const prefetchUsers = args.prefetchUsers ?? [];
13671367
const timeout = args.timeout ?? this.options.getInitTimeoutMs();
13681368
if (prefetchUsers.length > 5) {
1369-
OutputLogger.info('Cannot prefetch more than 5 users.');
1369+
OutputLogger.info('statsigSDK> Cannot prefetch more than 5 users.');
13701370
}
13711371

13721372
const keyedPrefetchUsers = this.normalizePrefetchUsers(prefetchUsers)

src/StatsigLogger.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export default class StatsigLogger {
292292
): void {
293293
this.logGenericEvent(DEFAULT_VALUE_WARNING, user, message, metadata);
294294
this.loggedErrors.add(message);
295-
OutputLogger.error(message);
295+
OutputLogger.error(`statsigSDK> ${message}`);
296296
}
297297

298298
public logAppError(
@@ -472,7 +472,8 @@ export default class StatsigLogger {
472472
throw response;
473473
}
474474
})
475-
.catch(() => {
475+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
476+
.catch((error) => {
476477
this.addFailedRequest({ events: oldQueue, statsigMetadata: this.sdkInternal.getStatsigMetadata(), time: Date.now() });
477478
})
478479
.finally(async () => {
@@ -554,6 +555,7 @@ export default class StatsigLogger {
554555
}
555556
}
556557
} catch (e) {
558+
OutputLogger.error("statsigSDK> sendSavedRequests ", e as Error);
557559
this.sdkInternal.getErrorBoundary().logError('sendSavedRequests', e);
558560
} finally {
559561
this.clearLocalStorageRequests();

src/StatsigNetwork.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { IHasStatsigInternal } from './StatsigClient';
33
import StatsigRuntime from './StatsigRuntime';
44
import { StatsigUser } from './StatsigUser';
55
import Diagnostics from './utils/Diagnostics';
6+
import OutputLogger from './utils/OutputLogger';
67

78
export enum StatsigEndpoint {
89
Initialize = 'initialize',
@@ -159,16 +160,20 @@ export default class StatsigNetwork {
159160
.then((localRes) => {
160161
res = localRes;
161162
if (!res.ok) {
163+
const errorMessage = `Request to ${endpointName} failed with status ${res.status}`;
164+
OutputLogger.error(`statsigSDK> ${errorMessage}`);
162165
return Promise.reject(
163166
new Error(
164-
`Request to ${endpointName} failed with status ${res.status}`,
167+
errorMessage,
165168
),
166169
);
167170
}
168171

169172
if (typeof res.data !== 'object') {
173+
const errorMessage = `Request to ${endpointName} received invalid response type. Expected 'object' but got '${typeof res.data}'`;
174+
OutputLogger.error(`statsigSDK> ${errorMessage}`);
170175
const error = new Error(
171-
`Request to ${endpointName} received invalid response type. Expected 'object' but got '${typeof res.data}'`,
176+
errorMessage,
172177
);
173178
this.sdkInternal
174179
.getErrorBoundary()
@@ -365,6 +370,13 @@ export default class StatsigNetwork {
365370
})
366371
.catch((e) => {
367372
diagnostics?.end(this.getDiagnosticsData(res, attempt, e));
373+
const errorMessage = `statsigSDK> Error occurred while posting to endpoint: ${e.message}\n` +
374+
`Error Details: ${JSON.stringify(e)}\n` +
375+
`Endpoint: ${endpointName}\n` +
376+
`Attempt: ${attempt}\n` +
377+
`Retry Limit: ${retryLimit}\n` +
378+
`Backoff: ${backoff}`;
379+
OutputLogger.error(errorMessage);
368380
if (attempt < retryLimit && isRetryCode) {
369381
return new Promise<NetworkResponse>((resolve, reject) => {
370382
setTimeout(() => {

src/__tests__/OutputLogger.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ describe('Output logger Interface', () => {
3535
client.logEvent('');
3636
if (level === LogLevel.ERROR) {
3737
expect(errors).toContainEqual(
38-
'Event not logged. No valid eventName passed.',
38+
'statsigSDK> Event not logged. No valid eventName passed.',
3939
);
4040
}
4141

4242
client.logEvent('a'.repeat(100));
4343
if (level == LogLevel.INFO) {
4444
expect(infos).toContainEqual(
45-
'eventName is too long, trimming to ' + MAX_VALUE_SIZE + ' characters.',
45+
'statsigSDK> eventName is too long, trimming to ' + MAX_VALUE_SIZE + ' characters.',
4646
);
4747
}
4848
}

src/utils/ConsoleLogger.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)