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

Add support for an inclusive job list in Jenkins plugin #8287

Merged
merged 4 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion plugins/inputs/jenkins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ This plugin does not require a plugin on jenkins and it makes use of Jenkins API
## empty will use default value 10
# max_subjob_per_layer = 10

## Jobs to exclude from gathering
## Jobs to include or exclude from gathering
## When using both lists, job_exclude has priority.
# job_include = [ "jobA", "jobB/subjobA/subjobB", "jobC*"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default should be an empty list with the meaning of "include all" as I guess that was the behavior before.

# job_exclude = [ "job1", "job2/subjob1/subjob2", "job3/*"]

## Nodes to exclude from gathering
Expand Down
25 changes: 20 additions & 5 deletions plugins/inputs/jenkins/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ type Jenkins struct {
MaxSubJobDepth int `toml:"max_subjob_depth"`
MaxSubJobPerLayer int `toml:"max_subjob_per_layer"`
JobExclude []string `toml:"job_exclude"`
jobFilter filter.Filter
JobInclude []string `toml:"job_include"`
jobFilterExclude filter.Filter
jobFilterInclude filter.Filter

NodeExclude []string `toml:"node_exclude"`
nodeFilter filter.Filter
Expand Down Expand Up @@ -77,7 +79,9 @@ const sampleConfig = `
## empty will use default value 10
# max_subjob_per_layer = 10

## Jobs to exclude from gathering
## Jobs to include or exclude from gathering
## When using both lists, job_exclude has priority.
# job_include = [ "jobA", "jobB/subjobA/subjobB", "jobC*"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in README.

# job_exclude = [ "job1", "job2/subjob1/subjob2", "job3/*"]

## Nodes to exclude from gathering
Expand Down Expand Up @@ -157,8 +161,13 @@ func (j *Jenkins) initialize(client *http.Client) error {
}
j.Source = u.Hostname()

// init job filter
j.jobFilter, err = filter.Compile(j.JobExclude)
// init job filters
j.jobFilterExclude, err = filter.Compile(j.JobExclude)
if err != nil {
return fmt.Errorf("error compile job filters[%s]: %v", j.URL, err)
}

j.jobFilterInclude, err = filter.Compile(j.JobInclude)
if err != nil {
return fmt.Errorf("error compile job filters[%s]: %v", j.URL, err)
}
Expand Down Expand Up @@ -303,8 +312,14 @@ func (j *Jenkins) getJobDetail(jr jobRequest, acc telegraf.Accumulator) error {
if j.MaxSubJobDepth > 0 && jr.layer == j.MaxSubJobDepth {
return nil
}

// filter out not included job.
if j.jobFilterInclude != nil && j.jobFilterInclude.Match(jr.hierarchyName()) == false {
return nil
}

// filter out excluded job.
if j.jobFilter != nil && j.jobFilter.Match(jr.hierarchyName()) {
if j.jobFilterExclude != nil && j.jobFilterExclude.Match(jr.hierarchyName()) {
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions plugins/inputs/jenkins/jenkins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ func TestInitialize(t *testing.T) {
Log: testutil.Logger{},
URL: ts.URL,
ResponseTimeout: internal.Duration{Duration: time.Microsecond},
JobInclude: []string{"jobA", "jobB"},
JobExclude: []string{"job1", "job2"},
NodeExclude: []string{"node1", "node2"},
},
Expand Down Expand Up @@ -792,6 +793,9 @@ func TestGatherJobs(t *testing.T) {
URL: ts.URL,
MaxBuildAge: internal.Duration{Duration: time.Hour},
ResponseTimeout: internal.Duration{Duration: time.Microsecond},
JobInclude: []string{
"*",
},
JobExclude: []string{
"ignore-1",
"apps/ignore-all/*",
Expand Down