From d009795f1add77495c5888dcf20ad28a903b7190 Mon Sep 17 00:00:00 2001 From: Andreas Christou Date: Wed, 26 Feb 2025 14:01:26 +0000 Subject: [PATCH] Graphite: Handle graphite series' with spaces (#101231) * Handle graphite series' with spaces - Correctly type series response - Handle series names with spaces * Fix lint * Remove redundant block --- .../datasource/graphite/datasource.test.ts | 40 +++++++++++++++++++ .../plugins/datasource/graphite/datasource.ts | 20 ++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/public/app/plugins/datasource/graphite/datasource.test.ts b/public/app/plugins/datasource/graphite/datasource.test.ts index a9268dbaed9..de993ff6102 100644 --- a/public/app/plugins/datasource/graphite/datasource.test.ts +++ b/public/app/plugins/datasource/graphite/datasource.test.ts @@ -123,6 +123,46 @@ describe('graphiteDatasource', () => { expect(result.data[1].meta.notices).toBeUndefined(); expect(result.data[1].refId).toBe('B'); }); + it('handles series with spaces in the name', () => { + const refIDMap = { + refIDA: 'A', + refIDB: 'B', + }; + const result = ctx.ds.convertResponseToDataFrames( + createFetchResponse({ + meta: { + stats: { + 'executeplan.cache-hit-partial.count': 5, + 'executeplan.cache-hit.count': 10, + }, + }, + series: [ + { + target: 'series A with spaces refIDA', + datapoints: [ + [100, 200], + [101, 201], + ], + }, + { + target: 'series B with spaces refIDB', + datapoints: [ + [200, 300], + [201, 301], + ], + }, + ], + }), + refIDMap + ); + + expect(result.data.length).toBe(2); + expect(getFrameDisplayName(result.data[0])).toBe('series A with spaces'); + expect(getFrameDisplayName(result.data[1])).toBe('series B with spaces'); + expect(result.data[0].length).toBe(2); + expect(result.data[0].refId).toBe('A'); + expect(result.data[1].refId).toBe('B'); + }); }); describe('When querying graphite with one target using query editor target spec', () => { diff --git a/public/app/plugins/datasource/graphite/datasource.ts b/public/app/plugins/datasource/graphite/datasource.ts index 23979d1d546..9998fe8980e 100644 --- a/public/app/plugins/datasource/graphite/datasource.ts +++ b/public/app/plugins/datasource/graphite/datasource.ts @@ -42,6 +42,7 @@ import { GraphiteQueryType, GraphiteType, MetricTankRequestMeta, + MetricTankSeriesMeta, } from './types'; import { reduceError } from './utils'; import { DEFAULT_GRAPHITE_VERSION } from './versions'; @@ -285,7 +286,13 @@ export class GraphiteDatasource } // Series are either at the root or under a node called 'series' - const series = result.data.series || result.data; + const series: Array<{ + target: string; + title: string; + tags: Record; + datapoints: Array<[number, number]>; + meta: MetricTankSeriesMeta[]; + }> = result.data.series || result.data; if (!isArray(series)) { throw { message: 'Missing series in result', data: result }; @@ -293,10 +300,15 @@ export class GraphiteDatasource for (let i = 0; i < series.length; i++) { const s = series[i]; - // Retrieve the original refID of the query - const [target, refId] = s.target.split(' '); - s.target = target; + let refId = ''; + // Retrieve the original refID of the query + const splitTarget = s.target.split(' '); + if (splitTarget.length > 1) { + // refID should always be the last element + refId = splitTarget.pop() || ''; + s.target = splitTarget.join(' '); + } // Disables Grafana own series naming s.title = s.target;