Skip to content

Commit

Permalink
feat(inputs.prometheus): always apply kubernetes label and field sele…
Browse files Browse the repository at this point in the history
…ctors
  • Loading branch information
redbaron committed Jul 14, 2023
1 parent 50b6a63 commit 67be135
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
32 changes: 15 additions & 17 deletions plugins/inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,23 @@ 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{}
tagFilter.Name = k
Expand Down
3 changes: 3 additions & 0 deletions plugins/inputs/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 67be135

Please sign in to comment.