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 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
9 changes: 6 additions & 3 deletions plugins/inputs/jenkins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ 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
# job_exclude = [ "job1", "job2/subjob1/subjob2", "job3/*"]
## Jobs to include or exclude from gathering
## When using both lists, job_exclude has priority.
## Wildcards are supported: [ "jobA/*", "jobB/subjob1/*"]
# job_include = [ "*" ]
# job_exclude = [ ]

## Nodes to exclude from gathering
# node_exclude = [ "node1", "node2" ]
# node_exclude = [ ]

## Worker pool for jenkins plugin only
## Empty this field will use default value 5
Expand Down
30 changes: 23 additions & 7 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,11 +79,14 @@ const sampleConfig = `
## empty will use default value 10
# max_subjob_per_layer = 10

## Jobs to exclude from gathering
# job_exclude = [ "job1", "job2/subjob1/subjob2", "job3/*"]
## Jobs to include or exclude from gathering
## When using both lists, job_exclude has priority.
## Wildcards are supported: [ "jobA/*", "jobB/subjob1/*"]
# job_include = [ "*" ]
# job_exclude = [ ]

## Nodes to exclude from gathering
# node_exclude = [ "node1", "node2" ]
# node_exclude = [ ]

## Worker pool for jenkins plugin only
## Empty this field will use default value 5
Expand Down Expand Up @@ -157,8 +162,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 +313,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