Skip to content

Commit

Permalink
fix(clihttp): log errors (#212)
Browse files Browse the repository at this point in the history
* fix: add clihttp Providers

* fix: add clihttp Providers

* fix: add clihttp Providers

* fix: add clihttp Providers

* fix(logging): inconsistency between go kit Logger and spanLogger

Only log.Valuer passed via log.With are treated as dynamic argument in go kit log. The change makes spanLogger consistent with go kit.

* fix(clihttp): correctly logs error
  • Loading branch information
Reasno authored Nov 18, 2021
1 parent d4dd508 commit 65d7eb6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions clihttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
c.tracer.Inject(clientSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
response, err := c.underlying.Do(req)
if err != nil {
ext.Error.Set(clientSpan, true)
clientSpan.LogFields(log.String("event", "error"), log.String("message", err.Error()))
return response, err
}

Expand Down
11 changes: 10 additions & 1 deletion clihttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@ func TestClient_Do(t *testing.T) {
func() *http.Request { r, _ := http.NewRequest("GET", "https://example.com/", nil); return r }(),
[]Option{WithResponseLogThreshold(1)},
},
{
"error",
func() *http.Request { r, _ := http.NewRequest("GET", "https://non-exist-domain.com/", nil); return r }(),
[]Option{},
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
tracer := mocktracer.New()
client := NewClient(tracer, c.Option...)
resp, _ := client.Do(c.request)
resp, err := client.Do(c.request)
if err != nil {
assert.True(t, tracer.FinishedSpans()[0].Tags()["error"].(bool))
return
}
defer resp.Body.Close()
assert.NotEmpty(t, tracer.FinishedSpans())
byt, _ := ioutil.ReadAll(resp.Body)
Expand Down

0 comments on commit 65d7eb6

Please sign in to comment.