diff --git a/collector/collector.go b/collector/collector.go index 4eba7684..bf6c9025 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -19,6 +19,7 @@ const ( IdleAgentCount = "IdleAgentCount" BusyAgentCount = "BusyAgentCount" TotalAgentCount = "TotalAgentCount" + BusyAgentPercentage = "BusyAgentPercentage" PollDurationHeader = `Buildkite-Agent-Metrics-Poll-Duration` ) @@ -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 { @@ -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) @@ -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), } } @@ -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) diff --git a/collector/collector_test.go b/collector/collector_test.go index ac74d7b8..b591f917 100644 --- a/collector/collector_test.go +++ b/collector/collector_test.go @@ -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 { @@ -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 { @@ -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}, @@ -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 {