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

Added new fields for Jenkins Total and Busy executors #6957

Merged
merged 4 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions plugins/inputs/jenkins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ SELECT mean("duration") AS "mean_duration" FROM "jenkins_job" WHERE time > now()

```
$ ./telegraf --config telegraf.conf --input-filter jenkins --test
jenkins,host=myhost,port=80,source=my-jenkins-instance busy_executors=4i,total_executors=8i 1580418261000000000
jenkins_node,arch=Linux\ (amd64),disk_path=/var/jenkins_home,temp_path=/tmp,host=myhost,node_name=master,source=my-jenkins-instance,port=8080 swap_total=4294963200,memory_available=586711040,memory_total=6089498624,status=online,response_time=1000i,disk_available=152392036352,temp_available=152392036352,swap_available=3503263744,num_executors=2i 1516031535000000000
jenkins_job,host=myhost,name=JOB1,parents=apps/br1,result=SUCCESS,source=my-jenkins-instance,port=8080 duration=2831i,result_code=0i 1516026630000000000
jenkins_job,host=myhost,name=JOB2,parents=apps/br2,result=SUCCESS,source=my-jenkins-instance,port=8080 duration=2285i,result_code=0i 1516027230000000000
Expand Down
20 changes: 17 additions & 3 deletions plugins/inputs/jenkins/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ const sampleConfig = `

// measurement
const (
measurementNode = "jenkins_node"
measurementJob = "jenkins_job"
measurementJenkins = "jenkins"
measurementNode = "jenkins_node"
measurementJob = "jenkins_job"
)

// SampleConfig implements telegraf.Input interface
Expand Down Expand Up @@ -244,6 +245,17 @@ func (j *Jenkins) gatherNodesData(acc telegraf.Accumulator) {
acc.AddError(err)
return
}

// get total and busy executors
if nodeResp.TotalExecutors != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should add this measurement even when total executors is 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated code as per the suggestion

tags := map[string]string{"source": j.Source, "port": j.Port}
fields := make(map[string]interface{})
fields["busy_executors"] = nodeResp.BusyExecutors
fields["total_executors"] = nodeResp.TotalExecutors

acc.AddFields(measurementJenkins, fields, tags)
}

// get node data
for _, node := range nodeResp.Computers {
err = j.gatherNodeData(node, acc)
Expand Down Expand Up @@ -353,7 +365,9 @@ func (j *Jenkins) getJobDetail(jr jobRequest, acc telegraf.Accumulator) error {
}

type nodeResponse struct {
Computers []node `json:"computer"`
Computers []node `json:"computer"`
BusyExecutors int `json:"busyExecutors"`
TotalExecutors int `json:"totalExecutors"`
}

type node struct {
Expand Down
53 changes: 46 additions & 7 deletions plugins/inputs/jenkins/jenkins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,37 @@ func TestGatherNodeData(t *testing.T) {
responseMap: map[string]interface{}{
"/api/json": struct{}{},
"/computer/api/json": nodeResponse{
BusyExecutors: 4,
TotalExecutors: 8,
Computers: []node{
{DisplayName: "ignore-1"},
{DisplayName: "ignore-2"},
},
},
},
},
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",
input: mockHandler{
responseMap: map[string]interface{}{
"/api/json": struct{}{},
"/computer/api/json": nodeResponse{
BusyExecutors: 4,
TotalExecutors: 8,
Computers: []node{
{
DisplayName: "master",
Expand Down Expand Up @@ -175,6 +192,15 @@ func TestGatherNodeData(t *testing.T) {
},
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,
},
},
{
Tags: map[string]string{
"node_name": "master",
Expand Down Expand Up @@ -203,6 +229,8 @@ func TestGatherNodeData(t *testing.T) {
responseMap: map[string]interface{}{
"/api/json": struct{}{},
"/computer/api/json": nodeResponse{
BusyExecutors: 4,
TotalExecutors: 8,
Computers: []node{
{
DisplayName: "slave",
Expand All @@ -216,6 +244,15 @@ func TestGatherNodeData(t *testing.T) {
},
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,
},
},
{
Tags: map[string]string{
"node_name": "slave",
Expand Down Expand Up @@ -254,14 +291,16 @@ func TestGatherNodeData(t *testing.T) {
if test.output == nil && len(acc.Metrics) > 0 {
t.Fatalf("%s: collected extra data", test.name)
} else if test.output != nil && len(test.output.Metrics) > 0 {
for k, m := range test.output.Metrics[0].Tags {
if acc.Metrics[0].Tags[k] != m {
t.Fatalf("%s: tag %s metrics unmatch Expected %s, got %s\n", test.name, k, m, acc.Metrics[0].Tags[k])
for i := 0; i < len(test.output.Metrics); i++ {
for k, m := range test.output.Metrics[i].Tags {
if acc.Metrics[i].Tags[k] != m {
t.Fatalf("%s: tag %s metrics unmatch Expected %s, got %s\n", test.name, k, m, acc.Metrics[0].Tags[k])
}
}
}
for k, m := range test.output.Metrics[0].Fields {
if acc.Metrics[0].Fields[k] != m {
t.Fatalf("%s: field %s metrics unmatch Expected %v(%T), got %v(%T)\n", test.name, k, m, m, acc.Metrics[0].Fields[k], acc.Metrics[0].Fields[k])
for k, m := range test.output.Metrics[i].Fields {
if acc.Metrics[i].Fields[k] != m {
t.Fatalf("%s: field %s metrics unmatch Expected %v(%T), got %v(%T)\n", test.name, k, m, m, acc.Metrics[0].Fields[k], acc.Metrics[0].Fields[k])
}
}
}
}
Expand Down