Split ExternalMetricsQuery and MetricsQuery funcs

This commit is contained in:
Carson Anderson 2021-05-11 14:49:11 -06:00
parent c0ae5d6dd4
commit 6c1d85ccf9
4 changed files with 27 additions and 7 deletions

View file

@ -159,7 +159,7 @@ func NamersFromConfig(cfg []config.DiscoveryRule, mapper apimeta.RESTMapper) ([]
namespaced = *rule.Resources.Namespaced
}
metricsQuery, err := NewMetricsQuery(rule.MetricsQuery, resConv, namespaced)
metricsQuery, err := NewExternalMetricsQuery(rule.MetricsQuery, resConv, namespaced)
if err != nil {
return nil, fmt.Errorf("unable to construct metrics query associated with series query %q: %v", rule.SeriesQuery, err)
}

View file

@ -50,7 +50,27 @@ type MetricsQuery interface {
// - LabelMatchersByName: the raw map-form of the above matchers
// - GroupBy: the group-by clause to use for the resources in the query (stringified)
// - GroupBySlice: the raw slice form of the above group-by clause
func NewMetricsQuery(queryTemplate string, resourceConverter ResourceConverter, namespaced bool) (MetricsQuery, error) {
func NewMetricsQuery(queryTemplate string, resourceConverter ResourceConverter) (MetricsQuery, error) {
templ, err := template.New("metrics-query").Delims("<<", ">>").Parse(queryTemplate)
if err != nil {
return nil, fmt.Errorf("unable to parse metrics query template %q: %v", queryTemplate, err)
}
return &metricsQuery{
resConverter: resourceConverter,
template: templ,
namespaced: true,
}, nil
}
// NewExternalMetricsQuery constructs a new MetricsQuery by compiling the given Go template.
// The delimiters on the template are `<<` and `>>`, and it may use the following fields:
// - Series: the series in question
// - LabelMatchers: a pre-stringified form of the label matchers for the resources in the query
// - LabelMatchersByName: the raw map-form of the above matchers
// - GroupBy: the group-by clause to use for the resources in the query (stringified)
// - GroupBySlice: the raw slice form of the above group-by clause
func NewExternalMetricsQuery(queryTemplate string, resourceConverter ResourceConverter, namespaced bool) (MetricsQuery, error) {
templ, err := template.New("metrics-query").Delims("<<", ">>").Parse(queryTemplate)
if err != nil {
return nil, fmt.Errorf("unable to parse metrics query template %q: %v", queryTemplate, err)

View file

@ -76,7 +76,7 @@ func checks(cs ...checkFunc) checkFunc {
func TestBuildSelector(t *testing.T) {
mustNewQuery := func(queryTemplate string, namespaced bool) MetricsQuery {
mq, err := NewMetricsQuery(queryTemplate, &resourceConverterMock{namespaced}, true)
mq, err := NewMetricsQuery(queryTemplate, &resourceConverterMock{namespaced})
if err != nil {
t.Fatal(err)
}
@ -272,7 +272,7 @@ func TestBuildSelector(t *testing.T) {
func TestBuildExternalSelector(t *testing.T) {
mustNewQuery := func(queryTemplate string) MetricsQuery {
mq, err := NewMetricsQuery(queryTemplate, &resourceConverterMock{true}, true)
mq, err := NewExternalMetricsQuery(queryTemplate, &resourceConverterMock{true}, true)
if err != nil {
t.Fatal(err)
}
@ -280,7 +280,7 @@ func TestBuildExternalSelector(t *testing.T) {
}
mustNewNonNamespacedQuery := func(queryTemplate string) MetricsQuery {
mq, err := NewMetricsQuery(queryTemplate, &resourceConverterMock{true}, false)
mq, err := NewExternalMetricsQuery(queryTemplate, &resourceConverterMock{true}, false)
if err != nil {
t.Fatal(err)
}

View file

@ -54,11 +54,11 @@ func newResourceQuery(cfg config.ResourceRule, mapper apimeta.RESTMapper) (resou
return resourceQuery{}, fmt.Errorf("unable to construct label-resource converter: %v", err)
}
contQuery, err := naming.NewMetricsQuery(cfg.ContainerQuery, converter, true)
contQuery, err := naming.NewMetricsQuery(cfg.ContainerQuery, converter)
if err != nil {
return resourceQuery{}, fmt.Errorf("unable to construct container metrics query: %v", err)
}
nodeQuery, err := naming.NewMetricsQuery(cfg.NodeQuery, converter, true)
nodeQuery, err := naming.NewMetricsQuery(cfg.NodeQuery, converter)
if err != nil {
return resourceQuery{}, fmt.Errorf("unable to construct node metrics query: %v", err)
}