diff --git a/.golangci.yml b/.golangci.yml index 6a4b4585..9356cb7d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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 @@ -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 diff --git a/http/http.go b/http/http.go index 8f30d990..99d2a07b 100644 --- a/http/http.go +++ b/http/http.go @@ -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() { @@ -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") @@ -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{ @@ -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")