use stakejoy api by default, add timeout
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
8e7700e81c
commit
794285ac3a
10
exporter.go
10
exporter.go
|
@ -824,7 +824,10 @@ func (e *Exporter) collectHotspotRewardsMetrics(wg *sync.WaitGroup, ch chan<- pr
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fApiUrl := flag.String("apiUrl", "https://api.helium.io", "The helium api url")
|
var err error
|
||||||
|
|
||||||
|
fApiUrl := flag.String("apiUrl", "https://helium-api.stakejoy.com", "The helium api url")
|
||||||
|
fApiTimeout := flag.String("apiTimeout", "10s", "The request timeout to the helium api.")
|
||||||
fHeliumAccounts := flag.String("accounts", "", "A comma-delimited list of helium accounts to scrape (optional)")
|
fHeliumAccounts := flag.String("accounts", "", "A comma-delimited list of helium accounts to scrape (optional)")
|
||||||
fMetricsPath := flag.String("metricpath", "/metrics", "The metrics path")
|
fMetricsPath := flag.String("metricpath", "/metrics", "The metrics path")
|
||||||
fListenAddress := flag.String("listenAddress", "0.0.0.0", "The http server listen address")
|
fListenAddress := flag.String("listenAddress", "0.0.0.0", "The http server listen address")
|
||||||
|
@ -832,6 +835,11 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
heliumapi.ApiUrl = *fApiUrl
|
heliumapi.ApiUrl = *fApiUrl
|
||||||
|
heliumapi.ApiTimeout, err = time.ParseDuration(*fApiTimeout)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to parse apitTimeout: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
heliumAccounts := strings.Split(*fHeliumAccounts, ",")
|
heliumAccounts := strings.Split(*fHeliumAccounts, ",")
|
||||||
serverAddr := *fListenAddress + ":" + *fListenPort
|
serverAddr := *fListenAddress + ":" + *fListenPort
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,24 @@
|
||||||
package heliumapi
|
package heliumapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ApiUrl = "https://api.helium.io"
|
ApiUrl = "https://api.helium.io"
|
||||||
|
ApiTimeout = time.Duration(10 * time.Second)
|
||||||
client = &http.Client{}
|
client = &http.Client{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// createGetRequest create a GET request to the helium api
|
// createGetRequest create a GET request to the helium api
|
||||||
func createGetRequest(path string, params map[string]string) (*http.Request, error) {
|
func createGetRequest(path string, params map[string]string) (*http.Request, *context.CancelFunc, error) {
|
||||||
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", ApiUrl, path), nil)
|
ctx, ctxCancel := context.WithTimeout(context.Background(), ApiTimeout)
|
||||||
|
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s%s", ApiUrl, path), nil)
|
||||||
|
|
||||||
// setup headers
|
// setup headers
|
||||||
req.Header.Add("Accept", "application/json")
|
req.Header.Add("Accept", "application/json")
|
||||||
|
@ -26,12 +30,13 @@ func createGetRequest(path string, params map[string]string) (*http.Request, err
|
||||||
query.Add(k, v)
|
query.Add(k, v)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
defer ctxCancel()
|
||||||
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
req.URL.RawQuery = query.Encode()
|
req.URL.RawQuery = query.Encode()
|
||||||
}
|
}
|
||||||
|
|
||||||
return req, err
|
return req, &ctxCancel, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// getHeliumApi query a regular helium api endpoint
|
// getHeliumApi query a regular helium api endpoint
|
||||||
|
@ -41,10 +46,11 @@ func getHeliumApi(path string, params *map[string]string) ([]byte, error) {
|
||||||
params = &map[string]string{}
|
params = &map[string]string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := createGetRequest(path, *params)
|
req, ctxCancel, err := createGetRequest(path, *params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create query request %v: %v", path, err)
|
return nil, fmt.Errorf("failed to create query request %v: %v", path, err)
|
||||||
}
|
}
|
||||||
|
defer (*ctxCancel)()
|
||||||
|
|
||||||
// query the api
|
// query the api
|
||||||
// log.Printf("querying %v", req.URL.String())
|
// log.Printf("querying %v", req.URL.String())
|
||||||
|
@ -83,10 +89,11 @@ func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, e
|
||||||
|
|
||||||
res := [][]byte{}
|
res := [][]byte{}
|
||||||
respCursor := ResponseWithCursor{}
|
respCursor := ResponseWithCursor{}
|
||||||
req, err := createGetRequest(path, *params)
|
req, ctxCancel, err := createGetRequest(path, *params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create query request %v: %v", path, err)
|
return nil, fmt.Errorf("failed to create query request %v: %v", path, err)
|
||||||
}
|
}
|
||||||
|
defer (*ctxCancel)()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// query the api
|
// query the api
|
||||||
|
@ -119,10 +126,11 @@ func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, e
|
||||||
// continue querying until there is no longer a cursor
|
// continue querying until there is no longer a cursor
|
||||||
if respCursor.Cursor != "" {
|
if respCursor.Cursor != "" {
|
||||||
params = &map[string]string{"cursor": respCursor.Cursor}
|
params = &map[string]string{"cursor": respCursor.Cursor}
|
||||||
req, err = createGetRequest(path, *params)
|
req, ctxCancel, err = createGetRequest(path, *params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create query request %v: %v", req.URL.String(), err)
|
return nil, fmt.Errorf("failed to create query request %v: %v", req.URL.String(), err)
|
||||||
}
|
}
|
||||||
|
defer (*ctxCancel)()
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue