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 source and port tags in jenkins_job metrics #6844

Merged
merged 4 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 14 additions & 11 deletions plugins/inputs/jenkins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
```

50 changes: 34 additions & 16 deletions plugins/inputs/jenkins/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,20 @@ func (j *Jenkins) initialize(client *http.Client) error {
return j.client.init()
}

func (j *Jenkins) gatherNodeData(n node, acc telegraf.Accumulator) error {

// Function to get source and port tags
func (j *Jenkins) getInitialTags() (map[string]string, error) {
tags := map[string]string{}
u, err := url.Parse(j.URL)
if err != nil {
return tags, err
}
tags["source"] = u.Hostname()
tags["port"] = u.Port()
return tags, nil
}

func (j *Jenkins) gatherNodeData(tags map[string]string, n node, acc telegraf.Accumulator) error {

if n.DisplayName == "" {
return fmt.Errorf("error empty node name")
}
Expand All @@ -191,13 +202,6 @@ 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()

fields := make(map[string]interface{})
fields["num_executors"] = n.NumExecutors

Expand Down Expand Up @@ -230,9 +234,15 @@ func (j *Jenkins) gatherNodesData(acc telegraf.Accumulator) {
acc.AddError(err)
return
}
// Get source and port tags
tags, err := j.getInitialTags()
if err != nil {
acc.AddError(err)
return
}
// get node data
for _, node := range nodeResp.Computers {
err = j.gatherNodeData(node, acc)
err = j.gatherNodeData(tags, node, acc)
if err == nil {
continue
}
Expand All @@ -246,12 +256,18 @@ func (j *Jenkins) gatherJobs(acc telegraf.Accumulator) {
acc.AddError(err)
return
}
// Get source and port tags
tags, err := j.getInitialTags()
if err != nil {
acc.AddError(err)
return
}
var wg sync.WaitGroup
for _, job := range js.Jobs {
wg.Add(1)
go func(name string, wg *sync.WaitGroup, acc telegraf.Accumulator) {
defer wg.Done()
if err := j.getJobDetail(jobRequest{
if err := j.getJobDetail(tags, jobRequest{
name: name,
parents: []string{},
layer: 0,
Expand All @@ -275,7 +291,7 @@ func (j *Jenkins) doGet(tcp func() error) error {
return nil
}

func (j *Jenkins) getJobDetail(jr jobRequest, acc telegraf.Accumulator) error {
func (j *Jenkins) getJobDetail(tags map[string]string, jr jobRequest, acc telegraf.Accumulator) error {
if j.MaxSubJobDepth > 0 && jr.layer == j.MaxSubJobDepth {
return nil
}
Expand All @@ -298,7 +314,7 @@ func (j *Jenkins) getJobDetail(jr jobRequest, acc telegraf.Accumulator) error {
// schedule tcp fetch for inner jobs
go func(ij innerJob, jr jobRequest, acc telegraf.Accumulator) {
defer wg.Done()
if err := j.getJobDetail(jobRequest{
if err := j.getJobDetail(tags, jobRequest{
name: ij.Name,
parents: jr.combined(),
layer: jr.layer + 1,
Expand Down Expand Up @@ -334,7 +350,7 @@ func (j *Jenkins) getJobDetail(jr jobRequest, acc telegraf.Accumulator) error {
return nil
}

gatherJobBuild(jr, build, acc)
gatherJobBuild(tags, jr, build, acc)
return nil
}

Expand Down Expand Up @@ -432,8 +448,10 @@ 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 gatherJobBuild(tags map[string]string, jr jobRequest, b *buildResponse, acc telegraf.Accumulator) {
tags["name"] = jr.name
tags["parents"] = jr.parentsString()
tags["result"] = b.Result
vikkyomkar marked this conversation as resolved.
Show resolved Hide resolved
fields := make(map[string]interface{})
fields["duration"] = b.Duration
fields["result_code"] = mapResultCode(b.Result)
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/jenkins/jenkins_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Test Suite
package jenkins

import (
Expand Down