Graphite: Fallback to hardcoded list of functions when no functions are returned (#46681)

This commit is contained in:
Piotr Jamróz
2022-03-17 14:26:08 +01:00
committed by GitHub
parent a9002e80b4
commit ed155e47c4
2 changed files with 17 additions and 0 deletions

View File

@@ -292,6 +292,14 @@ describe('graphiteDatasource', () => {
}, },
}); });
}); });
it('should use hardcoded list of functions when no functions are returned', async () => {
fetchMock.mockImplementation(() => {
return of(createFetchResponse('{}'));
});
const funcDefs = await ctx.ds.getFuncDefs();
expect(Object.keys(funcDefs)).not.toHaveLength(0);
});
}); });
describe('building graphite params', () => { describe('building graphite params', () => {

View File

@@ -769,6 +769,15 @@ export class GraphiteDatasource
} else { } else {
this.funcDefs = gfunc.parseFuncDefs(results.data); this.funcDefs = gfunc.parseFuncDefs(results.data);
} }
// When /functions endpoint returns application/json response but containing invalid JSON the fix above
// wont' be triggered due to the changes in https://github.com/grafana/grafana/pull/45598 (parsing happens
// in fetch and Graphite receives an empty object and no error). In such cases, when the provided JSON
// seems empty we fallback to the hardcoded list of functions.
// See also: https://github.com/grafana/grafana/issues/45948
if (Object.keys(this.funcDefs).length === 0) {
this.funcDefs = gfunc.getFuncDefs(this.graphiteVersion);
}
return this.funcDefs; return this.funcDefs;
}), }),
catchError((error: any) => { catchError((error: any) => {