Skip to content

Commit

Permalink
⭐️ ability to print curl command when trace is active (#11)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Hartmann <[email protected]>
  • Loading branch information
chris-rock authored Jul 26, 2022
1 parent 87378aa commit 201af8a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
33 changes: 33 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
2 changes: 2 additions & 0 deletions examples/pingpong/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=

0 comments on commit 201af8a

Please sign in to comment.