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) => {