Skip to content

Commit

Permalink
Send log_event_failure to different endpoint than log_event (#243)
Browse files Browse the repository at this point in the history
* Send log_event failures to error boundary

* Add to log failure test case

* rename failure method
  • Loading branch information
tore-statsig authored Feb 27, 2023
1 parent a9ab89d commit e2480f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
37 changes: 21 additions & 16 deletions src/StatsigLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,23 +385,29 @@ export default class StatsigLogger {
.catch((error) => {
if (typeof error.text === 'function') {
error.text().then((errorText: string) => {
const logFailureEvent = new LogEvent(LOG_FAILURE_EVENT);
logFailureEvent.setMetadata({
error: `${error.status}: ${errorText}`,
});
logFailureEvent.setUser(processor.sdkInternal.getCurrentUser());
processor.appendFailureLog(logFailureEvent, oldQueue);
this.sdkInternal
.getErrorBoundary()
.logError(LOG_FAILURE_EVENT, error, async () => {
return {
eventCount: oldQueue.length,
error: errorText,
};
});
});
} else {
const logFailureEvent = new LogEvent(LOG_FAILURE_EVENT);
logFailureEvent.setMetadata({
error: error.message,
});
logFailureEvent.setUser(processor.sdkInternal.getCurrentUser());
processor.appendFailureLog(logFailureEvent, oldQueue);
this.sdkInternal
.getErrorBoundary()
.logError(LOG_FAILURE_EVENT, error, async () => {
return {
eventCount: oldQueue.length,
error: error.message,
};
});
}
processor.newFailedRequest(LOG_FAILURE_EVENT, oldQueue);
})
.finally(async () => {

if (isClosing) {
if (this.queue.length > 0) {
this.addFailedRequest({
Expand Down Expand Up @@ -514,12 +520,11 @@ export default class StatsigLogger {
}
}

private appendFailureLog(event: LogEvent, queue: object[]): void {
if (this.loggedErrors.has(event.getName())) {
private newFailedRequest(name: string, queue: object[]): void {
if (this.loggedErrors.has(name)) {
return;
}
this.loggedErrors.add(event.getName());
queue.push(event);
this.loggedErrors.add(name);

this.failedLogEvents.push({
events: queue,
Expand Down
11 changes: 8 additions & 3 deletions src/__tests__/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Verify behavior of StatsigLogger', () => {
});

test('Test constructor', () => {
expect.assertions(8);
expect.assertions(11);
const client = new StatsigClient(
sdkKey,
{ userID: 'user_key' },
Expand All @@ -65,7 +65,8 @@ describe('Verify behavior of StatsigLogger', () => {
expect(client.getLogger().flushInterval).not.toBeNull();

// @ts-ignore trust me, the method exists
const spyOnFailureLog = jest.spyOn(logger, 'appendFailureLog');
const spyOnFailureLog = jest.spyOn(logger, 'newFailedRequest');
const spyOnErrorBoundary = jest.spyOn(client.getErrorBoundary(), 'logError');
return client.initializeAsync().then(async () => {
logger.log(new LogEvent('event'));
logger.log(new LogEvent('event'));
Expand All @@ -83,14 +84,18 @@ describe('Verify behavior of StatsigLogger', () => {
logger.log(new LogEvent('event'));
}
expect(spyOnFlush).toHaveBeenCalledTimes(1);
expect(spyOnLog).toHaveBeenCalledTimes(101);
await waitAllPromises();
// posting logs network request fails, causing a log event failure
expect(spyOnErrorBoundary).toHaveBeenCalledTimes(1);
expect(spyOnLog).toHaveBeenCalledTimes(101);
expect(spyOnFailureLog).toHaveBeenCalledTimes(1);
// manually flush again, failing again, but we dont log a second time
// manually flush again, failing again
logger.flush();
await waitAllPromises();
// we dont log to the logger, but we do log to error boundary
expect(spyOnLog).toHaveBeenCalledTimes(101);
expect(spyOnErrorBoundary).toHaveBeenCalledTimes(2);

const elevenminslater = Date.now() + 11 * 60 * 1000;
jest.spyOn(global.Date, 'now').mockImplementation(() => elevenminslater);
Expand Down

0 comments on commit e2480f7

Please sign in to comment.