feat(inputs.prometheus): Always apply kubernetes label and field selectors (#13624)

This commit is contained in:
Maxim Ivanov 2023-07-21 09:00:54 +00:00 committed by GitHub
parent 22100d4914
commit 42c7a2027f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 17 deletions

View File

@ -166,24 +166,22 @@ func (p *Prometheus) Init() error {
p.MonitorKubernetesPodsMethod = MonitorMethodAnnotations
}
if p.isNodeScrapeScope || p.MonitorKubernetesPodsMethod != MonitorMethodAnnotations {
// Parse label and field selectors - will be used to filter pods after cAdvisor call
var err error
p.podLabelSelector, err = labels.Parse(p.KubernetesLabelSelector)
if err != nil {
return fmt.Errorf("error parsing the specified label selector(s): %w", err)
}
p.podFieldSelector, err = fields.ParseSelector(p.KubernetesFieldSelector)
if err != nil {
return fmt.Errorf("error parsing the specified field selector(s): %w", err)
}
isValid, invalidSelector := fieldSelectorIsSupported(p.podFieldSelector)
if !isValid {
return fmt.Errorf("the field selector %q is not supported for pods", invalidSelector)
}
p.Log.Infof("Using the label selector: %v and field selector: %v", p.podLabelSelector, p.podFieldSelector)
// Parse label and field selectors - will be used to filter pods after cAdvisor call
var err error
p.podLabelSelector, err = labels.Parse(p.KubernetesLabelSelector)
if err != nil {
return fmt.Errorf("error parsing the specified label selector(s): %w", err)
}
p.podFieldSelector, err = fields.ParseSelector(p.KubernetesFieldSelector)
if err != nil {
return fmt.Errorf("error parsing the specified field selector(s): %w", err)
}
isValid, invalidSelector := fieldSelectorIsSupported(p.podFieldSelector)
if !isValid {
return fmt.Errorf("the field selector %q is not supported for pods", invalidSelector)
}
p.Log.Infof("Using the label selector: %v and field selector: %v", p.podLabelSelector, p.podFieldSelector)
for k, vs := range p.NamespaceAnnotationPass {
tagFilter := models.TagFilter{}

View File

@ -352,6 +352,7 @@ func TestPrometheusGeneratesSummaryMetricsV2(t *testing.T) {
defer ts.Close()
p := &Prometheus{
Log: &testutil.Logger{},
URLs: []string{ts.URL},
URLTag: "url",
MetricVersion: 2,
@ -385,6 +386,7 @@ go_gc_duration_seconds_count 42`
defer ts.Close()
p := &Prometheus{
Log: &testutil.Logger{},
URLs: []string{ts.URL},
URLTag: "",
MetricVersion: 2,
@ -444,6 +446,7 @@ func TestPrometheusGeneratesGaugeMetricsV2(t *testing.T) {
defer ts.Close()
p := &Prometheus{
Log: &testutil.Logger{},
URLs: []string{ts.URL},
URLTag: "url",
MetricVersion: 2,

View File

@ -389,6 +389,7 @@ rpc_duration_seconds_count 2693
})
input := &inputs.Prometheus{
Log: logger,
URLs: []string{address},
URLTag: "",
MetricVersion: 1,

View File

@ -420,6 +420,7 @@ rpc_duration_seconds_count 2693
})
input := &inputs.Prometheus{
Log: logger,
URLs: []string{url},
URLTag: "",
MetricVersion: 2,