Skip to content

Commit

Permalink
set span error when http status code >= 400 on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwn committed Dec 22, 2024
1 parent 1d064fd commit a5f845b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions plugins/http/client_intercepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
24 changes: 24 additions & 0 deletions plugins/http/client_intercepter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit a5f845b

Please sign in to comment.