Skip to content

Commit 9cbc872

Browse files
authored
CloudMonitoring: Avoid to escape regexps in filters (grafana#41961)
1 parent adbc5bd commit 9cbc872

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

public/app/plugins/datasource/cloud-monitoring/datasource.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { chunk, flatten, isString } from 'lodash';
1+
import { chunk, flatten, isString, isArray } from 'lodash';
22
import { from, lastValueFrom, Observable, of } from 'rxjs';
33
import { map, mergeMap } from 'rxjs/operators';
44
import {
@@ -296,7 +296,9 @@ export default class CloudMonitoringDatasource extends DataSourceWithBackend<
296296
completeFilter.map(({ key, operator, value, condition }: Filter) => [
297297
this.templateSrv.replace(key, scopedVars || {}),
298298
operator,
299-
this.templateSrv.replace(value, scopedVars || {}, 'regex'),
299+
this.templateSrv.replace(value, scopedVars || {}, (value: string | string[]) => {
300+
return isArray(value) && value.length ? `(${value.join('|')})` : value;
301+
}),
300302
...(condition ? [condition] : []),
301303
])
302304
);

public/app/plugins/datasource/cloud-monitoring/specs/datasource.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ describe('CloudMonitoringDataSource', () => {
113113

114114
expect(interpolated[2]).toBe('(filtervalue1|filtervalue2)');
115115
});
116+
117+
it('should not escape a regex', () => {
118+
const templateSrv = initTemplateSrv('/[a-Z]*.html', true);
119+
const { ds } = getTestcontext({ templateSrv });
120+
const interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '[[test]]'], {});
121+
122+
expect(interpolated[2]).toBe('/[a-Z]*.html');
123+
});
124+
125+
it('should not escape an array of regexes but join them as a regex', () => {
126+
const templateSrv = initTemplateSrv(['/[a-Z]*.html', '/foo.html'], true);
127+
const { ds } = getTestcontext({ templateSrv });
128+
const interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '[[test]]'], {});
129+
130+
expect(interpolated[2]).toBe('(/[a-Z]*.html|/foo.html)');
131+
});
116132
});
117133
});
118134

0 commit comments

Comments
 (0)