Skip to content

Commit

Permalink
Merge pull request #80 from arromer/andres/agent-usage-percentage
Browse files Browse the repository at this point in the history
Add BusyAgentPercentage metric
  • Loading branch information
lox authored May 5, 2019
2 parents c245635 + 575f6ac commit e47fb1d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
IdleAgentCount = "IdleAgentCount"
BusyAgentCount = "BusyAgentCount"
TotalAgentCount = "TotalAgentCount"
BusyAgentPercentage = "BusyAgentPercentage"

PollDurationHeader = `Buildkite-Agent-Metrics-Poll-Duration`
)
Expand Down Expand Up @@ -156,6 +157,7 @@ func (c *Collector) Collect() (*Result, error) {
result.Totals[IdleAgentCount] = allMetrics.Agents.Idle
result.Totals[BusyAgentCount] = allMetrics.Agents.Busy
result.Totals[TotalAgentCount] = allMetrics.Agents.Total
result.Totals[BusyAgentPercentage] = busyAgentPercentage(allMetrics.Agents.metricsAgentsResponse)

for queueName, queueJobMetrics := range allMetrics.Jobs.Queues {
if _, ok := result.Queues[queueName]; !ok {
Expand All @@ -174,6 +176,7 @@ func (c *Collector) Collect() (*Result, error) {
result.Queues[queueName][IdleAgentCount] = queueAgentMetrics.Idle
result.Queues[queueName][BusyAgentCount] = queueAgentMetrics.Busy
result.Queues[queueName][TotalAgentCount] = queueAgentMetrics.Total
result.Queues[queueName][BusyAgentPercentage] = busyAgentPercentage(queueAgentMetrics)
}
} else {
log.Printf("Collecting agent metrics for queue '%s'", c.Queue)
Expand Down Expand Up @@ -233,6 +236,7 @@ func (c *Collector) Collect() (*Result, error) {
IdleAgentCount: queueMetrics.Agents.Idle,
BusyAgentCount: queueMetrics.Agents.Busy,
TotalAgentCount: queueMetrics.Agents.Total,
BusyAgentPercentage: busyAgentPercentage(queueMetrics.Agents),
}
}

Expand All @@ -243,6 +247,13 @@ func (c *Collector) Collect() (*Result, error) {
return result, nil
}

func busyAgentPercentage(agents metricsAgentsResponse) int {
if agents.Total > 0 {
return int(100 * agents.Busy / agents.Total)
}
return 0
}

func (r Result) Dump() {
for name, c := range r.Totals {
log.Printf("Buildkite > Org=%s > %s=%d", r.Org, name, c)
Expand Down
4 changes: 4 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestCollectorWithEmptyResponseForAllQueues(t *testing.T) {
{"Totals", res.Totals, TotalAgentCount, 0},
{"Totals", res.Totals, BusyAgentCount, 0},
{"Totals", res.Totals, IdleAgentCount, 0},
{"Totals", res.Totals, BusyAgentPercentage, 0},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -105,6 +106,7 @@ func TestCollectorWithNoJobsForAllQueues(t *testing.T) {
{"Totals", res.Totals, TotalAgentCount, 0},
{"Totals", res.Totals, BusyAgentCount, 0},
{"Totals", res.Totals, IdleAgentCount, 0},
{"Totals", res.Totals, BusyAgentPercentage, 0},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -183,6 +185,7 @@ func TestCollectorWithSomeJobsAndAgentsForAllQueues(t *testing.T) {
{"Totals", res.Totals, TotalAgentCount, 1},
{"Totals", res.Totals, BusyAgentCount, 1},
{"Totals", res.Totals, IdleAgentCount, 0},
{"Totals", res.Totals, BusyAgentPercentage, 100},

{"Queue.default", res.Queues["default"], RunningJobsCount, 1},
{"Queue.default", res.Queues["default"], ScheduledJobsCount, 2},
Expand Down Expand Up @@ -265,6 +268,7 @@ func TestCollectorWithSomeJobsAndAgentsForAQueue(t *testing.T) {
{"Queue.deploy", res.Queues["deploy"], TotalAgentCount, 1},
{"Queue.deploy", res.Queues["deploy"], BusyAgentCount, 1},
{"Queue.deploy", res.Queues["deploy"], IdleAgentCount, 0},
{"Queue.deploy", res.Queues["deploy"], BusyAgentPercentage, 100},
}

for _, tc := range testCases {
Expand Down

0 comments on commit e47fb1d

Please sign in to comment.