From b50737a76ceaa301d07deda42b8336e20b6d7953 Mon Sep 17 00:00:00 2001 From: Andreas Christou Date: Thu, 2 Oct 2025 15:57:39 +0200 Subject: [PATCH] Influx: Improve variable regex handling (#111853) --- .../datasource/influxdb/datasource.test.ts | 21 ++++++++++++++++ .../plugins/datasource/influxdb/datasource.ts | 25 +++++++++++-------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/datasource.test.ts b/public/app/plugins/datasource/influxdb/datasource.test.ts index 12ae898d58d..ec81ad57f1d 100644 --- a/public/app/plugins/datasource/influxdb/datasource.test.ts +++ b/public/app/plugins/datasource/influxdb/datasource.test.ts @@ -273,6 +273,15 @@ describe('interpolateQueryExpr', () => { replace: jest.fn().mockImplementation((...rest: unknown[]) => 'templateVarReplaced'), } as unknown as TemplateSrv; let ds = getMockInfluxDS(getMockDSInstanceSettings(), templateSrvStub); + + // Mock console.warn as we expect tests to use it + beforeEach(() => { + jest.spyOn(console, 'warn').mockImplementation(); + }); + afterEach(() => { + jest.restoreAllMocks(); + }); + it('should return the value as it is', () => { const value = 'normalValue'; const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build(); @@ -281,6 +290,18 @@ describe('interpolateQueryExpr', () => { expect(result).toBe(expectation); }); + it('should return the escaped value if the value wrapped in regex without !~ or =~', () => { + const value = '/special/path'; + const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build(); + const result = ds.interpolateQueryExpr( + value, + variableMock, + 'select atan(z/sqrt(3.14)), that where path /$tempVar/' + ); + const expectation = `\\/special\\/path`; + expect(result).toBe(expectation); + }); + it('should return the escaped value if the value wrapped in regex', () => { const value = '/special/path'; const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build(); diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index b073f333fcd..9fb42a6f236 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -360,10 +360,7 @@ export default class InfluxDatasource extends DataSourceWithBackend escapeRegex(v)).join('|')})`; + // If the value is a string array first escape them then join them with pipe + // then put inside parenthesis. + return typeof value === 'string' ? escapeRegex(value) : `(${value.map((v) => escapeRegex(v)).join('|')})`; + } catch (e) { + console.warn(`Supplied match is not valid regex: ${match}`); + } } return value;