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

Make the timeout configurable #184

Merged
merged 2 commits into from
Jul 13, 2023
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
3 changes: 2 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Collector struct {
Quiet bool
Debug bool
DebugHttp bool
Timeout int
}

type Result struct {
Expand Down Expand Up @@ -89,7 +90,7 @@ func (c *Collector) Collect() (*Result, error) {
}

httpClient := &http.Client{
Timeout: 15 * time.Second,
Timeout: time.Duration(c.Timeout) * time.Second,
}

if len(c.Queues) == 0 {
Expand Down
13 changes: 13 additions & 0 deletions lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -56,6 +57,7 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) {
clwDimensions := os.Getenv("BUILDKITE_CLOUDWATCH_DIMENSIONS")
quietString := os.Getenv("BUILDKITE_QUIET")
quiet := quietString == "1" || quietString == "true"
timeout := os.Getenv("BUILDKITE_AGENT_METRICS_TIMEOUT")

if quiet {
log.SetOutput(ioutil.Discard)
Expand Down Expand Up @@ -84,6 +86,16 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) {
queues = strings.Split(queue, ",")
}

if timeout == "" {
timeout = "15"
}

configuredTimeout, err := strconv.Atoi(timeout)

if err != nil {
return "", err
}

userAgent := fmt.Sprintf("buildkite-agent-metrics/%s buildkite-agent-metrics-lambda", version.Version)

c := collector.Collector{
Expand All @@ -94,6 +106,7 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) {
Quiet: quiet,
Debug: false,
DebugHttp: false,
Timeout: configuredTimeout,
}

switch strings.ToLower(backendOpt) {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func main() {
debugHttp = flag.Bool("debug-http", false, "Show full http traces")
dryRun = flag.Bool("dry-run", false, "Whether to only print metrics")
endpoint = flag.String("endpoint", "https://agent.buildkite.com/v3", "A custom Buildkite Agent API endpoint")
timeout = flag.Int("timeout", 15, "Timeout, in seconds, for HTTP requests to Buildkite API")

// backend config
backendOpt = flag.String("backend", "cloudwatch", "Specify the backend to use: cloudwatch, statsd, prometheus, stackdriver")
Expand Down Expand Up @@ -117,6 +118,7 @@ func main() {
Quiet: *quiet,
Debug: *debug,
DebugHttp: *debugHttp,
Timeout: *timeout,
}

f := func() (time.Duration, error) {
Expand Down