Skip to content

Commit

Permalink
feat: disableOnFail
Browse files Browse the repository at this point in the history
  • Loading branch information
williazz committed Mar 15, 2024
1 parent 634467b commit 67ed7b3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/dispatch/Dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ export class Dispatch {
// The handler has run out of retries. We adhere to our convention to
// fail safe by disabling dispatch. This ensures that we will not
// continue to attempt requests when the problem is not recoverable.
this.disable();
if (this.config.disableOnFail) {
this.disable();
}
throw e;
};

Expand Down
46 changes: 44 additions & 2 deletions src/dispatch/__tests__/Dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ describe('Dispatch tests', () => {
);
});

test('when a fetch request is rejected then dispatch is disabled', async () => {
test('when a fetch request is rejected and disableOnFaile=true then dispatch is disabled', async () => {
// Init
const ERROR = 'Something went wrong.';
const sendFetch = jest.fn(() => Promise.reject(ERROR));
Expand All @@ -438,7 +438,11 @@ describe('Dispatch tests', () => {
eventCache,
{
...DEFAULT_CONFIG,
...{ dispatchInterval: Utils.AUTO_DISPATCH_OFF, retries: 0 }
...{
dispatchInterval: Utils.AUTO_DISPATCH_OFF,
retries: 0,
disableOnFail: true
}
}
);
dispatch.setAwsCredentials(Utils.createAwsCredentials());
Expand All @@ -449,6 +453,44 @@ describe('Dispatch tests', () => {

// Assert
await expect(dispatch.dispatchFetch()).resolves.toEqual(undefined);
expect((dispatch as unknown as any).enabled).toBe(false);
});

test('when a fetch request is rejected and disableOnFail=false then dispatch is not disabled', async () => {
// Init
const ERROR = 'Something went wrong.';
const sendFetch = jest.fn(() => Promise.reject(ERROR));
(DataPlaneClient as any).mockImplementation(() => {
return {
sendFetch
};
});

const eventCache: EventCache =
Utils.createDefaultEventCacheWithEvents();

const dispatch = new Dispatch(
Utils.AWS_RUM_REGION,
Utils.AWS_RUM_ENDPOINT,
eventCache,
{
...DEFAULT_CONFIG,
...{
dispatchInterval: Utils.AUTO_DISPATCH_OFF,
retries: 0,
disableOnFail: false
}
}
);
dispatch.setAwsCredentials(Utils.createAwsCredentials());

// Run
await expect(dispatch.dispatchFetch()).rejects.toEqual(ERROR);
eventCache.recordEvent('com.amazon.rum.event1', {});

// Assert
await expect(dispatch.dispatchFetch()).rejects.toEqual(ERROR);
expect((dispatch as unknown as any).enabled).toBe(true);
});

test('when signing is disabled then credentials are not needed for dispatch', async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/orchestration/Orchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const defaultConfig = (cookieAttributes: CookieAttributes): Config => {
client: INSTALL_MODULE,
cookieAttributes,
disableAutoPageView: false,
disableOnFail: true,
dispatchInterval: 5 * 1000,
enableRumClient: true,
enableXRay: false,
Expand Down Expand Up @@ -115,6 +116,7 @@ export interface Config {
cookieAttributes: CookieAttributes;
sessionAttributes: { [k: string]: string | number | boolean };
disableAutoPageView: boolean;
disableOnFail: boolean;
dispatchInterval: number;
enableRumClient: boolean;
enableXRay: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/orchestration/__tests__/Orchestration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ describe('Orchestration tests', () => {
sessionAttributes: {},
telemetries: [],
disableAutoPageView: false,
disableOnFail: true,
dispatchInterval: 5000,
enableXRay: false,
endpoint: 'https://dataplane.rum.us-west-2.amazonaws.com',
Expand Down

0 comments on commit 67ed7b3

Please sign in to comment.