From 21300a05023445c34dba3812e907a8248d590233 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Fri, 3 Oct 2025 15:22:30 +0100 Subject: [PATCH] [release-12.1.3] Influx: Improve variable regex handling (#111939) Influx: Improve variable regex handling (#111853) (cherry picked from commit b50737a76ceaa301d07deda42b8336e20b6d7953) Co-authored-by: Andreas Christou --- .../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 4633f20744a..8acca0c7ec5 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 a21031f20ab..92d26d51058 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -347,10 +347,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;