diff --git a/public/app/plugins/datasource/loki/importing/fromGraphite.ts b/public/app/plugins/datasource/loki/importing/fromGraphite.ts index 9243900f012..75986642a9c 100644 --- a/public/app/plugins/datasource/loki/importing/fromGraphite.ts +++ b/public/app/plugins/datasource/loki/importing/fromGraphite.ts @@ -12,6 +12,17 @@ const GRAPHITE_TO_LOKI_OPERATOR = { '!=~': '!~', }; +/** + * Converts Graphite glob-like pattern to a regular expression + */ +function convertGlobToRegEx(text: string): string { + if (text.includes('*') || text.includes('{')) { + return '^' + text.replace(/\*/g, '.*').replace(/\{/g, '(').replace(/}/g, ')').replace(/,/g, '|'); + } else { + return text; + } +} + export default function fromGraphiteQueries( graphiteQueries: GraphiteQuery[], graphiteDataSource: GraphiteDatasource @@ -62,17 +73,12 @@ function fromGraphite(graphiteQuery: GraphiteQueryModel, config: GraphiteToLokiQ return true; } - if (value.includes('{')) { - labels[matcher.labelName] = { - value: value.replace(/\*/g, '.*').replace(/\{/g, '(').replace(/}/g, ')').replace(/,/g, '|'), - operator: '=~', - }; - } else { - labels[matcher.labelName] = { - value: value, - operator: '=', - }; - } + const converted = convertGlobToRegEx(value); + labels[matcher.labelName] = { + value: converted, + operator: converted !== value ? '=~' : '=', + }; + return true; } return targetNodes[index] === matcher.value || matcher.value === '*'; diff --git a/public/app/plugins/datasource/loki/importing/importing.test.ts b/public/app/plugins/datasource/loki/importing/importing.test.ts index 65ec715ce07..8b909c38ffb 100644 --- a/public/app/plugins/datasource/loki/importing/importing.test.ts +++ b/public/app/plugins/datasource/loki/importing/importing.test.ts @@ -34,7 +34,7 @@ describe('importing from Graphite queries', () => { beforeEach(() => {}); - it('test', () => { + it('test matching mappings', () => { mockSettings(['servers.(cluster).(server).*']); const lokiQueries = fromGraphiteQueries( [ @@ -45,6 +45,8 @@ describe('importing from Graphite queries', () => { // tags: captured mockGraphiteQuery("interpolate(seriesByTag('cluster=west', 'server=002'), inf))"), mockGraphiteQuery("interpolate(seriesByTag('foo=bar', 'server=002'), inf))"), + // regexp + mockGraphiteQuery('interpolate(alias(servers.eas*.{001,002}.request.POST.200,1,2))'), // not captured mockGraphiteQuery('interpolate(alias(test.west.001.cpu))'), mockGraphiteQuery('interpolate(alias(servers.west.001))'), @@ -56,8 +58,12 @@ describe('importing from Graphite queries', () => { { refId: 'A', expr: '{cluster="west", server="001"}' }, { refId: 'A', expr: '{cluster="east", server="001"}' }, { refId: 'A', expr: '{server="002"}' }, + { refId: 'A', expr: '{cluster="west", server="002"}' }, { refId: 'A', expr: '{foo="bar", server="002"}' }, + + { refId: 'A', expr: '{cluster=~"^eas.*", server=~"^(001|002)"}' }, + { refId: 'A', expr: '' }, { refId: 'A', expr: '' }, ]);