Graphite: Display an error message when finding metrics fails (#32639)

* Display an error when finding metrics fails

* Update public/app/plugins/datasource/graphite/query_ctrl.ts

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

* Make the level of the log message more accurate

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
This commit is contained in:
Piotr Jamróz 2021-04-09 16:36:08 +02:00 committed by GitHub
parent 7749724194
commit fa45fc1833
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 4 deletions

View File

@ -24,8 +24,9 @@ export class GraphiteQueryCtrl extends QueryCtrl {
supportsTags: boolean;
paused: boolean;
// to avoid error flooding, it's shown only once per session
// to avoid error flooding, these errors are shown only once per session
private _tagsAutoCompleteErrorShown = false;
private _metricAutoCompleteErrorShown = false;
/** @ngInject */
constructor(
@ -110,7 +111,7 @@ export class GraphiteQueryCtrl extends QueryCtrl {
}
})
.catch((err: any) => {
dispatch(notifyApp(createErrorNotification('Error', err)));
this.handleMetricsAutoCompleteError(err);
});
}
@ -183,6 +184,7 @@ export class GraphiteQueryCtrl extends QueryCtrl {
}
})
.catch((err: any): any[] => {
this.handleMetricsAutoCompleteError(err);
return [];
});
}
@ -431,12 +433,20 @@ export class GraphiteQueryCtrl extends QueryCtrl {
}
private handleTagsAutoCompleteError(error: Error): void {
console.log(error);
console.error(error);
if (!this._tagsAutoCompleteErrorShown) {
this._tagsAutoCompleteErrorShown = true;
dispatch(notifyApp(createErrorNotification(`Fetching tags failed: ${error.message}.`)));
}
}
private handleMetricsAutoCompleteError(error: Error): void {
console.error(error);
if (!this._metricAutoCompleteErrorShown) {
this._metricAutoCompleteErrorShown = true;
dispatch(notifyApp(createErrorNotification(`Fetching metrics failed: ${error.message}.`)));
}
}
}
function mapToDropdownOptions(results: any[]) {

View File

@ -193,10 +193,61 @@ describe('GraphiteQueryCtrl', () => {
});
});
describe('when autocomplete for metric names is not available', () => {
silenceConsoleOutput();
beforeEach(() => {
ctx.ctrl.datasource.getTagsAutoComplete = jest.fn().mockReturnValue(Promise.resolve([]));
ctx.ctrl.datasource.metricFindQuery = jest.fn().mockReturnValue(
new Promise(() => {
throw new Error();
})
);
});
it('getAltSegments should handle autocomplete errors', async () => {
await expect(async () => {
await ctx.ctrl.getAltSegments(0, 'any');
expect(mockDispatch).toBeCalledWith(
expect.objectContaining({
type: 'appNotifications/notifyApp',
})
);
}).not.toThrow();
});
it('getAltSegments should display the error message only once', async () => {
await ctx.ctrl.getAltSegments(0, 'any');
expect(mockDispatch.mock.calls.length).toBe(1);
await ctx.ctrl.getAltSegments(0, 'any');
expect(mockDispatch.mock.calls.length).toBe(1);
});
it('checkOtherSegments should handle autocomplete errors', async () => {
await expect(async () => {
await ctx.ctrl.checkOtherSegments(1, false);
expect(mockDispatch).toBeCalledWith(
expect.objectContaining({
type: 'appNotifications/notifyApp',
})
);
}).not.toThrow();
});
it('checkOtherSegments should display the error message only once', async () => {
await ctx.ctrl.checkOtherSegments(1, false);
expect(mockDispatch.mock.calls.length).toBe(1);
await ctx.ctrl.checkOtherSegments(1, false);
expect(mockDispatch.mock.calls.length).toBe(1);
});
});
describe('when autocomplete for tags is not available', () => {
silenceConsoleOutput();
beforeEach(() => {
ctx.ctrl.datasource.getTagsAutoComplete.mockReturnValue(
ctx.datasource.metricFindQuery = jest.fn().mockReturnValue(Promise.resolve([]));
ctx.datasource.getTagsAutoComplete = jest.fn().mockReturnValue(
new Promise(() => {
throw new Error();
})