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

Fixes Unleash client stops fetching #615 #616

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,10 @@ export default class Metrics extends EventEmitter {
httpOptions: this.httpOptions,
});
if (!res.ok) {
if (res.status === 404 || res.status === 403 || res.status == 401) {
if (res.status === 403 || res.status == 401) {
this.configurationError(url, res.status);
} else if (
res.status === 404 ||
res.status === 429 ||
res.status === 500 ||
res.status === 502 ||
Expand Down
15 changes: 4 additions & 11 deletions src/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,7 @@ Message: ${err.message}`,
// and returns 0 as the next fetch interval (stop polling)
private configurationError(url: string, statusCode: number): number {
this.failures += 1;
if (statusCode === 404) {
this.emit(
UnleashEvents.Error,
new Error(
// eslint-disable-next-line max-len
`${url} responded NOT_FOUND (404) which means your API url most likely needs correction. Stopping refresh of toggles`,
),
);
} else if (statusCode === 401 || statusCode === 403) {
if (statusCode === 401 || statusCode === 403) {
this.emit(
UnleashEvents.Error,
new Error(
Expand All @@ -294,7 +286,7 @@ Message: ${err.message}`,
// and return the new interval.
private recoverableError(url: string, statusCode: number): number {
let nextFetch = this.backoff();
if (statusCode === 429) {
if (statusCode === 404 || statusCode === 429) {
this.emit(
UnleashEvents.Warn,
// eslint-disable-next-line max-len
Expand All @@ -312,9 +304,10 @@ Message: ${err.message}`,
}

private handleErrorCases(url: string, statusCode: number): number {
if (statusCode === 401 || statusCode === 403 || statusCode === 404) {
if (statusCode === 401 || statusCode === 403) {
return this.configurationError(url, statusCode);
} else if (
statusCode === 404 ||
statusCode === 429 ||
statusCode === 500 ||
statusCode === 502 ||
Expand Down
45 changes: 22 additions & 23 deletions src/test/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,29 +252,28 @@ test('registerInstance should warn when non 200 statusCode', async (t) => {
metrics.start();
});

test('sendMetrics should stop/disable metrics if endpoint returns 404', (t) =>
new Promise((resolve) => {
const url = getUrl();
const metEP = nockMetrics(url, 404);
// @ts-expect-error
const metrics = new Metrics({
metricsInterval: 50,
url,
});

metrics.on('warn', () => {
metrics.stop();
t.true(metEP.isDone());
// @ts-expect-error
t.true(metrics.disabled);
resolve();
});
metrics.start();

metrics.count('x-y-z', true);

metrics.sendMetrics();
}));
test('sendMetrics should backoff on 404', async (t) => {
const url = getUrl();
nockMetrics(url, 404).persist();
const metrics = new Metrics({
appName: '404-tester',
instanceId: '404-instance',
metricsInterval: 10,
strategies: [],
url,
});
metrics.count('x-y-z', true);
await metrics.sendMetrics();
// @ts-expect-error actually a private field, but we access it for tests
t.false(metrics.disabled);
t.is(metrics.getFailures(), 1);
t.is(metrics.getInterval(), 20);
await metrics.sendMetrics();
// @ts-expect-error actually a private field, but we access it for tests
t.false(metrics.disabled);
t.is(metrics.getFailures(), 2);
t.is(metrics.getInterval(), 30);
});

test('sendMetrics should emit warn on non 200 statusCode', (t) =>
new Promise((resolve) => {
Expand Down
Loading