diff --git a/public/app/plugins/datasource/graphite/datasource.test.ts b/public/app/plugins/datasource/graphite/datasource.test.ts index 2034d847c21..0d0de3fb1b8 100644 --- a/public/app/plugins/datasource/graphite/datasource.test.ts +++ b/public/app/plugins/datasource/graphite/datasource.test.ts @@ -462,6 +462,52 @@ describe('graphiteDatasource', () => { expect(results[2]).toBe('target=' + encodeURIComponent('asPercent(aMetricName,sumSeries(aMetricName))')); }); + it('should use scoped variables when nesting query references', () => { + ctx.templateSrv.init([{ type: 'query', name: 'metric', current: { value: ['globalValue'] } }]); + + const originalTargetMap = { + A: '$metric', + B: 'sumSeries(#A)', + }; + + const scopedVars = { + metric: { text: 'scopedValue', value: 'scopedValue' }, + }; + + const results = ctx.ds.buildGraphiteParams( + { + targets: [{ target: '$metric' }, { target: 'sumSeries(#A)' }], + }, + originalTargetMap, + scopedVars + ); + + expect(results[1]).toBe('target=' + encodeURIComponent('sumSeries(scopedValue)')); + }); + + it('should apply scoped variables to nested references with hidden targets', () => { + ctx.templateSrv.init([{ type: 'query', name: 'server', current: { value: ['global'] } }]); + + const originalTargetMap = { + A: '$server.cpu', + B: 'avg(#A)', + }; + + const scopedVars = { + server: { text: 'web01', value: 'web01' }, + }; + + const results = ctx.ds.buildGraphiteParams( + { + targets: [{ target: '$server.cpu', hide: true }, { target: 'avg(#A)' }], + }, + originalTargetMap, + scopedVars + ); + + expect(results[0]).toBe('target=' + encodeURIComponent('avg(web01.cpu)')); + }); + it('should fix wrong minute interval parameters', () => { const originalTargetMap = { A: "summarize(prod.25m.count, '25m', 'sum')", diff --git a/public/app/plugins/datasource/graphite/datasource.ts b/public/app/plugins/datasource/graphite/datasource.ts index 0601bedeb9f..a2bf8e81e86 100644 --- a/public/app/plugins/datasource/graphite/datasource.ts +++ b/public/app/plugins/datasource/graphite/datasource.ts @@ -1029,7 +1029,7 @@ export class GraphiteDatasource } targetValue = targets[target.refId]; - targetValue = this.templateSrv.replace(targetValue.replace(regex, nestedSeriesRegexReplacer)); + targetValue = this.templateSrv.replace(targetValue.replace(regex, nestedSeriesRegexReplacer), scopedVars); targets[target.refId] = targetValue; if (!target.hide) {