Skip to content

Commit

Permalink
IMPROVEMENT: netdiagnostics trace change (v13.1.2)
Browse files Browse the repository at this point in the history
- improvement: netdiagnostics with `--trace` flag will now list response
  header information
  • Loading branch information
vjmp committed Jan 23, 2023
1 parent 4e29d4a commit 8a63f79
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
20 changes: 20 additions & 0 deletions cloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (

"github.com/robocorp/rcc/common"
"github.com/robocorp/rcc/pathlib"
"github.com/robocorp/rcc/set"
"github.com/robocorp/rcc/settings"
"github.com/robocorp/rcc/xviper"
)

type internalClient struct {
endpoint string
client *http.Client
tracing bool
}

type Request struct {
Expand Down Expand Up @@ -48,6 +50,7 @@ type Client interface {
Delete(request *Request) *Response
NewClient(endpoint string) (Client, error)
WithTimeout(time.Duration) Client
WithTracing() Client
}

func EnsureHttps(endpoint string) (string, error) {
Expand All @@ -73,6 +76,7 @@ func NewClient(endpoint string) (Client, error) {
return &internalClient{
endpoint: https,
client: &http.Client{Transport: settings.Global.ConfiguredHttpTransport()},
tracing: false,
}, nil
}

Expand All @@ -83,6 +87,15 @@ func (it *internalClient) WithTimeout(timeout time.Duration) Client {
Transport: settings.Global.ConfiguredHttpTransport(),
Timeout: timeout,
},
tracing: it.tracing,
}
}

func (it *internalClient) WithTracing() Client {
return &internalClient{
endpoint: it.endpoint,
client: it.client,
tracing: true,
}
}

Expand Down Expand Up @@ -128,6 +141,13 @@ func (it *internalClient) does(method string, request *Request) *Response {
return response
}
defer httpResponse.Body.Close()
if it.tracing {
common.Trace("Response %d headers:", httpResponse.StatusCode)
keys := set.Keys(httpResponse.Header)
for _, key := range keys {
common.Trace("> %s: %q", key, httpResponse.Header[key])
}
}
response.Status = httpResponse.StatusCode
if request.Stream != nil {
io.Copy(request.Stream, httpResponse.Body)
Expand Down
2 changes: 1 addition & 1 deletion common/version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package common

const (
Version = `v13.1.1`
Version = `v13.1.2`
)
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# rcc change log

## v13.1.2 (date: 23.1.2023)

- improvement: netdiagnostics with `--trace` flag will now list response
header information

## v13.1.1 (date: 20.1.2023) UNSTABLE

- fix: netdiagnostics configuration flag change (now it is `--checks filename`)
Expand Down
4 changes: 4 additions & 0 deletions mocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (it *MockClient) WithTimeout(time.Duration) cloud.Client {
return it
}

func (it *MockClient) WithTracing() cloud.Client {
return it
}

func (it *MockClient) NewRequest(url string) *cloud.Request {
return &cloud.Request{
Url: url,
Expand Down
6 changes: 6 additions & 0 deletions operations/netdiagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ func headRequest(link string) (code int, fingerprint string, err error) {

client, err := cloud.NewClient(link)
fail.On(err != nil, "Client for %q failed, reason: %v", link, err)
if common.TraceFlag {
client = client.WithTracing()
}
request := client.NewRequest("")
response := client.Head(request)
fail.On(response.Err != nil, "HEAD request to %q failed, reason: %v", link, response.Err)
Expand All @@ -153,6 +156,9 @@ func getRequest(link string) (code int, fingerprint string, err error) {

client, err := cloud.NewClient(link)
fail.On(err != nil, "Client for %q failed, reason: %v", link, err)
if common.TraceFlag {
client = client.WithTracing()
}
request := client.NewRequest("")
response := client.Get(request)
fail.On(response.Err != nil, "HEAD request to %q failed, reason: %v", link, response.Err)
Expand Down

0 comments on commit 8a63f79

Please sign in to comment.