diff --git a/plugins/http/client_intercepter.go b/plugins/http/client_intercepter.go index 482a7783..fd28851d 100644 --- a/plugins/http/client_intercepter.go +++ b/plugins/http/client_intercepter.go @@ -51,6 +51,9 @@ func (h *ClientInterceptor) AfterInvoke(invocation operator.Invocation, result . span := invocation.GetContext().(tracing.Span) if resp, ok := result[0].(*http.Response); ok && resp != nil { span.Tag(tracing.TagStatusCode, fmt.Sprintf("%d", resp.StatusCode)) + if resp.StatusCode >= 400 { + span.ErrorOccured() + } } if err, ok := result[1].(error); ok && err != nil { span.Error(err.Error()) diff --git a/plugins/http/client_intercepter_test.go b/plugins/http/client_intercepter_test.go index 51e9c8e8..eaf812a8 100644 --- a/plugins/http/client_intercepter_test.go +++ b/plugins/http/client_intercepter_test.go @@ -57,3 +57,27 @@ func TestClientInvoke(t *testing.T) { assert.Nil(t, spans[0].Refs(), "refs should be nil") assert.Greater(t, spans[0].EndTime(), spans[0].StartTime(), "end time should be greater than start time") } + +func TestClientInvokeError(t *testing.T) { + defer core.ResetTracingContext() + interceptor := &ClientInterceptor{} + request, err := http.NewRequest("GET", "http://localhost/", http.NoBody) + assert.Nil(t, err, "new request error should be nil") + invocation := operator.NewInvocation(nil, request) + err = interceptor.BeforeInvoke(invocation) + assert.Nil(t, err, "before invoke error should be nil") + assert.NotNil(t, invocation.GetContext(), "context should not be nil") + + time.Sleep(100 * time.Millisecond) + + err = interceptor.AfterInvoke(invocation, &http.Response{ + StatusCode: 500, + }, nil) + assert.Nil(t, err, "after invoke error should be nil") + + time.Sleep(100 * time.Millisecond) + spans := core.GetReportedSpans() + assert.NotNil(t, spans, "spans should not be nil") + assert.Equal(t, 1, len(spans), "spans length should be 1") + assert.True(t, spans[0].IsError(), "span should be error") +}