mirror of
https://github.com/kyverno/policy-reporter.git
synced 2024-12-14 11:57:32 +00:00
feat: add debug http logging (#346)
Add optional debug logging for dumping http requests and responses. Signed-off-by: Blake Pettersson <blake.pettersson@gmail.com>
This commit is contained in:
parent
fbfb7b1a11
commit
5e8275ded6
2 changed files with 40 additions and 3 deletions
37
pkg/target/http/logroundtripper.go
Normal file
37
pkg/target/http/logroundtripper.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
)
|
||||
|
||||
func NewLoggingRoundTripper(roundTripper http.RoundTripper) http.RoundTripper {
|
||||
return &logRoundTripper{roundTripper: roundTripper}
|
||||
}
|
||||
|
||||
type logRoundTripper struct {
|
||||
roundTripper http.RoundTripper
|
||||
}
|
||||
|
||||
func (rt *logRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
logger := zap.L()
|
||||
if logger.Core().Enabled(zap.DebugLevel) {
|
||||
if info, err := httputil.DumpRequest(req, true); err == nil {
|
||||
logger.Debug(fmt.Sprintf("Sending request: %s", string(info)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
resp, err := rt.roundTripper.RoundTrip(req)
|
||||
if resp != nil {
|
||||
if logger.Core().Enabled(zap.DebugLevel) {
|
||||
if info, err := httputil.DumpResponse(resp, true); err == nil {
|
||||
logger.Debug(fmt.Sprintf("Received response: %s", string(info)))
|
||||
}
|
||||
}
|
||||
}
|
||||
return resp, err
|
||||
}
|
|
@ -5,8 +5,8 @@ import (
|
|||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
@ -86,11 +86,11 @@ func NewClient(certificatePath string, skipTLS bool) *http.Client {
|
|||
}
|
||||
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
Transport: NewLoggingRoundTripper(transport),
|
||||
}
|
||||
|
||||
if certificatePath != "" {
|
||||
caCert, err := ioutil.ReadFile(certificatePath)
|
||||
caCert, err := os.ReadFile(certificatePath)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to read certificate", zap.String("path", certificatePath))
|
||||
return client
|
||||
|
|
Loading…
Reference in a new issue