Add include_nodes filter for jenkins (#9351)
This commit is contained in:
parent
e0ac5078bb
commit
17e86ab4ca
|
|
@ -45,7 +45,9 @@ This plugin does not require a plugin on jenkins and it makes use of Jenkins API
|
||||||
# job_include = [ "*" ]
|
# job_include = [ "*" ]
|
||||||
# job_exclude = [ ]
|
# job_exclude = [ ]
|
||||||
|
|
||||||
## Nodes to exclude from gathering
|
## Nodes to include or exclude from gathering
|
||||||
|
## When using both lists, node_exclude has priority.
|
||||||
|
# node_include = [ "*" ]
|
||||||
# node_exclude = [ ]
|
# node_exclude = [ ]
|
||||||
|
|
||||||
## Worker pool for jenkins plugin only
|
## Worker pool for jenkins plugin only
|
||||||
|
|
|
||||||
|
|
@ -38,10 +38,10 @@ type Jenkins struct {
|
||||||
MaxSubJobPerLayer int `toml:"max_subjob_per_layer"`
|
MaxSubJobPerLayer int `toml:"max_subjob_per_layer"`
|
||||||
JobExclude []string `toml:"job_exclude"`
|
JobExclude []string `toml:"job_exclude"`
|
||||||
JobInclude []string `toml:"job_include"`
|
JobInclude []string `toml:"job_include"`
|
||||||
jobFilterExclude filter.Filter
|
jobFilter filter.Filter
|
||||||
jobFilterInclude filter.Filter
|
|
||||||
|
|
||||||
NodeExclude []string `toml:"node_exclude"`
|
NodeExclude []string `toml:"node_exclude"`
|
||||||
|
NodeInclude []string `toml:"node_include"`
|
||||||
nodeFilter filter.Filter
|
nodeFilter filter.Filter
|
||||||
|
|
||||||
semaphore chan struct{}
|
semaphore chan struct{}
|
||||||
|
|
@ -85,7 +85,9 @@ const sampleConfig = `
|
||||||
# job_include = [ "*" ]
|
# job_include = [ "*" ]
|
||||||
# job_exclude = [ ]
|
# job_exclude = [ ]
|
||||||
|
|
||||||
## Nodes to exclude from gathering
|
## Nodes to include or exclude from gathering
|
||||||
|
## When using both lists, node_exclude has priority.
|
||||||
|
# node_include = [ "*" ]
|
||||||
# node_exclude = [ ]
|
# node_exclude = [ ]
|
||||||
|
|
||||||
## Worker pool for jenkins plugin only
|
## Worker pool for jenkins plugin only
|
||||||
|
|
@ -162,21 +164,14 @@ func (j *Jenkins) initialize(client *http.Client) error {
|
||||||
}
|
}
|
||||||
j.Source = u.Hostname()
|
j.Source = u.Hostname()
|
||||||
|
|
||||||
// init job filters
|
// init filters
|
||||||
j.jobFilterExclude, err = filter.Compile(j.JobExclude)
|
j.jobFilter, err = filter.NewIncludeExcludeFilter(j.JobInclude, j.JobExclude)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error compile job filters[%s]: %v", j.URL, err)
|
return fmt.Errorf("error compiling job filters[%s]: %v", j.URL, err)
|
||||||
}
|
}
|
||||||
|
j.nodeFilter, err = filter.NewIncludeExcludeFilter(j.NodeInclude, j.NodeExclude)
|
||||||
j.jobFilterInclude, err = filter.Compile(j.JobInclude)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error compile job filters[%s]: %v", j.URL, err)
|
return fmt.Errorf("error compiling node filters[%s]: %v", j.URL, err)
|
||||||
}
|
|
||||||
|
|
||||||
// init node filter
|
|
||||||
j.nodeFilter, err = filter.Compile(j.NodeExclude)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error compile node filters[%s]: %v", j.URL, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// init tcp pool with default value
|
// init tcp pool with default value
|
||||||
|
|
@ -203,8 +198,9 @@ func (j *Jenkins) gatherNodeData(n node, acc telegraf.Accumulator) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
tags["node_name"] = n.DisplayName
|
tags["node_name"] = n.DisplayName
|
||||||
// filter out excluded node_name
|
|
||||||
if j.nodeFilter != nil && j.nodeFilter.Match(tags["node_name"]) {
|
// filter out excluded or not included node_name
|
||||||
|
if !j.nodeFilter.Match(tags["node_name"]) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -300,13 +296,8 @@ func (j *Jenkins) getJobDetail(jr jobRequest, acc telegraf.Accumulator) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter out not included job.
|
// filter out excluded or not included jobs
|
||||||
if j.jobFilterInclude != nil && !j.jobFilterInclude.Match(jr.hierarchyName()) {
|
if !j.jobFilter.Match(jr.hierarchyName()) {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// filter out excluded job.
|
|
||||||
if j.jobFilterExclude != nil && j.jobFilterExclude.Match(jr.hierarchyName()) {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,7 @@ func TestGatherNodeData(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "filtered nodes",
|
name: "filtered nodes (excluded)",
|
||||||
input: mockHandler{
|
input: mockHandler{
|
||||||
responseMap: map[string]interface{}{
|
responseMap: map[string]interface{}{
|
||||||
"/api/json": struct{}{},
|
"/api/json": struct{}{},
|
||||||
|
|
@ -184,6 +184,35 @@ func TestGatherNodeData(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "filtered nodes (included)",
|
||||||
|
input: mockHandler{
|
||||||
|
responseMap: map[string]interface{}{
|
||||||
|
"/api/json": struct{}{},
|
||||||
|
"/computer/api/json": nodeResponse{
|
||||||
|
BusyExecutors: 4,
|
||||||
|
TotalExecutors: 8,
|
||||||
|
Computers: []node{
|
||||||
|
{DisplayName: "filtered-1"},
|
||||||
|
{DisplayName: "filtered-1"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output: &testutil.Accumulator{
|
||||||
|
Metrics: []*testutil.Metric{
|
||||||
|
{
|
||||||
|
Tags: map[string]string{
|
||||||
|
"source": "127.0.0.1",
|
||||||
|
},
|
||||||
|
Fields: map[string]interface{}{
|
||||||
|
"busy_executors": 4,
|
||||||
|
"total_executors": 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "normal data collection",
|
name: "normal data collection",
|
||||||
input: mockHandler{
|
input: mockHandler{
|
||||||
|
|
@ -306,6 +335,7 @@ func TestGatherNodeData(t *testing.T) {
|
||||||
URL: ts.URL,
|
URL: ts.URL,
|
||||||
ResponseTimeout: config.Duration(time.Microsecond),
|
ResponseTimeout: config.Duration(time.Microsecond),
|
||||||
NodeExclude: []string{"ignore-1", "ignore-2"},
|
NodeExclude: []string{"ignore-1", "ignore-2"},
|
||||||
|
NodeInclude: []string{"master", "slave"},
|
||||||
}
|
}
|
||||||
te := j.initialize(&http.Client{Transport: &http.Transport{}})
|
te := j.initialize(&http.Client{Transport: &http.Transport{}})
|
||||||
acc := new(testutil.Accumulator)
|
acc := new(testutil.Accumulator)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue