Skip to content

Commit

Permalink
refactor: extract DIAGNOSTIC_MESSAGES
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Oct 12, 2023
1 parent 860bde2 commit 266c52c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 72 deletions.
12 changes: 6 additions & 6 deletions packages/analytics-browser/test/browser-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ describe('browser-client', () => {
await client.track('event_type', { userId: 'user_0' }).promise;

expect(diagnosticTrack).toHaveBeenCalledTimes(1);
expect(diagnosticTrack).toHaveBeenCalledWith(1, 0, core.UNEXPECTED_DIAGNOSTIC_MESSAGE);
expect(diagnosticTrack).toHaveBeenCalledWith(1, 0, core.DIAGNOSTIC_MESSAGES.UNEXPECTED_ERROR);
});

test.each([
['api_key', undefined, core.INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE],
[undefined, { time: [0] }, core.EVENT_ERROR_DIAGNOSTIC_MESSAGE],
['api_key', undefined, core.DIAGNOSTIC_MESSAGES.INVALID_OR_MISSING_FIELDS],
[undefined, { time: [0] }, core.DIAGNOSTIC_MESSAGES.EVENT_ERROR],
])(
'should diagnostic track when 400 invalid response',
async (missingField, eventsWithInvalidFields, message) => {
Expand Down Expand Up @@ -361,7 +361,7 @@ describe('browser-client', () => {
await client.flush().promise;

expect(diagnosticTrack).toHaveBeenCalledTimes(1);
expect(diagnosticTrack).toHaveBeenCalledWith(1, 429, core.EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE);
expect(diagnosticTrack).toHaveBeenCalledWith(1, 429, core.DIAGNOSTIC_MESSAGES.EXCEEDED_DAILY_QUOTA);
});

test('should diagnostic track when 413', async () => {
Expand All @@ -385,7 +385,7 @@ describe('browser-client', () => {
await client.track('event_type', { userId: 'user_0' }).promise;

expect(diagnosticTrack).toHaveBeenCalledTimes(1);
expect(diagnosticTrack).toHaveBeenCalledWith(1, 413, core.PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE);
expect(diagnosticTrack).toHaveBeenCalledWith(1, 413, core.DIAGNOSTIC_MESSAGES.PAYLOAD_TOO_LARGE);
});

test('should diagnostic track when 500 hit max retries', async () => {
Expand Down Expand Up @@ -415,7 +415,7 @@ describe('browser-client', () => {
await client.track('event_type', { userId: 'user_0' }).promise;

expect(diagnosticTrack).toHaveBeenCalledTimes(1);
expect(diagnosticTrack).toHaveBeenCalledWith(1, 500, core.EXCEEDED_MAX_RETRY_DIAGNOSTIC_MESSAGE);
expect(diagnosticTrack).toHaveBeenCalledWith(1, 500, core.DIAGNOSTIC_MESSAGES.EXCEEDED_MAX_RETRY);
});
});
});
Expand Down
18 changes: 10 additions & 8 deletions packages/analytics-core/src/diagnostics/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export const EXCEEDED_MAX_RETRY_DIAGNOSTIC_MESSAGE = 'exceeded max retries';
export const MISSING_API_KEY_DIAGNOSTIC_MESSAGE = 'missing API key';
export const UNEXPECTED_DIAGNOSTIC_MESSAGE = 'unexpected error';
export const INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE = 'invalid or missing fields';
export const EVENT_ERROR_DIAGNOSTIC_MESSAGE = 'event error';
export const PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE = 'payload too large';
export const EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE = 'exceeded daily quota users or devices';

export const DIAGNOSTIC_METADATA_TYPE = 'SDK_DIAGNOSTIC';
export const DIAGNOSTIC_ENDPOINT = 'https://api-omni.stag2.amplitude.com/omni/metrics';

export const DIAGNOSTIC_MESSAGES = {
EXCEEDED_MAX_RETRY: 'exceeded max retries',
MISSING_API_KEY: 'missing API key',
UNEXPECTED_ERROR: 'unexpected error',
INVALID_OR_MISSING_FIELDS: 'invalid or missing fields',
EVENT_ERROR: 'event error',
PAYLOAD_TOO_LARGE: 'payload too large',
EXCEEDED_DAILY_QUOTA: 'exceeded daily quota users or devices',
};
12 changes: 1 addition & 11 deletions packages/analytics-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@ export { Identify } from './identify';
export { Revenue } from './revenue';
export { Destination } from './plugins/destination';
export { BaseDiagnostic } from './diagnostics/diagnostic';
export {
EXCEEDED_MAX_RETRY_DIAGNOSTIC_MESSAGE,
MISSING_API_KEY_DIAGNOSTIC_MESSAGE,
UNEXPECTED_DIAGNOSTIC_MESSAGE,
INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE,
EVENT_ERROR_DIAGNOSTIC_MESSAGE,
PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE,
EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE,
DIAGNOSTIC_METADATA_TYPE,
DIAGNOSTIC_ENDPOINT,
} from './diagnostics/constants';
export { DIAGNOSTIC_MESSAGES, DIAGNOSTIC_METADATA_TYPE, DIAGNOSTIC_ENDPOINT } from './diagnostics/constants';
export { Config } from './config';
export { Logger } from './logger';
export { AMPLITUDE_PREFIX, STORAGE_PREFIX } from './constants';
Expand Down
32 changes: 12 additions & 20 deletions packages/analytics-core/src/plugins/destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ import { STORAGE_PREFIX } from '../constants';
import { chunk } from '../utils/chunk';
import { buildResult } from '../utils/result-builder';
import { createServerConfig } from '../config';
import {
EVENT_ERROR_DIAGNOSTIC_MESSAGE,
EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE,
EXCEEDED_MAX_RETRY_DIAGNOSTIC_MESSAGE,
INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE,
MISSING_API_KEY_DIAGNOSTIC_MESSAGE,
PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE,
UNEXPECTED_DIAGNOSTIC_MESSAGE,
} from '../diagnostics/constants';
import { DIAGNOSTIC_MESSAGES } from '../diagnostics/constants';

function getErrorMessage(error: unknown) {
if (error instanceof Error) return error.message;
Expand Down Expand Up @@ -95,7 +87,7 @@ export class Destination implements DestinationPlugin {
return true;
}
void this.fulfillRequest([context], 500, MAX_RETRIES_EXCEEDED_MESSAGE);
this.config.diagnosticProvider.track(list.length, 500, EXCEEDED_MAX_RETRY_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(list.length, 500, DIAGNOSTIC_MESSAGES.EXCEEDED_MAX_RETRY);
return false;
});

Expand Down Expand Up @@ -143,7 +135,7 @@ export class Destination implements DestinationPlugin {

async send(list: Context[], useRetry = true) {
if (!this.config.apiKey) {
this.config.diagnosticProvider.track(list.length, 400, MISSING_API_KEY_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(list.length, 400, DIAGNOSTIC_MESSAGES.MISSING_API_KEY);
return this.fulfillRequest(list, 400, MISSING_API_KEY_MESSAGE);
}

Expand All @@ -163,7 +155,7 @@ export class Destination implements DestinationPlugin {
const { serverUrl } = createServerConfig(this.config.serverUrl, this.config.serverZone, this.config.useBatch);
const res = await this.config.transportProvider.send(serverUrl, payload);
if (res === null) {
this.config.diagnosticProvider.track(list.length, 0, UNEXPECTED_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(list.length, 0, DIAGNOSTIC_MESSAGES.UNEXPECTED_ERROR);
this.fulfillRequest(list, 0, UNEXPECTED_ERROR_MESSAGE);
return;
}
Expand All @@ -172,7 +164,7 @@ export class Destination implements DestinationPlugin {
const errorMessage = getErrorMessage(e);
this.config.loggerProvider.error(errorMessage);
this.fulfillRequest(list, 0, errorMessage);
this.config.diagnosticProvider.track(list.length, 0, UNEXPECTED_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(list.length, 0, DIAGNOSTIC_MESSAGES.UNEXPECTED_ERROR);
}
}

Expand Down Expand Up @@ -213,7 +205,7 @@ export class Destination implements DestinationPlugin {
handleInvalidResponse(res: InvalidResponse, list: Context[], useRetry: boolean) {
if (res.body.missingField || res.body.error.startsWith(INVALID_API_KEY)) {
this.fulfillRequest(list, res.statusCode, `${res.status}: ${getResponseBodyString(res)}`);
this.config.diagnosticProvider.track(list.length, 400, INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(list.length, 400, DIAGNOSTIC_MESSAGES.INVALID_OR_MISSING_FIELDS);
return;
}

Expand All @@ -227,7 +219,7 @@ export class Destination implements DestinationPlugin {

if (useRetry) {
if (dropIndexSet.size) {
this.config.diagnosticProvider.track(dropIndexSet.size, 400, EVENT_ERROR_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(dropIndexSet.size, 400, DIAGNOSTIC_MESSAGES.EVENT_ERROR);
}
const retry = list.filter((context, index) => {
if (dropIndexSet.has(index)) {
Expand All @@ -243,13 +235,13 @@ export class Destination implements DestinationPlugin {
}
this.addToQueue(...retry);
} else {
this.config.diagnosticProvider.track(list.length, 400, EVENT_ERROR_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(list.length, 400, DIAGNOSTIC_MESSAGES.EVENT_ERROR);
}
}

handlePayloadTooLargeResponse(res: PayloadTooLargeResponse, list: Context[], useRetry: boolean) {
if (list.length === 1 || !useRetry) {
this.config.diagnosticProvider.track(list.length, 413, PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(list.length, 413, DIAGNOSTIC_MESSAGES.PAYLOAD_TOO_LARGE);
this.fulfillRequest(list, res.statusCode, res.body.error);
return;
}
Expand All @@ -263,7 +255,7 @@ export class Destination implements DestinationPlugin {

handleRateLimitResponse(res: RateLimitResponse, list: Context[], useRetry: boolean) {
if (!useRetry) {
this.config.diagnosticProvider.track(list.length, 429, EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(list.length, 429, DIAGNOSTIC_MESSAGES.EXCEEDED_DAILY_QUOTA);
this.fulfillRequest(list, res.statusCode, res.status);
return;
}
Expand Down Expand Up @@ -291,7 +283,7 @@ export class Destination implements DestinationPlugin {

const dropEvents = list.filter((element) => !retry.includes(element));
if (dropEvents.length > 0) {
this.config.diagnosticProvider.track(dropEvents.length, 429, EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(dropEvents.length, 429, DIAGNOSTIC_MESSAGES.EXCEEDED_DAILY_QUOTA);
}

if (retry.length > 0) {
Expand All @@ -312,7 +304,7 @@ export class Destination implements DestinationPlugin {
);
} else {
this.fulfillRequest(list, res.statusCode, res.status);
this.config.diagnosticProvider.track(list.length, 0, UNEXPECTED_DIAGNOSTIC_MESSAGE);
this.config.diagnosticProvider.track(list.length, 0, DIAGNOSTIC_MESSAGES.UNEXPECTED_ERROR);
}
}

Expand Down
22 changes: 8 additions & 14 deletions packages/analytics-core/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ import {
createIdentifyEvent,
BaseDiagnostic,
buildResult,
EXCEEDED_MAX_RETRY_DIAGNOSTIC_MESSAGE,
MISSING_API_KEY_DIAGNOSTIC_MESSAGE,
UNEXPECTED_DIAGNOSTIC_MESSAGE,
INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE,
EVENT_ERROR_DIAGNOSTIC_MESSAGE,
PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE,
EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE,
DIAGNOSTIC_MESSAGES,
DIAGNOSTIC_METADATA_TYPE,
DIAGNOSTIC_ENDPOINT,
} from '../src/index';
Expand Down Expand Up @@ -55,13 +49,13 @@ describe('index', () => {
expect(typeof buildResult).toBe('function');
expect(AMPLITUDE_PREFIX).toBe('AMP');
expect(STORAGE_PREFIX).toBe('AMP_unsent');
expect(EXCEEDED_MAX_RETRY_DIAGNOSTIC_MESSAGE).toBe('exceeded max retries');
expect(MISSING_API_KEY_DIAGNOSTIC_MESSAGE).toBe('missing API key');
expect(UNEXPECTED_DIAGNOSTIC_MESSAGE).toBe('unexpected error');
expect(INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE).toBe('invalid or missing fields');
expect(EVENT_ERROR_DIAGNOSTIC_MESSAGE).toBe('event error');
expect(PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE).toBe('payload too large');
expect(EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE).toBe('exceeded daily quota users or devices');
expect(DIAGNOSTIC_MESSAGES.EXCEEDED_MAX_RETRY).toBe('exceeded max retries');
expect(DIAGNOSTIC_MESSAGES.MISSING_API_KEY).toBe('missing API key');
expect(DIAGNOSTIC_MESSAGES.UNEXPECTED_ERROR).toBe('unexpected error');
expect(DIAGNOSTIC_MESSAGES.INVALID_OR_MISSING_FIELDS).toBe('invalid or missing fields');
expect(DIAGNOSTIC_MESSAGES.EVENT_ERROR).toBe('event error');
expect(DIAGNOSTIC_MESSAGES.PAYLOAD_TOO_LARGE).toBe('payload too large');
expect(DIAGNOSTIC_MESSAGES.EXCEEDED_DAILY_QUOTA).toBe('exceeded daily quota users or devices');
expect(DIAGNOSTIC_METADATA_TYPE).toBe('SDK_DIAGNOSTIC');
expect(DIAGNOSTIC_ENDPOINT).toBe('https://api-omni.stag2.amplitude.com/omni/metrics');
});
Expand Down
20 changes: 7 additions & 13 deletions packages/analytics-core/test/plugins/destination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ import {
SUCCESS_MESSAGE,
UNEXPECTED_ERROR_MESSAGE,
} from '../../src/messages';
import {
EVENT_ERROR_DIAGNOSTIC_MESSAGE,
EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE,
INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE,
PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE,
UNEXPECTED_DIAGNOSTIC_MESSAGE,
} from '../../src/diagnostics/constants';
import { DIAGNOSTIC_MESSAGES } from '../../src/diagnostics/constants';

const jsons = (obj: any) => JSON.stringify(obj, null, 2);
class TestDiagnostic implements Diagnostic {
Expand Down Expand Up @@ -451,13 +445,13 @@ describe('destination', () => {
await destination.send([context]);
expect(callback).toHaveBeenCalledTimes(1);
expect(diagnosticProvider.track).toHaveBeenCalledTimes(1);
expect(diagnosticProvider.track).toHaveBeenLastCalledWith(1, 0, UNEXPECTED_DIAGNOSTIC_MESSAGE);
expect(diagnosticProvider.track).toHaveBeenLastCalledWith(1, 0, DIAGNOSTIC_MESSAGES.UNEXPECTED_ERROR);
});

test.each([
['api_key', undefined, true, 2, INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE],
[undefined, { time: [0] }, true, 1, EVENT_ERROR_DIAGNOSTIC_MESSAGE],
[undefined, { time: [0] }, false, 2, EVENT_ERROR_DIAGNOSTIC_MESSAGE],
['api_key', undefined, true, 2, DIAGNOSTIC_MESSAGES.INVALID_OR_MISSING_FIELDS],
[undefined, { time: [0] }, true, 1, DIAGNOSTIC_MESSAGES.EVENT_ERROR],
[undefined, { time: [0] }, false, 2, DIAGNOSTIC_MESSAGES.EVENT_ERROR],
])(
'should track diagnostic when 400',
async (missingField, eventsWithInvalidFields, useRetry, dropCount, message) => {
Expand Down Expand Up @@ -558,7 +552,7 @@ describe('destination', () => {
);

expect(diagnosticProvider.track).toHaveBeenCalledTimes(1);
expect(diagnosticProvider.track).toHaveBeenCalledWith(2, 413, PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE);
expect(diagnosticProvider.track).toHaveBeenCalledWith(2, 413, DIAGNOSTIC_MESSAGES.PAYLOAD_TOO_LARGE);
});

test.each([
Expand Down Expand Up @@ -610,7 +604,7 @@ describe('destination', () => {
useRetry,
);
expect(diagnosticProvider.track).toHaveBeenCalledTimes(1);
expect(diagnosticProvider.track).toHaveBeenCalledWith(dropCount, 429, EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE);
expect(diagnosticProvider.track).toHaveBeenCalledWith(dropCount, 429, DIAGNOSTIC_MESSAGES.EXCEEDED_DAILY_QUOTA);
expect(destination.queue.length).toBe(queueCount);
});
});
Expand Down

0 comments on commit 266c52c

Please sign in to comment.