From e017de4f061aab133054cf9622392475d80e8d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jamr=C3=B3z?= Date: Tue, 20 Apr 2021 13:21:57 +0200 Subject: [PATCH] Fix inefficient regular expression (#33155) --- .../plugins/datasource/graphite/datasource.ts | 29 +++++-------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/public/app/plugins/datasource/graphite/datasource.ts b/public/app/plugins/datasource/graphite/datasource.ts index f3209b89709..d2927cc5567 100644 --- a/public/app/plugins/datasource/graphite/datasource.ts +++ b/public/app/plugins/datasource/graphite/datasource.ts @@ -350,32 +350,17 @@ export class GraphiteDatasource extends DataSourceApi[,]*), this is used for template variables - let matches = interpolatedQuery.match(/^tag_values\(([^,]+)((, *[^,]+)*)\)$/); - if (matches) { - const expressions = []; - const exprRegex = /, *([^,]+)/g; - let match = exprRegex.exec(matches[2]); - while (match !== null) { - expressions.push(match[1]); - match = exprRegex.exec(matches[2]); - } + let allParams = interpolatedQuery.match(/^tag_values\((.*)\)$/); + let expressions = allParams ? allParams[1].split(',').filter((p) => !!p) : undefined; + if (expressions) { options.limit = 10000; - return this.getTagValuesAutoComplete(expressions, matches[1], undefined, options); + return this.getTagValuesAutoComplete(expressions.slice(1), expressions[0], undefined, options); } // special handling for tags([,]*), this is used for template variables - matches = interpolatedQuery.match(/^tags\(([^,]*)((, *[^,]+)*)\)$/); - if (matches) { - const expressions = []; - if (matches[1]) { - expressions.push(matches[1]); - const exprRegex = /, *([^,]+)/g; - let match = exprRegex.exec(matches[2]); - while (match !== null) { - expressions.push(match[1]); - match = exprRegex.exec(matches[2]); - } - } + allParams = interpolatedQuery.match(/^tags\((.*)\)$/); + expressions = allParams ? allParams[1].split(',').filter((p) => !!p) : undefined; + if (expressions) { options.limit = 10000; return this.getTagsAutoComplete(expressions, undefined, options); }