Skip to content

Commit

Permalink
add context for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tbauriedel committed Dec 21, 2023
1 parent bcc1d25 commit 95bc293
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ run:
linters:
enable-all: true
disable:
- goimports
- cyclop
- depguard
- exhaustivestruct
Expand Down
10 changes: 9 additions & 1 deletion modules/icinga2/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package icinga2

import (
"context"
"crypto/tls"
"fmt"
"github.com/NETWAYS/support-collector/internal/collection"
Expand Down Expand Up @@ -29,6 +30,7 @@ func InitAPICollection(c *collection.Collection) error {
if len(APIEndpoints) == 0 {
return fmt.Errorf("0 API endpoints provided. No data will be collected from remote targets")
}

c.Log.Info("Start collection of Icinga 2 API endpoints")

// return if username or password is not provided
Expand All @@ -43,6 +45,7 @@ func InitAPICollection(c *collection.Collection) error {
c.Log.Warn(err)
continue
}

c.Log.Debugf("Endpoint '%s' is reachable", endpoint)

// collect /v1/status from endpoint
Expand Down Expand Up @@ -79,8 +82,12 @@ func collectStatus(endpoint string, c *collection.Collection) error {
}
client := &http.Client{Transport: tr}

// build context for request
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// build request
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/v1/status", endpoint), nil)
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://%s/v1/status", endpoint), nil)

Check failure on line 90 in modules/icinga2/api.go

View workflow job for this annotation

GitHub Actions / lint

"GET" can be replaced by http.MethodGet (usestdlibvars)
if err != nil {
return fmt.Errorf("cant build new request for '%s': %w", endpoint, err)
}
Expand All @@ -93,6 +100,7 @@ func collectStatus(endpoint string, c *collection.Collection) error {
if err != nil {
return fmt.Errorf("cant requests status from '%s': %w", endpoint, err)
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
Expand Down

0 comments on commit 95bc293

Please sign in to comment.