Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#474 from TaoZou1/httpdebug
Browse files Browse the repository at this point in the history
Add http request, response debug log
  • Loading branch information
TaoZou1 authored Jan 9, 2024
2 parents f5902cc + 1c07b2d commit 6e6a1c6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 0 additions & 1 deletion pkg/nsx/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ func (ep *Endpoint) UpdateHttpRequestAuth(request *http.Request) error {
} else {
xsrfToken := ep.XSRFToken()
if len(xsrfToken) > 0 {
log.V(2).Info("update cookie")
if request.Header.Get("Authorization") != "" {
request.Header.Del("Authorization")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/nsx/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package nsx
import (
"bytes"
"errors"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -46,6 +46,7 @@ func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
ep.UpdateHttpRequestAuth(r)
start := time.Now()
ep.wait()
util.DumpHttpRequest(r)
waitTime := time.Since(start)
if resp, resul = t.base().RoundTrip(r); resul != nil {
ep.setStatus(DOWN)
Expand All @@ -57,10 +58,9 @@ func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
if resp == nil {
return nil
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
resp.Body = ioutil.NopCloser(bytes.NewReader(body))
resp.Body = io.NopCloser(bytes.NewReader(body))

if err != nil {
log.Error(err, "failed to extract HTTP body")
Expand Down
19 changes: 18 additions & 1 deletion pkg/nsx/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package util

import (
"bytes"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
Expand Down Expand Up @@ -129,6 +130,7 @@ func InitErrorFromResponse(host string, statusCode int, body []byte) NsxError {
}

func extractHTTPDetailFromBody(host string, statusCode int, body []byte) (ErrorDetail, error) {
log.V(2).Info("http response", "status code", statusCode, "body", string(body))
ec := ErrorDetail{StatusCode: statusCode}
if len(body) == 0 {
log.V(1).Info("body length is 0")
Expand All @@ -141,7 +143,6 @@ func extractHTTPDetailFromBody(host string, statusCode int, body []byte) (ErrorD
}

ec.ErrorCode = res.ErrorCode
log.V(2).Info("http response", "status code", statusCode, "body", res)
msg := []string{res.ErrorMsg}
for _, a := range res.RelatedErr {
ec.RelatedErrorCodes = append(ec.RelatedErrorCodes, a.ErrorCode)
Expand Down Expand Up @@ -289,6 +290,22 @@ func ParseVPCPath(nsxResourcePath string) (orgID string, projectID string, vpcID
resourceID = paras[8]
return
}
func DumpHttpRequest(request *http.Request) {
var body []byte
var err error
if request.Body == nil {
return
}
if request != nil {
body, err = io.ReadAll(request.Body)
if err != nil {
return
}
}
request.Body.Close()
request.Body = io.NopCloser(bytes.NewReader(body))
log.V(2).Info("http request", "url", request.URL, "body", string(body))
}

// if ApiError is nil, check ErrorTypeEnum, such as ServiceUnavailable
// if both return value are nil, the error is not on the list
Expand Down

0 comments on commit 6e6a1c6

Please sign in to comment.