From ed155e47c4f9a2c57acfee0a9a5ca681b5dd2894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jamr=C3=B3z?= Date: Thu, 17 Mar 2022 14:26:08 +0100 Subject: [PATCH] Graphite: Fallback to hardcoded list of functions when no functions are returned (#46681) --- .../app/plugins/datasource/graphite/datasource.test.ts | 8 ++++++++ public/app/plugins/datasource/graphite/datasource.ts | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/public/app/plugins/datasource/graphite/datasource.test.ts b/public/app/plugins/datasource/graphite/datasource.test.ts index aa120c42d61..da27191be21 100644 --- a/public/app/plugins/datasource/graphite/datasource.test.ts +++ b/public/app/plugins/datasource/graphite/datasource.test.ts @@ -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', () => { diff --git a/public/app/plugins/datasource/graphite/datasource.ts b/public/app/plugins/datasource/graphite/datasource.ts index b33baad6011..f09ea680694 100644 --- a/public/app/plugins/datasource/graphite/datasource.ts +++ b/public/app/plugins/datasource/graphite/datasource.ts @@ -769,6 +769,15 @@ export class GraphiteDatasource } else { 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; }), catchError((error: any) => {