diff --git a/client.go b/client.go index fece3c1..7ff02ca 100644 --- a/client.go +++ b/client.go @@ -3,13 +3,16 @@ package ranger import ( "bytes" "context" + "encoding/json" "io/ioutil" "net/http" + "strconv" "github.com/cockroachdb/errors" "github.com/rs/zerolog/log" "go.mondoo.com/ranger-rpc/status" "google.golang.org/protobuf/proto" + "moul.io/http2curl" ) // Client is the client for the Ranger service. It is used as the base client for all service calls. @@ -45,6 +48,11 @@ func (c *Client) DoClientRequest(ctx context.Context, client HTTPClient, url str req = req.WithContext(ctx) req.Header = header + // trace curl request + if log.Trace().Enabled() { + c.PrintTraceCurlCommand(url, in) + } + // do http call resp, err := client.Do(req) if err != nil { @@ -90,3 +98,28 @@ func (c *Client) DoClientRequest(ctx context.Context, client HTTPClient, url str } return nil } + +func (c *Client) PrintTraceCurlCommand(url string, in proto.Message) { + // for better debuggability we try to construct an equivalent json request + jsonBytes, err := json.Marshal(in) + if err != nil { + log.Error().Err(err).Msg("could not generate trace http log") + } + + header := make(http.Header) + header.Set("Accept", "application/json") + header.Set("Content-Type", "application/json") + header.Set("Content-Length", strconv.Itoa(len(jsonBytes))) + + // create http request + reader := bytes.NewReader(jsonBytes) + req, err := http.NewRequest("POST", url, reader) + if err != nil { + return + } + req.Header = header + + // convert request to curl command + command, _ := http2curl.GetCurlCommand(req) + log.Trace().Msg(command.String()) +} diff --git a/examples/pingpong/client/main.go b/examples/pingpong/client/main.go index c0c7a4c..3bef3bd 100644 --- a/examples/pingpong/client/main.go +++ b/examples/pingpong/client/main.go @@ -6,10 +6,12 @@ import ( "net/http" "time" + "github.com/rs/zerolog" "go.mondoo.com/ranger-rpc/examples/pingpong" ) func main() { + zerolog.SetGlobalLevel(zerolog.TraceLevel) fmt.Println("start pingpong example client") client, err := pingpong.NewPingPongClient("http://localhost:2155/api/", &http.Client{ Timeout: time.Second * 10, diff --git a/go.mod b/go.mod index 1018572..c644aac 100644 --- a/go.mod +++ b/go.mod @@ -37,4 +37,5 @@ require ( golang.org/x/text v0.3.7 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + moul.io/http2curl v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index 5583bfd..bf6abef 100644 --- a/go.sum +++ b/go.sum @@ -738,6 +738,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= +moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8= +moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=