Skip to content

Commit

Permalink
support metrics variable in Grafana
Browse files Browse the repository at this point in the history
Signed-off-by: Augustin Husson <[email protected]>
  • Loading branch information
Nexucis committed Nov 18, 2024
1 parent 1703b60 commit 043cb4b
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/analyze/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
labelValuesRegexp = regexp.MustCompile(`(?s)label_values\((.+),.+\)`)
labelValuesNoQueryRegexp = regexp.MustCompile(`(?s)label_values\((.+)\)`)
queryResultRegexp = regexp.MustCompile(`(?s)query_result\((.+)\)`)
metricsRegexp = regexp.MustCompile(`(?s)metrics\((.+)\)`)
variableRangeQueryRangeRegex = regexp.MustCompile(`\[\$?\w+?]`)
variableSubqueryRangeRegex = regexp.MustCompile(`\[\$?\w+:\$?\w+?]`)
globalVariableList = []variableTuple{
Expand Down Expand Up @@ -229,6 +230,12 @@ func extractMetricsFromVariables(variables []templateVar, staticVariables *strin
} else if queryResultRegexp.MatchString(query) {
// query_result(query)
query = queryResultRegexp.FindStringSubmatch(query)[1]
// metrics(.*partial_metric_name)
} else if metricsRegexp.MatchString(query) {
// for this particular use case, the query is an invalid metric names so there is no need to use the PromQL parser.
query = metricsRegexp.FindStringSubmatch(query)[1]
invalidMetricsResult.Add(formatVariableInMetricName(query, allVariableNames))
continue
}
exprWithVariableReplaced := replaceVariables(query, staticVariables)
metrics, invalidMetrics, err := prometheus.AnalyzePromQLExpression(exprWithVariableReplaced)
Expand Down

0 comments on commit 043cb4b

Please sign in to comment.