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

Update buildkite-metrics to use the agent metrics api #40

Merged
merged 4 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions .buildkite/steps/build-lambda.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ go_src_dir="/go/src/${go_pkg}"
version=$(awk -F\" '/const Version/ {print $2}' version/version.go)
dist_file="dist/buildkite-metrics-v${version}-lambda.zip"

docker run --rm -v "${PWD}:${go_src_dir}" -w "${go_src_dir}" eawsy/aws-lambda-go
mkdir -p dist/
mv handler.zip "$dist_file"

docker run --rm -v "${PWD}:${go_src_dir}" -w "${go_src_dir}" eawsy/aws-lambda-go --package "$dist_file"
buildkite-agent artifact upload "$dist_file"
10 changes: 5 additions & 5 deletions .buildkite/steps/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set -eu
go_version="1.9.2"
go_pkg="github.com/buildkite/buildkite-metrics"

docker run \
-v "${PWD}:/go/src/${go_pkg}" \
-w "/go/src/${go_pkg}" \
--rm "golang:${go_version}" \
go test -v ./...
docker run \
-v "${PWD}:/go/src/${go_pkg}" \
-w "/go/src/${go_pkg}" \
--rm "golang:${go_version}" \
go test -v ./...
45 changes: 29 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Buildkite Metrics

A command-line tool for collecting [Buildkite](https://buildkite.com/) build/job statistics for external metrics systems. Currently [AWS Cloudwatch](http://aws.amazon.com/cloudwatch/) and [StatsD](https://github.com/etsy/statsd) are supported.
A command-line tool for collecting [Buildkite](https://buildkite.com/) agent and job statistics for external metrics systems, focusing on enabling auto—scaling. Currently [AWS Cloudwatch](http://aws.amazon.com/cloudwatch/), [StatsD](https://github.com/etsy/statsd) and [Prometheus](https://prometheus.io) are supported.

[![Build status](https://badge.buildkite.com/80d04fcde3a306bef44e77aadb1f1ffdc20ebb3c8f1f585a60.svg)](https://buildkite.com/buildkite/buildkite-metrics)

Expand All @@ -12,60 +12,73 @@ Either download the latest binary from [Github Releases](https://github.com/buil
go get github.com/buildkite/buildkite-metrics
```

## Running

Run the metrics agent using an Agent Registration Token, found on the [Buildkite Agents page](https://buildkite.com/organizations/-/agents):

```
buildkite-metrics -token abc123...
```

By default this will publish metrics for every queue with connected agents and unfinished jobs, and a total of all metrics. Restrict it to a single queue with `-queue` if you're scaling a single cluster of agents:

```
buildkite-metrics -token abc123... -queue my-queue
```

### Backends

By default metrics will be submitted to CloudWatch but the backend can be switched to StatsD or Prometheus using the command-line argument `-backend statsd` or `-backend prometheus` respectively.

The StatsD backend supports the following arguments

* `-statsd-host HOST`: The StatsD host and port (defaults to `127.0.0.1:8125`).
* `-statsd-tags`: Some StatsD servers like the agent provided by DataDog support tags. If specified, metrics will be tagged by `queue` and `pipeline` otherwise metrics will include the queue/pipeline name in the metric. Only enable this option if you know your StatsD server supports tags.
* `-statsd-tags`: Some StatsD servers like the agent provided by DataDog support tags. If specified, metrics will be tagged by `queue` otherwise metrics will include the queue name in the metric. Only enable this option if you know your StatsD server supports tags.

The Prometheus backend supports the following arguments

* `-prometheus-addr`: The local address to listen on (defaults to `:8080`).
* `-prometheus-path`: The path under `prometheus-addr` to expose metrics on (defaults to `/metrics`).

### Upgrading from v2

1. The `-org` argument is no longer needed
2. The `-token` argument is now an _Agent Registration Token_ — the same used in the Buildkite Agent configuration file, and found on the [Buildkite Agents page](https://buildkite.com/organizations/-/agents).
3. Build and pipeline metrics have been removed, focusing on agents and jobs by queue for auto–scaling.
If you have a compelling reason to gather build or pipeline metrics please continue to use the [previous version](https://github.com/buildkite/buildkite-metrics/releases/tag/v2.1.0) or [open an issue](https://github.com/buildkite/buildkite-metrics/issues) with details.

## Development

You can build and run the binary tool locally with golang installed:

```
go run *.go -org [myorg] -token [buildkite api access token]
go run *.go -token [buildkite agent registration token]
```

Currently this will publish metrics to Cloudwatch under the custom metric prefix of `Buildkite`, using AWS credentials from your environment. The machine will require the [`cloudwatch:PutMetricData`](https://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/publishingMetrics.html) IAM permission, and the Buildkite API Access token requires the scopes `read_pipelines`, `read_builds` and `read_agents`.
Currently this will publish metrics to Cloudwatch under the custom metric prefix of `Buildkite`, using AWS credentials from your environment. The machine will require the [`cloudwatch:PutMetricData`](https://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/publishingMetrics.html) IAM permission.

## Metrics

The following metrics are gathered:
The following metrics are gathered when no specific queue is supplied:

```
Buildkite > RunningBuildsCount
Buildkite > RunningJobsCount
Buildkite > ScheduledBuildsCount
Buildkite > ScheduledJobsCount
Buildkite > UnfinishedJobsCount
Buildkite > IdleAgentsCount
Buildkite > BusyAgentsCount
Buildkite > TotalAgentsCount

Buildkite > (Queue) > RunningBuildsCount
Buildkite > (Queue) > RunningJobsCount
Buildkite > (Queue) > ScheduledBuildsCount
Buildkite > (Queue) > ScheduledJobsCount
Buildkite > (Queue) > UnfinishedJobsCount
Buildkite > (Queue) > IdleAgentsCount
Buildkite > (Queue) > BusyAgentsCount
Buildkite > (Queue) > TotalAgentsCount

Buildkite > (Pipeline) > RunningBuildsCount
Buildkite > (Pipeline) > RunningJobsCount
Buildkite > (Pipeline) > ScheduledBuildsCount
Buildkite > (Pipeline) > ScheduledJobsCount
Buildkite > (Pipeline) > UnfinishedJobsCount
```

When a queue is specified, only that queue's metrics are published.

## AWS Lambda

An AWS Lambda bundle is created and published as part of the build process.
Expand All @@ -78,7 +91,7 @@ It's entrypoint is `handler.handle`, it requires a `python2.7` environment and m
- BUILDKITE_QUEUE
- BUILDKITE_QUIET

Checkout https://github.com/buildkite/elastic-ci-stack-for-aws/blob/master/templates/metrics.yml for examples of usage.
Take a look at https://github.com/buildkite/elastic-ci-stack-for-aws/blob/master/templates/metrics.yml for examples of usage.

## License

Expand Down
6 changes: 0 additions & 6 deletions backend/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ func (cb *CloudWatchBackend) Collect(r *collector.Result) error {
})...)
}

for name, c := range r.Pipelines {
metrics = append(metrics, cloudwatchMetrics(c, []*cloudwatch.Dimension{
{Name: aws.String("Pipeline"), Value: aws.String(name)},
})...)
}

log.Printf("Extracted %d cloudwatch metrics from results", len(metrics))

for _, chunk := range chunkCloudwatchMetrics(10, metrics) {
Expand Down
15 changes: 0 additions & 15 deletions backend/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,6 @@ func (p *Prometheus) Collect(r *collector.Result) error {
}
}

for pipeline, counts := range r.Pipelines {
for name, value := range counts {
gauge, ok := p.pipelines[name]
if !ok {
gauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: fmt.Sprintf("buildkite_pipelines_%s", camelToUnderscore(name)),
Help: fmt.Sprintf("Buildkite Pipelines: %s", name),
}, []string{"pipeline"})
prometheus.MustRegister(gauge)
p.pipelines[name] = gauge
}
gauge.WithLabelValues(pipeline).Set(float64(value))
}
}

return nil
}

Expand Down
35 changes: 5 additions & 30 deletions backend/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,23 @@ const (

func newTestResult(t *testing.T) *collector.Result {
t.Helper()
pipelines := map[string]int{
totals := map[string]int{
"RunningBuildsCount": runningBuildsCount,
"ScheduledBuildsCount": scheduledBuildsCount,
"RunningJobsCount": runningJobsCount,
"ScheduledJobsCount": scheduledJobsCount,
"UnfinishedJobsCount": unfinishedJobsCount,
"IdleAgentCount": idleAgentCount,
"BusyAgentCount": busyAgentCount,
"TotalAgentCount": totalAgentCount,
}

totals := make(map[string]int)
for k, v := range pipelines {
totals[k] = v
}
totals["IdleAgentCount"] = idleAgentCount
totals["BusyAgentCount"] = busyAgentCount
totals["TotalAgentCount"] = totalAgentCount

res := &collector.Result{
Totals: totals,
Queues: map[string]map[string]int{
"default": totals,
"deploy": totals,
},
Pipelines: map[string]map[string]int{
"pipeline1": pipelines,
"pipeline2": pipelines,
},
}
return res
}
Expand Down Expand Up @@ -81,7 +72,7 @@ func gatherMetrics(t *testing.T) map[string]*dto.MetricFamily {
func TestCollect(t *testing.T) {
mfs := gatherMetrics(t)

if want, have := 21, len(mfs); want != have {
if want, have := 16, len(mfs); want != have {
t.Errorf("wanted %d Prometheus metrics, have: %d", want, have)
}

Expand Down Expand Up @@ -125,22 +116,6 @@ func TestCollect(t *testing.T) {
idleAgentCount,
dto.MetricType_GAUGE,
},
{
"Pipelines",
"buildkite_pipelines_running_builds_count",
"Buildkite Pipelines: RunningBuildsCount",
[]string{"pipeline1", "pipeline2"},
runningBuildsCount,
dto.MetricType_GAUGE,
},
{
"Pipelines",
"buildkite_pipelines_unfinished_jobs_count",
"Buildkite Pipelines: UnfinishedJobsCount",
[]string{"pipeline1", "pipeline2"},
unfinishedJobsCount,
dto.MetricType_GAUGE,
},
}

for _, tc := range tcs {
Expand Down
16 changes: 0 additions & 16 deletions backend/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@ func (cb *StatsD) Collect(r *collector.Result) error {
}
}

for pipeline, counts := range r.Pipelines {
for name, value := range counts {
var finalName string
tags := []string{}
if cb.tagsSupported {
finalName = "pipeline." + name
tags = []string{"pipeline:" + pipeline}
} else {
finalName = "pipeline." + pipeline + "." + name
}
if err := cb.client.Gauge(finalName, float64(value), tags, 1.0); err != nil {
return err
}
}
}

if err := cb.client.Flush(); err != nil {
return err
}
Expand Down
Loading