Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: keep alive when dispatch fails #524

Merged
merged 7 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ For example, the config object may look similar to the following:
| cookieAttributes | [CookieAttributes](#cookieattributes) | `{ domain: window.location.hostname, path: '/', sameSite: 'Strict', secure: true, unique: false } ` | Cookie attributes are applied to all cookies stored by the web client, including `cwr_s` and `cwr_u`. |
| sessionAttributes | [MetadataAttributes](#metadataattributes) | `{}` | Session attributes will be added the metadata of all events in the session.|
| disableAutoPageView | Boolean | `false` | When this field is `false`, the web client will automatically record page views.<br/><br/>By default, the web client records page views when (1) the page first loads and (2) the browser's [history API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) is called. The page ID is `window.location.pathname`.<br/><br/>In some cases, the web client's instrumentation will not record the desired page ID. In this case, the web client's page view automation must be disabled using the `disableAutoPageView` configuration, and the application must be instrumented to record page views using the `recordPageView` command. |
| disableOnFail | Boolean | `true` | When this field is `true`, the web client will disable itself when the PutRumEvents request fails and all retries have been exhausted. |
| enableRumClient | Boolean | `true` | When this field is `true`, the web client will record and dispatch RUM events. |
| enableXRay | Boolean | `false` | When this field is `true` **and** the `http` telemetry is used, the web client will record X-Ray traces for HTTP requests.<br/><br/>See the [HTTP telemetry configuration](#http) for more information, including how to connect client-side and server-side traces. |
| endpoint | String | `'https://dataplane.rum.[region].amazonaws.com'`<br/><br/>`'https://[restapi_id].execute-api.[region].amazonaws.com/[stage_name]/'` | The URL of the CloudWatch RUM API where data will be sent.<br/><br/>You may include a path prefix like `/stage_name/` in the endpoint URL if there is a proxy between your web application and CloudWatch RUM. |
Expand Down Expand Up @@ -210,4 +211,4 @@ telemetries: [
}
],
]
```
```
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
Loading