From 082778c4e416834ab126e2e3171ee1cdb89d82d1 Mon Sep 17 00:00:00 2001 From: Himanshu Kaushik Date: Mon, 4 Dec 2023 14:34:51 +0530 Subject: [PATCH 1/4] SUMO-228138: Adding ErrorHandler in retryble client to handle http error codes. --- sumologic/sumologic_client.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sumologic/sumologic_client.go b/sumologic/sumologic_client.go index 5e30c607..00244edd 100644 --- a/sumologic/sumologic_client.go +++ b/sumologic/sumologic_client.go @@ -10,6 +10,7 @@ import ( "log" "net/http" "net/url" + "strconv" "time" "github.com/hashicorp/go-retryablehttp" @@ -63,7 +64,7 @@ func createNewRequest(method, url string, body io.Reader, accessID string, acces func logRequestAndResponse(req *http.Request, resp *http.Response) { var maskedHeader = req.Header.Clone() maskedHeader.Set("Authorization", "xxxxxxxxxxx") - log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [StatusCode=%s]\n", req.Method, req.URL, maskedHeader, resp.Status) + log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [Status=%s] [Number of Retries=%s]\n", req.Method, req.URL, maskedHeader, resp.Status, resp.Header.Get("numberOfRetries")) } func (s *Client) PostWithCookies(urlPath string, payload interface{}) ([]byte, []*http.Cookie, error) { @@ -328,12 +329,18 @@ func (s *Client) Delete(urlPath string) ([]byte, error) { return d, nil } +func ErrorHandler(resp *http.Response, err error, numTries int) (*http.Response, error) { + log.Printf("[DEBUG] Resquest Failed after %d number of retries with Response: [%s]", numTries, resp.Status) + resp.Header.Add("numberOfRetries", strconv.Itoa(numTries)) + return resp, err +} + func NewClient(accessID, accessKey, authJwt, environment, base_url string, admin bool) (*Client, error) { retryClient := retryablehttp.NewClient() retryClient.RetryMax = 10 // Disable DEBUG logs (https://github.com/hashicorp/go-retryablehttp/issues/31) retryClient.Logger = nil - + retryClient.ErrorHandler = ErrorHandler client := Client{ AccessID: accessID, AccessKey: accessKey, From 0f60d99a6a455d4221ca03ee5f8d71f3a38155b1 Mon Sep 17 00:00:00 2001 From: Marcin Flis Date: Mon, 4 Dec 2023 11:59:44 +0100 Subject: [PATCH 2/4] SUMO-228138: Reformat error message after failed request --- sumologic/sumologic_client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sumologic/sumologic_client.go b/sumologic/sumologic_client.go index 00244edd..3dda93f5 100644 --- a/sumologic/sumologic_client.go +++ b/sumologic/sumologic_client.go @@ -330,7 +330,7 @@ func (s *Client) Delete(urlPath string) ([]byte, error) { } func ErrorHandler(resp *http.Response, err error, numTries int) (*http.Response, error) { - log.Printf("[DEBUG] Resquest Failed after %d number of retries with Response: [%s]", numTries, resp.Status) + log.Printf("[ERROR] Request %s failed after %d attempts with response: [%s]", resp.Request.URL, numTries, resp.Status) resp.Header.Add("numberOfRetries", strconv.Itoa(numTries)) return resp, err } From 92523afbdb8c11fea9bade23030b0cfba2b8fe4f Mon Sep 17 00:00:00 2001 From: Marcin Flis Date: Mon, 4 Dec 2023 12:02:56 +0100 Subject: [PATCH 3/4] SUMO-228138: fix log levels Previosly all provider logs were prefixed with DEBUG, so logs were only shown after setting TF_LOG_PROVIDER=DEBUG, despite having ERROR level Ref: https://developer.hashicorp.com/terraform/plugin/log/writing#duplicate-timestamp-and-incorrect-level-messages --- main.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main.go b/main.go index 98207003..1b4fbddf 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,17 @@ package main import ( "github.com/SumoLogic/terraform-provider-sumologic/sumologic" "github.com/hashicorp/terraform-plugin-sdk/plugin" + "log" ) var version string // provider version is passed as compile time argument var defaultVersion = "dev" func main() { + // Remove any date and time prefix in log package function output to + // prevent duplicate timestamp and incorrect log level setting + // See: https://developer.hashicorp.com/terraform/plugin/log/writing#duplicate-timestamp-and-incorrect-level-messages + log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime)) if version == "" { sumologic.ProviderVersion = defaultVersion } else { From 25614398e3e67af27c94a76902c4fa9cefe1612f Mon Sep 17 00:00:00 2001 From: Himanshu Kaushik Date: Mon, 4 Dec 2023 17:41:29 +0530 Subject: [PATCH 4/4] SUMO-228138: Removing number of retries from response header. --- sumologic/sumologic_client.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sumologic/sumologic_client.go b/sumologic/sumologic_client.go index 3dda93f5..de0aa935 100644 --- a/sumologic/sumologic_client.go +++ b/sumologic/sumologic_client.go @@ -10,7 +10,6 @@ import ( "log" "net/http" "net/url" - "strconv" "time" "github.com/hashicorp/go-retryablehttp" @@ -64,7 +63,7 @@ func createNewRequest(method, url string, body io.Reader, accessID string, acces func logRequestAndResponse(req *http.Request, resp *http.Response) { var maskedHeader = req.Header.Clone() maskedHeader.Set("Authorization", "xxxxxxxxxxx") - log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [Status=%s] [Number of Retries=%s]\n", req.Method, req.URL, maskedHeader, resp.Status, resp.Header.Get("numberOfRetries")) + log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [Status=%s]\n", req.Method, req.URL, maskedHeader, resp.Status) } func (s *Client) PostWithCookies(urlPath string, payload interface{}) ([]byte, []*http.Cookie, error) { @@ -331,7 +330,6 @@ func (s *Client) Delete(urlPath string) ([]byte, error) { func ErrorHandler(resp *http.Response, err error, numTries int) (*http.Response, error) { log.Printf("[ERROR] Request %s failed after %d attempts with response: [%s]", resp.Request.URL, numTries, resp.Status) - resp.Header.Add("numberOfRetries", strconv.Itoa(numTries)) return resp, err }