mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-20 07:19:17 +01:00
lib/promscrape: set 30 seconds timeout for discovery api requests
Previously such requests could hang for long time. This could make debugging harder.
This commit is contained in:
parent
b6d88bac04
commit
de5f923476
@ -10,6 +10,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
||||
)
|
||||
|
||||
type apiConfig struct {
|
||||
@ -142,6 +144,8 @@ type IdentityDocument struct {
|
||||
func getMetadataByPath(apiPath string) ([]byte, error) {
|
||||
// See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html
|
||||
|
||||
client := discoveryutils.GetHTTPClient()
|
||||
|
||||
// Obtain session token
|
||||
sessionTokenURL := "http://169.254.169.254/latest/api/token"
|
||||
req, err := http.NewRequest("PUT", sessionTokenURL, nil)
|
||||
@ -149,7 +153,7 @@ func getMetadataByPath(apiPath string) ([]byte, error) {
|
||||
return nil, fmt.Errorf("cannot create request for IMDSv2 session token at url %q: %s", sessionTokenURL, err)
|
||||
}
|
||||
req.Header.Set("X-aws-ec2-metadata-token-ttl-seconds", "60")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot obtain IMDSv2 session token from %q; probably, `region` is missing in `ec2_sd_config`; error: %s", sessionTokenURL, err)
|
||||
}
|
||||
@ -165,7 +169,7 @@ func getMetadataByPath(apiPath string) ([]byte, error) {
|
||||
return nil, fmt.Errorf("cannot create request to %q: %s", apiURL, err)
|
||||
}
|
||||
req.Header.Set("X-aws-ec2-metadata-token", string(token))
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
resp, err = client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot obtain response for %q: %s", apiURL, err)
|
||||
}
|
||||
@ -197,7 +201,7 @@ func getAPIResponse(cfg *apiConfig, action, nextPageToken string) ([]byte, error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create signed request: %s", err)
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := discoveryutils.GetHTTPClient().Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot perform http request to %q: %s", apiURL, err)
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
||||
"golang.org/x/oauth2/google"
|
||||
)
|
||||
|
||||
@ -187,7 +188,7 @@ func getGCEMetadata(path string) ([]byte, error) {
|
||||
return nil, fmt.Errorf("cannot create http request for %q: %s", metadataURL, err)
|
||||
}
|
||||
req.Header.Set("Metadata-Flavor", "Google")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := discoveryutils.GetHTTPClient().Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot obtain response to %q: %s", metadataURL, err)
|
||||
}
|
||||
|
15
lib/promscrape/discoveryutils/client.go
Normal file
15
lib/promscrape/discoveryutils/client.go
Normal file
@ -0,0 +1,15 @@
|
||||
package discoveryutils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var defaultClient = &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
// GetHTTPClient returns default client for http API requests.
|
||||
func GetHTTPClient() *http.Client {
|
||||
return defaultClient
|
||||
}
|
Loading…
Reference in New Issue
Block a user