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

This change will ensure a single HTTP client is created #285

Merged
merged 1 commit into from
Jun 4, 2024
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
35 changes: 24 additions & 11 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log"
"maps"
"net"
"net/http"
"net/http/httptrace"
"net/http/httputil"
Expand Down Expand Up @@ -36,14 +37,14 @@ var ErrUnauthorized = errors.New("unauthorized")
var traceLog = log.New(os.Stderr, "TRACE", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile|log.Lmsgprefix)

type Collector struct {
Client *http.Client
Endpoint string
Token string
UserAgent string
Queues []string
Quiet bool
Debug bool
DebugHttp bool
Timeout int
}

type Result struct {
Expand Down Expand Up @@ -105,17 +106,13 @@ func (c *Collector) Collect() (*Result, error) {
Queues: map[string]map[string]int{},
}

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

if len(c.Queues) == 0 {
if err := c.collectAllQueues(httpClient, result); err != nil {
if err := c.collectAllQueues(result); err != nil {
return nil, err
}
} else {
for _, queue := range c.Queues {
if err := c.collectQueue(httpClient, result, queue); err != nil {
if err := c.collectQueue(result, queue); err != nil {
return nil, err
}
}
Expand All @@ -128,7 +125,7 @@ func (c *Collector) Collect() (*Result, error) {
return result, nil
}

func (c *Collector) collectAllQueues(httpClient *http.Client, result *Result) error {
func (c *Collector) collectAllQueues(result *Result) error {
log.Println("Collecting agent metrics for all queues")

endpoint, err := url.Parse(c.Endpoint)
Expand All @@ -150,7 +147,7 @@ func (c *Collector) collectAllQueues(httpClient *http.Client, result *Result) er
req = traceHTTPRequest(req)
}

res, err := httpClient.Do(req)
res, err := c.Client.Do(req)
if err != nil {
return err
}
Expand Down Expand Up @@ -241,7 +238,7 @@ func (c *Collector) collectAllQueues(httpClient *http.Client, result *Result) er
return nil
}

func (c *Collector) collectQueue(httpClient *http.Client, result *Result, queue string) error {
func (c *Collector) collectQueue(result *Result, queue string) error {
log.Printf("Collecting agent metrics for queue '%s'", queue)

endpoint, err := url.Parse(c.Endpoint)
Expand All @@ -264,7 +261,7 @@ func (c *Collector) collectQueue(httpClient *http.Client, result *Result, queue
req = traceHTTPRequest(req)
}

res, err := httpClient.Do(req)
res, err := c.Client.Do(req)
if err != nil {
return err
}
Expand Down Expand Up @@ -409,3 +406,19 @@ func traceHTTPRequest(req *http.Request) *http.Request {
traceLog.Printf("request uri=%s\n%s", req.URL, dump)
return req
}

func NewHTTPClient(timeout int) *http.Client {

connectionTimeout := time.Duration(timeout) * time.Second

return &http.Client{
Timeout: connectionTimeout,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: connectionTimeout,
KeepAlive: connectionTimeout,
}).Dial,
TLSHandshakeTimeout: connectionTimeout,
},
}
}
4 changes: 4 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestCollectorWithEmptyResponseForAllQueues(t *testing.T) {
}
}))
c := &Collector{
Client: &http.Client{},
Endpoint: s.URL,
Token: "abc123",
UserAgent: "some-client/1.2.3",
Expand Down Expand Up @@ -86,6 +87,7 @@ func TestCollectorWithNoJobsForAllQueues(t *testing.T) {
}
}))
c := &Collector{
Client: &http.Client{},
Endpoint: s.URL,
Token: "abc123",
UserAgent: "some-client/1.2.3",
Expand Down Expand Up @@ -165,6 +167,7 @@ func TestCollectorWithSomeJobsAndAgentsForAllQueues(t *testing.T) {
}
}))
c := &Collector{
Client: &http.Client{},
Endpoint: s.URL,
Token: "abc123",
UserAgent: "some-client/1.2.3",
Expand Down Expand Up @@ -244,6 +247,7 @@ func TestCollectorWithSomeJobsAndAgentsForAQueue(t *testing.T) {
}
}))
c := &Collector{
Client: &http.Client{},
Endpoint: s.URL,
Token: "abc123",
UserAgent: "some-client/1.2.3",
Expand Down
4 changes: 3 additions & 1 deletion lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,21 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) {
return "", err
}

httpClient := collector.NewHTTPClient(configuredTimeout)

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

collectors := make([]*collector.Collector, 0, len(tokens))
for _, token := range tokens {
collectors = append(collectors, &collector.Collector{
Client: httpClient,
UserAgent: userAgent,
Endpoint: "https://agent.buildkite.com/v3",
Token: token,
Queues: queues,
Quiet: quiet,
Debug: debug,
DebugHttp: debugHTTP,
Timeout: configuredTimeout,
})
}

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,19 @@ func main() {
}
}

httpClient := collector.NewHTTPClient(*timeout)

collectors := make([]*collector.Collector, 0, len(tokens))
for _, token := range tokens {
collectors = append(collectors, &collector.Collector{
Client: httpClient,
UserAgent: userAgent,
Endpoint: *endpoint,
Token: token,
Queues: []string(queues),
Quiet: *quiet,
Debug: *debug,
DebugHttp: *debugHttp,
Timeout: *timeout,
})
}

Expand Down