Skip to content

Commit

Permalink
HTTP traced client panic (fixes #247)
Browse files Browse the repository at this point in the history
Signed-off-by: Sotirios Mantziaris <[email protected]>
  • Loading branch information
mantzas authored Dec 11, 2018
1 parent 803b83a commit 4bde445
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
5 changes: 3 additions & 2 deletions trace/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/mantzas/patron/trace"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)

Expand Down Expand Up @@ -52,9 +52,10 @@ func (tc *TracedClient) Do(ctx context.Context, req *http.Request) (*http.Respon
rsp, err := tc.cl.Do(req)
if err != nil {
ext.Error.Set(ht.Span(), true)
} else {
ext.HTTPStatusCode.Set(ht.Span(), uint16(rsp.StatusCode))
}
ext.HTTPMethod.Set(ht.Span(), req.Method)
ext.HTTPUrl.Set(ht.Span(), req.URL.String())
ext.HTTPStatusCode.Set(ht.Span(), uint16(rsp.StatusCode))
return rsp, err
}
24 changes: 23 additions & 1 deletion trace/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/mantzas/patron/trace"
"github.com/opentracing/opentracing-go"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/stretchr/testify/assert"
)
Expand All @@ -36,6 +36,28 @@ func TestTracedClient_Do(t *testing.T) {
assert.Equal(t, trace.HTTPOpName("Client", "GET", ts.URL), sp.OperationName)
}

func TestTracedClient_Do_Error(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "true", r.Header.Get("Mockpfx-Ids-Sampled"))
assert.Equal(t, "46", r.Header.Get("Mockpfx-Ids-Spanid"))
assert.Equal(t, "43", r.Header.Get("Mockpfx-Ids-Traceid"))
fmt.Fprintln(w, "Hello, client")
}))
defer ts.Close()
mtr := mocktracer.New()
opentracing.SetGlobalTracer(mtr)
c, err := New()
assert.NoError(t, err)
req, err := http.NewRequest("GET", "", nil)
assert.NoError(t, err)
rsp, err := c.Do(context.Background(), req)
assert.Error(t, err)
assert.Nil(t, rsp)
sp := mtr.FinishedSpans()[0]
assert.NotNil(t, sp)
assert.Equal(t, "HTTP GET", sp.OperationName)
}

func TestNew(t *testing.T) {
type args struct {
opt OptionFunc
Expand Down

0 comments on commit 4bde445

Please sign in to comment.