Skip to content

Commit

Permalink
feat: always make sure to close request bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
tboerger committed Jun 5, 2023
1 parent ea6222e commit 8825ada
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
3 changes: 2 additions & 1 deletion pkg/exporter/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,9 @@ func (c *AdminCollector) Collect(ch chan<- prometheus.Metric) {
defer cancel()

now := time.Now()
record, _, err := c.client.Admin.GetAdminStats(ctx)
record, resp, err := c.client.Admin.GetAdminStats(ctx)
c.duration.WithLabelValues("admin").Observe(time.Since(now).Seconds())
defer resp.Body.Close()

if err != nil {
level.Error(c.logger).Log(
Expand Down
18 changes: 12 additions & 6 deletions pkg/exporter/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ func (c *BillingCollector) getActionBilling() []*actionBilling {
}

record := &github.ActionBilling{}
_, err = c.client.Do(ctx, req, record)
resp, err := c.client.Do(ctx, req, record)
defer resp.Body.Close()

if err != nil {
level.Error(c.logger).Log(
Expand All @@ -387,7 +388,8 @@ func (c *BillingCollector) getActionBilling() []*actionBilling {
}

for _, name := range c.config.Orgs.Value() {
record, _, err := c.client.Billing.GetActionsBillingOrg(ctx, name)
record, resp, err := c.client.Billing.GetActionsBillingOrg(ctx, name)
defer resp.Body.Close()

if err != nil {
level.Error(c.logger).Log(
Expand Down Expand Up @@ -443,7 +445,8 @@ func (c *BillingCollector) getPackageBilling() []*packageBilling {
}

record := &github.PackageBilling{}
_, err = c.client.Do(ctx, req, record)
resp, err := c.client.Do(ctx, req, record)
defer resp.Body.Close()

if err != nil {
level.Error(c.logger).Log(
Expand All @@ -468,7 +471,8 @@ func (c *BillingCollector) getPackageBilling() []*packageBilling {
ctx, cancel := context.WithTimeout(context.Background(), c.config.Timeout)
defer cancel()

record, _, err := c.client.Billing.GetPackagesBillingOrg(ctx, name)
record, resp, err := c.client.Billing.GetPackagesBillingOrg(ctx, name)
defer resp.Body.Close()

if err != nil {
level.Error(c.logger).Log(
Expand Down Expand Up @@ -524,7 +528,8 @@ func (c *BillingCollector) getStorageBilling() []*storageBilling {
}

record := &github.StorageBilling{}
_, err = c.client.Do(ctx, req, record)
resp, err := c.client.Do(ctx, req, record)
defer resp.Body.Close()

if err != nil {
level.Error(c.logger).Log(
Expand All @@ -549,7 +554,8 @@ func (c *BillingCollector) getStorageBilling() []*storageBilling {
ctx, cancel := context.WithTimeout(context.Background(), c.config.Timeout)
defer cancel()

record, _, err := c.client.Billing.GetStorageBillingOrg(ctx, name)
record, resp, err := c.client.Billing.GetStorageBillingOrg(ctx, name)
defer resp.Body.Close()

if err != nil {
level.Error(c.logger).Log(
Expand Down
3 changes: 3 additions & 0 deletions pkg/exporter/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func reposByOwnerAndName(ctx context.Context, client *github.Client, owner, repo
)

if err != nil {
resp.Body.Close()
return nil, err
}

Expand All @@ -55,9 +56,11 @@ func reposByOwnerAndName(ctx context.Context, client *github.Client, owner, repo
)

if resp.NextPage == 0 {
resp.Body.Close()
break
}

resp.Body.Close()
opts.Page = resp.NextPage
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/exporter/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ func (c *OrgCollector) Collect(ch chan<- prometheus.Metric) {
defer cancel()

now := time.Now()
record, _, err := c.client.Organizations.Get(ctx, name)
record, resp, err := c.client.Organizations.Get(ctx, name)
c.duration.WithLabelValues("org").Observe(time.Since(now).Seconds())
defer resp.Body.Close()

if err != nil {
level.Error(c.logger).Log(
Expand Down
9 changes: 9 additions & 0 deletions pkg/exporter/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ func (c *RunnerCollector) pagedRepoRunners(ctx context.Context, owner, name stri
)

if err != nil {
resp.Body.Close()
return nil, err
}

Expand All @@ -402,9 +403,11 @@ func (c *RunnerCollector) pagedRepoRunners(ctx context.Context, owner, name stri
)

if resp.NextPage == 0 {
resp.Body.Close()
break
}

resp.Body.Close()
opts.Page = resp.NextPage
}

Expand Down Expand Up @@ -459,6 +462,7 @@ func (c *RunnerCollector) pagedEnterpriseRunners(ctx context.Context, name strin
)

if err != nil {
resp.Body.Close()
return nil, err
}

Expand All @@ -468,9 +472,11 @@ func (c *RunnerCollector) pagedEnterpriseRunners(ctx context.Context, name strin
)

if resp.NextPage == 0 {
resp.Body.Close()
break
}

resp.Body.Close()
opts.Page = resp.NextPage
}

Expand Down Expand Up @@ -525,6 +531,7 @@ func (c *RunnerCollector) pagedOrgRunners(ctx context.Context, name string) ([]*
)

if err != nil {
resp.Body.Close()
return nil, err
}

Expand All @@ -534,9 +541,11 @@ func (c *RunnerCollector) pagedOrgRunners(ctx context.Context, name string) ([]*
)

if resp.NextPage == 0 {
resp.Body.Close()
break
}

resp.Body.Close()
opts.Page = resp.NextPage
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/exporter/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func (c *WorkflowCollector) pagedRepoWorkflows(ctx context.Context, owner, name
)

if err != nil {
resp.Body.Close()
return nil, err
}

Expand All @@ -257,9 +258,11 @@ func (c *WorkflowCollector) pagedRepoWorkflows(ctx context.Context, owner, name
)

if resp.NextPage == 0 {
resp.Body.Close()
break
}

resp.Body.Close()
opts.Page = resp.NextPage
}

Expand Down

0 comments on commit 8825ada

Please sign in to comment.