Skip to content

Commit

Permalink
Add support for an inclusive job list in Jenkins plugin (influxdata#8287
Browse files Browse the repository at this point in the history
)

* Add support for an inclusive job list

* Update jenkins plugin tests

* Update jenkins plugin docs

* Update jenkins plugin docs
  • Loading branch information
JS1010111 committed Dec 23, 2020
1 parent bb0ef7c commit 1f380b2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
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 @@ -806,6 +807,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

0 comments on commit 1f380b2

Please sign in to comment.