From 12ec8631b106265e54d4f326bddcc17cf4b8efaa Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Mon, 22 May 2023 09:13:52 -0500 Subject: [PATCH] Stop logging all ES requests in debug mode (only log errors) --- utils/http.go | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/utils/http.go b/utils/http.go index 152f499..4fc6d0d 100644 --- a/utils/http.go +++ b/utils/http.go @@ -50,43 +50,39 @@ func shouldRetry(request *http.Request, response *http.Response, withDelay time. } // MakeJSONRequest is a utility function to make a JSON request, optionally decoding the response into the passed in struct -func MakeJSONRequest(method string, url string, body []byte, jsonStruct interface{}) (*http.Response, error) { +func MakeJSONRequest(method string, url string, body []byte, dest any) (*http.Response, error) { + l := log.WithField("url", url).WithField("method", method) + req, _ := httpx.NewRequest(method, url, bytes.NewReader(body), map[string]string{"Content-Type": "application/json"}) resp, err := httpx.Do(http.DefaultClient, req, retryConfig, nil) - - l := log.WithField("url", url).WithField("method", method).WithField("request", body) if err != nil { - l.WithError(err).Error("error making ES request") + l.WithError(err).Error("error making request") return resp, err } defer resp.Body.Close() // if we have a body, try to decode it - jsonBody, err := ioutil.ReadAll(resp.Body) + respBody, err := ioutil.ReadAll(resp.Body) if err != nil { - l.WithError(err).Error("error reading ES response") + l.WithError(err).Error("error reading response") return resp, err } - l = l.WithField("response", string(jsonBody)).WithField("status", resp.StatusCode) + l = l.WithField("response", string(respBody)).WithField("status", resp.StatusCode) // error if we got a non-200 if resp.StatusCode != http.StatusOK { l.WithError(err).Error("error reaching ES") - return resp, fmt.Errorf("received non 200 response %d: %s", resp.StatusCode, jsonBody) - } - - if jsonStruct == nil { - l.Debug("ES request successful") - return resp, nil + return resp, fmt.Errorf("received non-200 response %d: %s", resp.StatusCode, respBody) } - err = json.Unmarshal(jsonBody, jsonStruct) - if err != nil { - l.WithError(err).Error("error unmarshalling ES response") - return resp, err + if dest != nil { + err = json.Unmarshal(respBody, dest) + if err != nil { + l.WithError(err).Error("error unmarshalling response") + return resp, err + } } - l.Debug("ES request successful") return resp, nil }