Add source and port tags to jenkins_job metrics (#6844)

This commit is contained in:
vikkyomkar 2020-01-10 07:22:26 +05:30 committed by Daniel Nelson
parent 7faf05023d
commit 1b1d78b8dd
3 changed files with 38 additions and 20 deletions

View File

@ -60,14 +60,15 @@ This plugin does not require a plugin on jenkins and it makes use of Jenkins API
- node_name
- status ("online", "offline")
- source
- port
- fields:
- disk_available
- temp_available
- memory_available
- memory_total
- swap_available
- swap_total
- response_time
- disk_available (Bytes)
- temp_available (Bytes)
- memory_available (Bytes)
- memory_total (Bytes)
- swap_available (Bytes)
- swap_total (Bytes)
- response_time (ms)
- num_executors
- jenkins_job
@ -76,8 +77,9 @@ This plugin does not require a plugin on jenkins and it makes use of Jenkins API
- parents
- result
- source
- port
- fields:
- duration
- duration (ms)
- result_code (0 = SUCCESS, 1 = FAILURE, 2 = NOT_BUILD, 3 = UNSTABLE, 4 = ABORTED)
### Sample Queries:
@ -94,7 +96,8 @@ SELECT mean("duration") AS "mean_duration" FROM "jenkins_job" WHERE time > now()
```
$ ./telegraf --config telegraf.conf --input-filter jenkins --test
jenkins_node,arch=Linux\ (amd64),disk_path=/var/jenkins_home,temp_path=/tmp,host=myhost,node_name=master swap_total=4294963200,memory_available=586711040,memory_total=6089498624,status=online,response_time=1000i,disk_available=152392036352,temp_available=152392036352,swap_available=3503263744 1516031535000000000
jenkins_job,host=myhost,name=JOB1,parents=apps/br1,result=SUCCESS duration=2831i,result_code=0i 1516026630000000000
jenkins_job,host=myhost,name=JOB2,parents=apps/br2,result=SUCCESS duration=2285i,result_code=0i 1516027230000000000
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

@ -22,6 +22,8 @@ type Jenkins struct {
URL string
Username string
Password string
Source string
Port string
// HTTP Timeout specified as a string - 3s, 1m, 1h
ResponseTimeout internal.Duration
@ -138,6 +140,22 @@ func (j *Jenkins) newHTTPClient() (*http.Client, error) {
func (j *Jenkins) initialize(client *http.Client) error {
var err error
// init jenkins tags
u, err := url.Parse(j.URL)
if err != nil {
return err
}
if u.Port() == "" {
if u.Scheme == "http" {
j.Port = "80"
} else if u.Scheme == "https" {
j.Port = "443"
}
} else {
j.Port = u.Port()
}
j.Source = u.Hostname()
// init job filter
j.jobFilter, err = filter.Compile(j.JobExclude)
if err != nil {
@ -191,12 +209,8 @@ func (j *Jenkins) gatherNodeData(n node, acc telegraf.Accumulator) error {
tags["status"] = "offline"
}
u, err := url.Parse(j.URL)
if err != nil {
return err
}
tags["source"] = u.Hostname()
tags["port"] = u.Port()
tags["source"] = j.Source
tags["port"] = j.Port
fields := make(map[string]interface{})
fields["num_executors"] = n.NumExecutors
@ -334,7 +348,7 @@ func (j *Jenkins) getJobDetail(jr jobRequest, acc telegraf.Accumulator) error {
return nil
}
gatherJobBuild(jr, build, acc)
j.gatherJobBuild(jr, build, acc)
return nil
}
@ -432,8 +446,8 @@ func (jr jobRequest) parentsString() string {
return strings.Join(jr.parents, "/")
}
func gatherJobBuild(jr jobRequest, b *buildResponse, acc telegraf.Accumulator) {
tags := map[string]string{"name": jr.name, "parents": jr.parentsString(), "result": b.Result}
func (j *Jenkins) gatherJobBuild(jr jobRequest, b *buildResponse, acc telegraf.Accumulator) {
tags := map[string]string{"name": jr.name, "parents": jr.parentsString(), "result": b.Result, "source": j.Source, "port": j.Port}
fields := make(map[string]interface{})
fields["duration"] = b.Duration
fields["result_code"] = mapResultCode(b.Result)

View File

@ -1,3 +1,4 @@
// Test Suite
package jenkins
import (