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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
10 changes: 10 additions & 0 deletions ddtrace/tracer/option.go
Expand Up @@ -15,6 +15,7 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -941,6 +942,15 @@ func FinishTime(t time.Time) FinishOption {
// err to set tags such as the error message, error type and stack trace. It has
// no effect if the error is nil.
ajgajg1134 marked this conversation as resolved.
Show resolved Hide resolved
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()) {
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
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