Skip to content

Commit

Permalink
Merge pull request #50 from buildkite/add-http-client-timeouts
Browse files Browse the repository at this point in the history
Add a 5 second timeout for metrics requests
  • Loading branch information
lox authored Aug 17, 2018
2 parents 46d211e + a037ce2 commit e60b6b3
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package collector
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"log"
"time"
)

const (
ScheduledJobsCount = "ScheduledJobsCount"
RunningJobsCount = "RunningJobsCount"
UnfinishedJobsCount = "UnfinishedJobsCount"
IdleAgentCount = "IdleAgentCount"
BusyAgentCount = "BusyAgentCount"
TotalAgentCount = "TotalAgentCount"
ScheduledJobsCount = "ScheduledJobsCount"
RunningJobsCount = "RunningJobsCount"
UnfinishedJobsCount = "UnfinishedJobsCount"
IdleAgentCount = "IdleAgentCount"
BusyAgentCount = "BusyAgentCount"
TotalAgentCount = "TotalAgentCount"
)

type Collector struct {
Expand All @@ -34,20 +35,20 @@ type Result struct {
}

type metricsAgentsResponse struct {
Idle int `json:"idle"`
Busy int `json:"busy"`
Idle int `json:"idle"`
Busy int `json:"busy"`
Total int `json:"total"`
}

type metricsJobsResponse struct {
Scheduled int `json:"scheduled"`
Running int `json:"running"`
Total int `json:"total"`
Running int `json:"running"`
Total int `json:"total"`
}

type queueMetricsResponse struct {
Agents metricsAgentsResponse `json:"agents"`
Jobs metricsJobsResponse `json:"jobs"`
Jobs metricsJobsResponse `json:"jobs"`
}

type allMetricsAgentsResponse struct {
Expand All @@ -62,7 +63,7 @@ type allMetricsJobsResponse struct {

type allMetricsResponse struct {
Agents allMetricsAgentsResponse `json:"agents"`
Jobs allMetricsJobsResponse `json:"jobs"`
Jobs allMetricsJobsResponse `json:"jobs"`
}

func (c *Collector) Collect() (*Result, error) {
Expand All @@ -73,7 +74,7 @@ func (c *Collector) Collect() (*Result, error) {

if c.Queue == "" {
log.Println("Collecting agent metrics for all queues")

endpoint, err := url.Parse(c.Endpoint)
if err != nil {
return nil, err
Expand All @@ -95,7 +96,11 @@ func (c *Collector) Collect() (*Result, error) {
}
}

res, err := http.DefaultClient.Do(req)
httpClient := &http.Client{
Timeout: 5 * time.Second,
}

res, err := httpClient.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -120,7 +125,7 @@ func (c *Collector) Collect() (*Result, error) {
result.Totals[BusyAgentCount] = allMetrics.Agents.Busy
result.Totals[TotalAgentCount] = allMetrics.Agents.Total

for queueName, queueJobMetrics := range(allMetrics.Jobs.Queues) {
for queueName, queueJobMetrics := range allMetrics.Jobs.Queues {
if _, ok := result.Queues[queueName]; !ok {
result.Queues[queueName] = map[string]int{}
}
Expand All @@ -129,7 +134,7 @@ func (c *Collector) Collect() (*Result, error) {
result.Queues[queueName][UnfinishedJobsCount] = queueJobMetrics.Total
}

for queueName, queueAgentMetrics := range(allMetrics.Agents.Queues) {
for queueName, queueAgentMetrics := range allMetrics.Agents.Queues {
if _, ok := result.Queues[queueName]; !ok {
result.Queues[queueName] = map[string]int{}
}
Expand All @@ -139,7 +144,7 @@ func (c *Collector) Collect() (*Result, error) {
}
} else {
log.Printf("Collecting agent metrics for queue '%s'", c.Queue)

endpoint, err := url.Parse(c.Endpoint)
if err != nil {
return nil, err
Expand Down Expand Up @@ -181,12 +186,12 @@ func (c *Collector) Collect() (*Result, error) {
}

result.Queues[c.Queue] = map[string]int{
ScheduledJobsCount: queueMetrics.Jobs.Scheduled,
RunningJobsCount: queueMetrics.Jobs.Running,
ScheduledJobsCount: queueMetrics.Jobs.Scheduled,
RunningJobsCount: queueMetrics.Jobs.Running,
UnfinishedJobsCount: queueMetrics.Jobs.Total,
IdleAgentCount: queueMetrics.Agents.Idle,
BusyAgentCount: queueMetrics.Agents.Busy,
TotalAgentCount: queueMetrics.Agents.Total,
IdleAgentCount: queueMetrics.Agents.Idle,
BusyAgentCount: queueMetrics.Agents.Busy,
TotalAgentCount: queueMetrics.Agents.Total,
}
}

Expand Down

0 comments on commit e60b6b3

Please sign in to comment.