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

Debug flag now shows useful debugging, added dry-run #20

Merged
merged 1 commit into from
Dec 18, 2016
Merged
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
37 changes: 24 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ func main() {
orgSlug = flag.String("org", "", "A Buildkite Organization Slug")
interval = flag.Duration("interval", 0, "Update metrics every interval, rather than once")
history = flag.Duration("history", time.Hour*24, "Historical data to use for finished builds")
debug = flag.Bool("debug", false, "Show API debugging output")
debug = flag.Bool("debug", false, "Show debug output")
version = flag.Bool("version", false, "Show the version")
quiet = flag.Bool("quiet", false, "Only print errors")
dryRun = flag.Bool("dry-run", false, "Whether to only print metrics")

// filters
queue = flag.String("queue", "", "Only include a specific queue")
Expand Down Expand Up @@ -65,7 +66,9 @@ func main() {
}

client := buildkite.NewClient(config.Client())
buildkite.SetHttpDebug(*debug)
if *debug && os.Getenv("TRACE_HTTP") != "" {
buildkite.SetHttpDebug(*debug)
}

f := func() error {
t := time.Now()
Expand All @@ -74,6 +77,7 @@ func main() {
OrgSlug: *orgSlug,
Historical: *history,
Queue: *queue,
Debug: *debug,
})
if err != nil {
return err
Expand All @@ -83,9 +87,11 @@ func main() {
dumpResults(res)
}

err = cloudwatchSend(res)
if err != nil {
return err
if !*dryRun {
err = cloudwatchSend(res)
if err != nil {
return err
}
}

log.Printf("Finished in %s", time.Now().Sub(t))
Expand All @@ -111,6 +117,7 @@ type collectOpts struct {
OrgSlug string
Historical time.Duration
Queue string
Debug bool
}

func collectResults(client *buildkite.Client, opts collectOpts) (*result, error) {
Expand Down Expand Up @@ -253,8 +260,10 @@ func (r *result) addBuildAndJobMetrics(client *buildkite.Client, opts collectOpt

return currentBuilds.Pages(func(v interface{}) bool {
for _, build := range v.([]buildkite.Build) {
// log.Printf("Adding build to stats (id=%q, pipeline=%q, branch=%q, state=%q)",
// *build.ID, *build.Pipeline.Name, *build.Branch, *build.State)
if opts.Debug {
log.Printf("Adding build to stats (id=%q, pipeline=%q, branch=%q, state=%q)",
*build.ID, *build.Pipeline.Name, *build.Branch, *build.State)
}

if _, ok := r.pipelines[*build.Pipeline.Name]; !ok {
r.pipelines[*build.Pipeline.Name] = newCounts()
Expand Down Expand Up @@ -282,8 +291,10 @@ func (r *result) addBuildAndJobMetrics(client *buildkite.Client, opts collectOpt
state = *job.State
}

// log.Printf("Adding job to stats (id=%q, pipeline=%q, queue=%q, type=%q, state=%q)",
// *job.ID, *build.Pipeline.Name, queue(job), *job.Type, state)
if opts.Debug {
log.Printf("Adding job to stats (id=%q, pipeline=%q, queue=%q, type=%q, state=%q)",
*job.ID, *build.Pipeline.Name, queue(job), *job.Type, state)
}

if _, ok := r.queues[queue(job)]; !ok {
r.queues[queue(job)] = newCounts()
Expand Down Expand Up @@ -335,7 +346,6 @@ func (r *result) addAgentMetrics(client *buildkite.Client, opts collectOpts) err
if err != nil {
return nil, 0, err
}
log.Printf("Agents page %d has %d agents, next page is %d", page, len(agents), resp.NextPage)
return agents, resp.NextPage, err
},
}
Expand Down Expand Up @@ -369,8 +379,10 @@ func (r *result) addAgentMetrics(client *buildkite.Client, opts collectOpts) err
r.queues[queue][totalAgentCount] = 0
}

// log.Printf("Adding agent to stats (name=%q, queue=%q, job=%#v)",
// *agent.Name, queue, agent.Job != nil)
if opts.Debug {
log.Printf("Adding agent to stats (name=%q, queue=%q, job=%#v)",
*agent.Name, queue, agent.Job != nil)
}

if agent.Job != nil {
r.totals[busyAgentCount]++
Expand Down Expand Up @@ -422,7 +434,6 @@ func listBuildsByOrg(builds *buildkite.BuildsService, orgSlug string, opts build
if err != nil {
return nil, 0, err
}
log.Printf("Builds page %d has %d builds, next page is %d", page, len(builds), resp.NextPage)
return builds, resp.NextPage, err
},
}
Expand Down