Skip to content

Commit

Permalink
Tidy up responses for POST requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Jun 7, 2024
1 parent 000ee97 commit af90f05
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
10 changes: 0 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,10 @@ linters:
# https://golangci-lint.run/usage/linters/#disabled-by-default
disable:
- cyclop
- deadcode
- depguard
- dupl
- execinquery
- exhaustive
- exhaustivestruct
- exhaustruct
- forcetypeassert
- funlen
Expand All @@ -172,21 +170,13 @@ linters:
- gocognit
- goconst
- err113
- golint
- gomnd
- ifshort
- interfacer
- ireturn
- lll
- maintidx
- maligned
- mnd
- musttag
- nosnakecase
- perfsprint
- scopelint
- structcheck
- varcheck
- varnamelen
- wrapcheck
- wsl
26 changes: 23 additions & 3 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const defaultUserAgent = "go-eth2-client/0.21.4"

// post sends an HTTP post request and returns the body.
func (s *Service) post(ctx context.Context, endpoint string, body io.Reader) (io.Reader, error) {
ctx, span := otel.Tracer("attestantio.go-eth2-client.http").Start(ctx, "post")
defer span.End()

// #nosec G404
log := s.log.With().Str("id", fmt.Sprintf("%02x", rand.Int31())).Str("address", s.address).Str("endpoint", endpoint).Logger()
if e := log.Trace(); e.Enabled() {
Expand All @@ -51,13 +54,18 @@ func (s *Service) post(ctx context.Context, endpoint string, body io.Reader) (io
}

callURL := urlForCall(s.base, endpoint, "")
log.Trace().Str("url", callURL.String()).Msg("URL to POST")
span.SetAttributes(attribute.String("url", callURL.String()))

opCtx, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
req, err := http.NewRequestWithContext(opCtx, http.MethodPost, callURL.String(), body)
if err != nil {
span.SetStatus(codes.Error, "Failed to create request")

return nil, errors.Join(errors.New("failed to create POST request"), err)
}

s.addExtraHeaders(req)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
Expand All @@ -68,21 +76,29 @@ func (s *Service) post(ctx context.Context, endpoint string, body io.Reader) (io
resp, err := s.client.Do(req)
if err != nil {
go s.CheckConnectionState(ctx)

span.SetStatus(codes.Error, err.Error())
s.monitorPostComplete(ctx, callURL.Path, "failed")

return nil, errors.Join(errors.New("failed to call POST endpoint"), err)
}
defer resp.Body.Close()
log = log.With().Int("status_code", resp.StatusCode).Logger()

data, err := io.ReadAll(resp.Body)
if err != nil {
span.SetStatus(codes.Error, err.Error())
s.monitorPostComplete(ctx, callURL.Path, "failed")

return nil, errors.Join(errors.New("failed to read POST response"), err)
}

statusFamily := resp.StatusCode / 100
if statusFamily != 2 {
log.Debug().Int("status_code", resp.StatusCode).Str("data", string(data)).Msg("POST failed")
trimmedResponse := bytes.ReplaceAll(bytes.ReplaceAll(data, []byte{0x0a}, []byte{}), []byte{0x0d}, []byte{})
log.Debug().Int("status_code", resp.StatusCode).RawJSON("response", trimmedResponse).Msg("POST failed")

span.SetStatus(codes.Error, fmt.Sprintf("Status code %d", resp.StatusCode))
s.monitorPostComplete(ctx, callURL.Path, "failed")

return nil, &api.Error{
Expand Down Expand Up @@ -225,8 +241,12 @@ func (s *Service) post2(ctx context.Context,

statusFamily := resp.StatusCode / 100
if statusFamily != 2 {
trimmedResponse := bytes.ReplaceAll(bytes.ReplaceAll(res.body, []byte{0x0a}, []byte{}), []byte{0x0d}, []byte{})
log.Debug().Int("status_code", resp.StatusCode).RawJSON("response", trimmedResponse).Msg("POST failed")
if res.contentType == ContentTypeJSON {
trimmedResponse := bytes.ReplaceAll(bytes.ReplaceAll(res.body, []byte{0x0a}, []byte{}), []byte{0x0d}, []byte{})
log.Debug().Int("status_code", resp.StatusCode).RawJSON("response", trimmedResponse).Msg("POST failed")
} else {
log.Debug().Int("status_code", resp.StatusCode).Msg("POST failed")
}

span.SetStatus(codes.Error, fmt.Sprintf("Status code %d", resp.StatusCode))
s.monitorPostComplete(ctx, callURL.Path, "failed")
Expand Down

0 comments on commit af90f05

Please sign in to comment.