Add new fields for Jenkins Total and Busy executors (#6957)

This commit is contained in:
vikkyomkar 2020-02-15 03:34:54 +05:30 committed by GitHub
parent 07a7b70f97
commit e056097cb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 11 deletions

View File

@ -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

View File

@ -90,6 +90,7 @@ const sampleConfig = `
// measurement
const (
measurementJenkins = "jenkins"
measurementNode = "jenkins_node"
measurementJob = "jenkins_job"
)
@ -244,6 +245,15 @@ func (j *Jenkins) gatherNodesData(acc telegraf.Accumulator) {
acc.AddError(err)
return
}
// get total and busy executors
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)
@ -354,6 +364,8 @@ func (j *Jenkins) getJobDetail(jr jobRequest, acc telegraf.Accumulator) error {
type nodeResponse struct {
Computers []node `json:"computer"`
BusyExecutors int `json:"busyExecutors"`
TotalExecutors int `json:"totalExecutors"`
}
type node struct {

View File

@ -106,6 +106,19 @@ func TestGatherNodeData(t *testing.T) {
},
},
wantErr: true,
output: &testutil.Accumulator{
Metrics: []*testutil.Metric{
{
Tags: map[string]string{
"source": "127.0.0.1",
},
Fields: map[string]interface{}{
"busy_executors": 0,
"total_executors": 0,
},
},
},
},
},
{
name: "empty monitor data",
@ -130,6 +143,8 @@ 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"},
@ -137,6 +152,19 @@ 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,
},
},
},
},
},
{
name: "normal data collection",
@ -144,6 +172,8 @@ func TestGatherNodeData(t *testing.T) {
responseMap: map[string]interface{}{
"/api/json": struct{}{},
"/computer/api/json": nodeResponse{
BusyExecutors: 4,
TotalExecutors: 8,
Computers: []node{
{
DisplayName: "master",
@ -175,6 +205,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",
@ -203,6 +242,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",
@ -216,6 +257,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",
@ -252,19 +302,21 @@ func TestGatherNodeData(t *testing.T) {
t.Fatalf("%s: expected err, got nil", test.name)
}
if test.output == nil && len(acc.Metrics) > 0 {
t.Fatalf("%s: collected extra data", test.name)
t.Fatalf("%s: collected extra data %s", test.name, acc.Metrics)
} 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 {
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 {
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])
}
}
}
}
})
}
}