diff --git a/trace/http/http.go b/trace/http/http.go index 25f338395..c3dc78aee 100644 --- a/trace/http/http.go +++ b/trace/http/http.go @@ -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" ) @@ -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 } diff --git a/trace/http/http_test.go b/trace/http/http_test.go index 1df85a30a..bcf2dd1dd 100644 --- a/trace/http/http_test.go +++ b/trace/http/http_test.go @@ -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" ) @@ -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