Skip to content

Commit

Permalink
opentracing: set Span error when adding error tags
Browse files Browse the repository at this point in the history
This change allows setting a tag of name Error ("error") having a value
of error (but covers for other types to) which will in turn set the
error properties of the Span internally.

Fixes #144
  • Loading branch information
gbbr committed Jan 18, 2018
1 parent debee06 commit 53d66e2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
9 changes: 9 additions & 0 deletions opentracing/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ func (s *Span) SetTag(key string, value interface{}) ot.Span {
s.Span.Lock()
defer s.Span.Unlock()
s.Span.Type = fmt.Sprint(value)
case Error:
switch v := value.(type) {
case nil:
// no error
case error:
s.Span.SetError(v)
default:
s.Span.SetError(fmt.Errorf("%v", v))
}
default:
// NOTE: locking is not required because the `SetMeta` is
// already thread-safe
Expand Down
53 changes: 53 additions & 0 deletions opentracing/span_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package opentracing

import (
"errors"
"testing"
"time"

Expand Down Expand Up @@ -74,3 +75,55 @@ func TestSpanSetDatadogTags(t *testing.T) {
assert.Equal("db-cluster", span.Span.Service)
assert.Equal("SELECT * FROM users;", span.Span.Resource)
}

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

for _, tt := range []struct {
name string // span name
val interface{} // tag value
msg string // error message
typ string // error type
}{
{
name: "error.error",
val: errors.New("some error"),
msg: "some error",
typ: "*errors.errorString",
},
{
name: "error.string",
val: "some string error",
msg: "some string error",
typ: "*errors.errorString",
},
{
name: "error.struct",
val: struct{ N int }{5},
msg: "{5}",
typ: "*errors.errorString",
},
{
name: "error.other",
val: 1,
msg: "1",
typ: "*errors.errorString",
},
{
name: "error.nil",
val: nil,
msg: "",
typ: "",
},
} {
span := NewSpan(tt.name)
span.SetTag(Error, tt.val)

assert.Equal(span.Meta["error.msg"], tt.msg)
assert.Equal(span.Meta["error.type"], tt.typ)

if tt.val != nil {
assert.NotEqual(span.Meta["error.stack"], "")
}
}
}
8 changes: 2 additions & 6 deletions opentracing/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ const (
ServiceName = "service.name"
// ResourceName defines the Resource name for the Span
ResourceName = "resource.name"
// ErrorMsg defines the error message
ErrorMsg = "error.msg"
// ErrorType defines the error class
ErrorType = "error.type"
// ErrorStack defines the stack for the given error or panic
ErrorStack = "error.stack"
// Error defines an error.
Error = "error.error"
)

0 comments on commit 53d66e2

Please sign in to comment.