Skip to content

Commit

Permalink
contrib/net/http: add errCheck function
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Feb 3, 2023
1 parent 863b9b2 commit edc342d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
10 changes: 10 additions & 0 deletions contrib/net/http/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type roundTripperConfig struct {
resourceNamer func(req *http.Request) string
ignoreRequest func(*http.Request) bool
spanOpts []ddtrace.StartSpanOption
errCheck func(err error) bool
}

func newRoundTripperConfig() *roundTripperConfig {
Expand Down Expand Up @@ -216,3 +217,12 @@ func RTWithIgnoreRequest(f func(*http.Request) bool) RoundTripperOption {
cfg.ignoreRequest = f
}
}

// RTWithErrorCheck specifies a function fn which determines whether the passed
// error should be marked as an error. The fn is called whenever an aws operation
// finishes with an error
func RTWithErrorCheck(fn func(err error) bool) RoundTripperOption {
return func(cfg *roundTripperConfig) {
cfg.errCheck = fn
}
}
10 changes: 8 additions & 2 deletions contrib/net/http/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ func (rt *roundTripper) RoundTrip(req *http.Request) (res *http.Response, err er
if rt.cfg.after != nil {
rt.cfg.after(res, span)
}
span.Finish(tracer.WithError(err))
if rt.cfg.errCheck == nil || rt.cfg.errCheck(err) {
span.Finish(tracer.WithError(err))
} else {
span.Finish()
}
}()
if rt.cfg.before != nil {
rt.cfg.before(req, span)
Expand All @@ -67,7 +71,9 @@ func (rt *roundTripper) RoundTrip(req *http.Request) (res *http.Response, err er
res, err = rt.base.RoundTrip(r2)
if err != nil {
span.SetTag("http.errors", err.Error())
span.SetTag(ext.Error, err)
if rt.cfg.errCheck == nil || rt.cfg.errCheck(err) {
span.SetTag(ext.Error, err)
}
} else {
span.SetTag(ext.HTTPCode, strconv.Itoa(res.StatusCode))
// treat 5XX as errors
Expand Down
47 changes: 47 additions & 0 deletions contrib/net/http/roundtripper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,53 @@ func TestRoundTripperNetworkError(t *testing.T) {
assert.Equal(t, "net/http", s0.Tag(ext.Component))
}

func TestRoundTripperNetworkErrorWithErrorCheck(t *testing.T) {
failedRequest := func(t *testing.T, mt mocktracer.Tracer, forwardErr bool, opts ...RoundTripperOption) mocktracer.Span {
done := make(chan struct{})
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := tracer.Extract(tracer.HTTPHeadersCarrier(r.Header))
assert.NoError(t, err)
<-done
}))
defer s.Close()
defer close(done)

rt := WrapRoundTripper(http.DefaultTransport,
RTWithErrorCheck(func(err error) bool {
return forwardErr
}))

client := &http.Client{
Transport: rt,
Timeout: 1 * time.Millisecond,
}

client.Get(s.URL + "/hello/world")

spans := mt.FinishedSpans()
assert.Len(t, spans, 1)

s0 := spans[0]
return s0
}

t.Run("error skipped", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

span := failedRequest(t, mt, false)
assert.Nil(t, span.Tag(ext.Error))
})

t.Run("error forwarded", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

span := failedRequest(t, mt, true)
assert.NotNil(t, span.Tag(ext.Error))
})
}

func TestRoundTripperCredentials(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
Expand Down

0 comments on commit edc342d

Please sign in to comment.