Skip to content

Commit

Permalink
Replace more logrus use by slog
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Oct 13, 2023
1 parent 0f1e603 commit f4ed394
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"time"

"github.com/nyaruka/gocommon/httpx"
log "github.com/sirupsen/logrus"
)

var retryConfig *httpx.RetryConfig
Expand Down Expand Up @@ -41,7 +41,7 @@ func shouldRetry(request *http.Request, response *http.Response, withDelay time.
bodyBytes, err := io.ReadAll(response.Body)
response.Body.Close()
if err != nil {
log.WithError(err).Error("error reading ES response, retrying")
slog.Error("error reading ES response, retrying", "error", err)
return true
}

Expand All @@ -51,35 +51,35 @@ 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, dest any) (*http.Response, error) {
l := log.WithField("url", url).WithField("method", method)
l := slog.With("url", url, "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)
if err != nil {
l.WithError(err).Error("error making request")
l.Error("error making request", "error", err)

Check warning on line 59 in utils/http.go

View check run for this annotation

Codecov / codecov/patch

utils/http.go#L59

Added line #L59 was not covered by tests
return resp, err
}
defer resp.Body.Close()

// if we have a body, try to decode it
respBody, err := io.ReadAll(resp.Body)
if err != nil {
l.WithError(err).Error("error reading response")
l.Error("error reading response", "error", err)

Check warning on line 67 in utils/http.go

View check run for this annotation

Codecov / codecov/patch

utils/http.go#L67

Added line #L67 was not covered by tests
return resp, err
}

l = l.WithField("response", string(respBody)).WithField("status", resp.StatusCode)
l = l.With("response", string(respBody), "status", resp.StatusCode)

// error if we got a non-200
if resp.StatusCode != http.StatusOK {
l.WithError(err).Error("error reaching ES")
l.Error("error reaching ES", "error", err)

Check warning on line 75 in utils/http.go

View check run for this annotation

Codecov / codecov/patch

utils/http.go#L75

Added line #L75 was not covered by tests
return resp, fmt.Errorf("received non-200 response %d: %s", resp.StatusCode, respBody)
}

if dest != nil {
err = json.Unmarshal(respBody, dest)
if err != nil {
l.WithError(err).Error("error unmarshalling response")
l.Error("error unmarshalling response", "error", err)

Check warning on line 82 in utils/http.go

View check run for this annotation

Codecov / codecov/patch

utils/http.go#L82

Added line #L82 was not covered by tests
return resp, err
}
}
Expand Down

0 comments on commit f4ed394

Please sign in to comment.