Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracer: Check for nil interface types in span.Finish(WithError()) #2036

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -939,8 +940,18 @@ func FinishTime(t time.Time) FinishOption {

// WithError marks the span as having had an error. It uses the information from
// err to set tags such as the error message, error type and stack trace. It has
// no effect if the error is nil.
// no effect if the error is nil or the underlying object is a nil pointer (https://go.dev/doc/faq#nil_error).
func WithError(err error) FinishOption {
if err == nil {
return func(_ *ddtrace.FinishConfig) {}
}
v := reflect.ValueOf(err)
// Here be dragons: https://go.dev/doc/faq#nil_error
katiehockman marked this conversation as resolved.
Show resolved Hide resolved
// https://github.com/DataDog/dd-trace-go/issues/2029
if !v.IsValid() || (v.Kind() == reflect.Ptr && v.IsNil()) {
log.Debug("Underlying error value is nil pointer, ignoring. See https://go.dev/doc/faq#nil_error for details.")
return func(_ *ddtrace.FinishConfig) {}
ajgajg1134 marked this conversation as resolved.
Show resolved Hide resolved
}
return func(cfg *ddtrace.FinishConfig) {
cfg.Error = err
}
Expand Down
22 changes: 22 additions & 0 deletions ddtrace/tracer/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,28 @@ func TestSpanFinishWithError(t *testing.T) {
assert.NotEmpty(span.Meta[ext.ErrorStack])
}

type MyError struct {
msg string
}

func (e *MyError) Error() string {
return e.msg
}

func TestSpanFinishWithErrorNilCustomError(t *testing.T) {
assert := assert.New(t)

var err error
var myError *MyError
myError = nil
err = myError

span := newBasicSpan("web.request")
span.Finish(WithError(err))

assert.Equal(int32(0), span.Error)
}

func TestSpanFinishWithErrorNoDebugStack(t *testing.T) {
assert := assert.New(t)

Expand Down
Loading