Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rm GetLabelMatchersFromResourceQuery #647

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions pkg/prometheus-adapter/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ func GetMetricRulesFromResourceRules(cfg config.ResourceRules) ([]MetricRule, er
return nil, fmt.Errorf("unable to match <.GroupBy>")
}

labelMatchers := GetLabelMatchersFromResourceQuery(cfg.CPU.ContainerQuery)

queryTemplate := reg.ReplaceAllString(cfg.CPU.ContainerQuery, "")
templ, err := template.New("metrics-query").Delims("<<", ">>").Parse(queryTemplate)
if err != nil {
Expand All @@ -108,7 +106,6 @@ func GetMetricRulesFromResourceRules(cfg config.ResourceRules) ([]MetricRule, er
metricRules = append(metricRules, MetricRule{
MetricMatches: "cpu",
Template: templ,
LabelMatchers: labelMatchers,
Namespaced: true,
})
}
Expand All @@ -119,8 +116,6 @@ func GetMetricRulesFromResourceRules(cfg config.ResourceRules) ([]MetricRule, er
return nil, fmt.Errorf("unable to match <.GroupBy>")
}

labelMatchers := GetLabelMatchersFromResourceQuery(cfg.Memory.ContainerQuery)

queryTemplate := reg.ReplaceAllString(cfg.Memory.ContainerQuery, "")
templ, err := template.New("metrics-query").Delims("<<", ">>").Parse(queryTemplate)
if err != nil {
Expand All @@ -130,7 +125,6 @@ func GetMetricRulesFromResourceRules(cfg config.ResourceRules) ([]MetricRule, er
metricRules = append(metricRules, MetricRule{
MetricMatches: "memory",
Template: templ,
LabelMatchers: labelMatchers,
Namespaced: true,
})
}
Expand Down Expand Up @@ -268,23 +262,6 @@ func GetLabelMatchersFromDiscoveryRule(rule config.DiscoveryRule) []string {
return labelMatchers
}

// get labelMatchers from ResourceQuery
func GetLabelMatchersFromResourceQuery(query string) (labelMatchers []string) {
// add labelMatcher
regLabelMatchers := regexp.MustCompile("{(.*?)}")

if len(regLabelMatchers.FindStringSubmatch(query)) < 2 {
return labelMatchers
}

for _, labelMatcher := range strings.Split(regLabelMatchers.FindStringSubmatch(query)[1], ",") {
if labelMatcher != "<<.LabelMatchers>>" {
labelMatchers = append(labelMatchers, labelMatcher)
}
}
return labelMatchers
}

// get MetricMatches from DiscoveryRule
func GetMetricMatchesFromDiscoveryRule(rule config.DiscoveryRule) (metricMatches string, err error) {
seriesName := GetSeriesNameFromSeriesQuery(rule.SeriesQuery)
Expand Down
21 changes: 0 additions & 21 deletions pkg/prometheus-adapter/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,6 @@ func TestGetLabelMatchersFromDiscoveryRule(t *testing.T) {
}
}

func TestGetLabelMatchersFromResourceQuery(t *testing.T) {
containerQuery := `sum(rate(container_cpu_usage_seconds_total{<<.LabelMatchers>>,pod!=""}[3m])) by (<<.GroupBy>>)`

test := struct {
description string
resource string
expect []string
}{
description: "get expressionQuery For SeriesRules",
resource: containerQuery,
expect: []string{"pod!=\"\""},
}

requests := GetLabelMatchersFromResourceQuery(test.resource)
for i := range requests {
if requests[i] != test.expect[i] {
t.Errorf("expect requests %s actual requests %s", test.expect, requests)
}
}
}

func TestGetMetricMatchesFromDiscoveryRule(t *testing.T) {
seriesQuery := `nginx_concurrent_utilization{pod_namespace!="",pod_name!=""}`
metricsQuery := `sum(<<.Series>>{<<.LabelMatchers>>}) by (<<.GroupBy>>)`
Expand Down