Fix SignalFx access token bug
Fixing bug with fetching access tokens: Use new request object for every fetch and pass the url params
@@ -261,6 +261,7 @@ func (sfx *SignalFxSink) clientByTagUpdater() {
sfx.clientsByTagValue[name] = NewClient(sfx.metricsEndpoint, token, sfx.httpClient)
sfx.clientsByTagValueMu.Unlock()
}
+ sfx.log.Debugf("Fetched %d tokens in total", len(tokens))
}
}
@@ -272,7 +273,7 @@ const (
limitQueryValue = 200
)
-func fetchAPIKeys(client *http.Client, endpoint, apiToken string) (map[string]string, error) {
+func getTokensApiResponseFromOffset(client *http.Client, endpoint, apiToken string, offset int) (*bytes.Buffer, error) {
b := &bytes.Buffer{}
req, err := http.NewRequest(http.MethodGet, endpoint, bytes.NewReader(b.Bytes()))
@@ -283,24 +284,22 @@ func fetchAPIKeys(client *http.Client, endpoint, apiToken string) (map[string]st
req.Header.Set(sfxclient.TokenHeaderName, apiToken)
req.Header.Set("Content-Type", "application/json")
- req.URL.Query().Add(limitQueryParam, fmt.Sprint(limitQueryValue))
- req.URL.Query().Add(nameQueryParam, "")
-
- allFetched := false
-
- offset := 0
+ q := req.URL.Query()
+ q.Add(limitQueryParam, fmt.Sprint(limitQueryValue))
+ q.Add(nameQueryParam, "")
- apiTokensByName := make(map[string]string)
+ q.Del(offsetQueryParam)
+ q.Add(offsetQueryParam, fmt.Sprint(offset))
- for !allFetched {
- req.URL.Query().Del(offsetQueryParam)
- req.URL.Query().Add(offsetQueryParam, fmt.Sprint(offset))
+ req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
- return apiTokensByName, err
+ return nil, err
}
+ defer resp.Body.Close()
+
// The API always returns OK, even if it doesn't have any tokens to return
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("signalfx api returned unknown response code: %s", resp.Status)
@@ -312,7 +311,20 @@ func fetchAPIKeys(client *http.Client, endpoint, apiToken string) (map[string]st
if err != nil {
return nil, err
}
+ return body, nil
+}
+func fetchAPIKeys(client *http.Client, endpoint, apiToken string) (map[string]string, error) {
+ allFetched := false
+ offset := 0
+
+ apiTokensByName := make(map[string]string)
+
+ for !allFetched {
+ body, err := getTokensApiResponseFromOffset(client, endpoint, apiToken, offset)
+ if err != nil {
+ return nil, err
+ }
count, err := extractTokensFromResponse(apiTokensByName, body)
if err != nil {
return nil, err
