From 7c6ba83d6c97489f3aaaccea6fccadfacaa8a882 Mon Sep 17 00:00:00 2001 From: alvarof2 Date: Fri, 19 Jan 2024 12:29:41 +0100 Subject: [PATCH] Timeout and retries --- README.md | 2 ++ go.mod | 3 +++ go.sum | 6 ++++++ main.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e22ad72..7f41488 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ socket_score{package="@celo/0x-contracts",score="vulnerability",version="2.1.2-0 - `API_TOKEN` (REQUIRED): A [Socket.dev](https://socket.dev/) API token. - `LOG_LEVEL`: The [Logrus](https://github.com/sirupsen/logrus) log level. If not set, defaults to `info`. - `PERIOD`: The period to refresh the [Socket.dev](https://socket.dev/) scores, in hours. If not set, defaults to `24`. +- `TIMEOUT`: The timeout for requests to [Socket.dev](https://socket.dev/), in seconds. If not set, defaults to `15`. +- `RETRIES`: The number of retries for requests to [Socket.dev](https://socket.dev/). If not set, defaults to `5`. ## Tests diff --git a/go.mod b/go.mod index 47cd7aa..b855513 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,13 @@ module socket-dev-exporter go 1.19 +require github.com/hashicorp/go-retryablehttp v0.7.5 + require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/joho/godotenv v1.5.1 // indirect github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect github.com/prometheus/client_golang v1.18.0 // indirect diff --git a/go.sum b/go.sum index 2aeb37a..a07be01 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= +github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= +github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= +github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M= +github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= @@ -23,6 +28,7 @@ github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3c github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= diff --git a/main.go b/main.go index 641f629..5a780a3 100644 --- a/main.go +++ b/main.go @@ -4,25 +4,27 @@ import ( "encoding/base64" "encoding/json" "fmt" + "io/ioutil" "log" "net/http" "os" "strconv" "time" + "github.com/hashicorp/go-retryablehttp" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/sirupsen/logrus" ) var initializing bool = true -var client = http.Client{ - Timeout: 5 * time.Second, -} + +var retries int +var timeout time.Duration + var exportedMetrics = []map[string]interface{}{} var token string -var periodEnvVar string var period time.Duration var port = 9101 @@ -128,6 +130,20 @@ func (collector *socketCollector) Collect(ch chan<- prometheus.Metric) { func updateMetrics() { var npmResponse NpmResponse + retryClient := retryablehttp.NewClient() + retryClient.RetryMax = retries + retryClient.Logger = log.New(ioutil.Discard, "", log.LstdFlags) + + retryClient.RequestLogHook = func(_ retryablehttp.Logger, req *http.Request, attempt int) { + logrus.WithFields(logrus.Fields{ + "host": req.URL.Host, + "path": req.URL.Path, + "attempt": attempt, + }).Info("Sending request") + } + + client := retryClient.StandardClient() // *http.Client + client.Timeout = timeout logrus.Info("Sending request to registry.npmjs.org") res, err := client.Get("https://registry.npmjs.org/-/v1/search?text=scope:celo&size=100") @@ -222,7 +238,7 @@ func main() { os.Exit(1) } - periodEnvVar, ok = os.LookupEnv("PERIOD") + periodEnvVar, ok := os.LookupEnv("PERIOD") if !ok { logrus.Error("Could not read env. var. PERIOD. Setting it to 24 hours.") period, err = time.ParseDuration("24h") @@ -234,6 +250,31 @@ func main() { } } + retriesEnvVar, ok := os.LookupEnv("RETRIES") + if !ok { + logrus.Error("Could not read env. var. RETRIES. Setting it to 5.") + retries = 5 + } else { + retries, err = strconv.Atoi(retriesEnvVar) + if err != nil { + logrus.Error(fmt.Sprintf("Could not parse RETRIES env. var. to int: %s", err)) + os.Exit(1) + } + } + + timeoutEnvVar, ok := os.LookupEnv("TIMEOUT") + if !ok { + logrus.Error("Could not read env. var. TIMEOUT. Setting it to 15 seconds.") + timeout = 15 * time.Second + } else { + timeoutInt, err := strconv.Atoi(timeoutEnvVar) + if err != nil { + logrus.Error(fmt.Sprintf("Could not parse TIMEOUT env. var. to int: %s", err)) + os.Exit(1) + } + timeout = time.Duration(timeoutInt) * time.Second + } + socketCollector := newSocketCollector() prometheus.MustRegister(socketCollector) http.Handle("/metrics", promhttp.Handler())