Skip to content

Commit

Permalink
add nil and recording check to span.RecordError (elastic#1566)
Browse files Browse the repository at this point in the history
Co-authored-by: Damien Mathieu <42@dmathieu.com>
  • Loading branch information
jacksehr and dmathieu authored Jan 2, 2024
1 parent 8d1504d commit 9adf983
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
4 changes: 4 additions & 0 deletions module/apmotel/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (s *span) IsRecording() bool {
}

func (s *span) RecordError(err error, opts ...trace.EventOption) {
if s == nil || err == nil || !s.IsRecording() {
return
}

opts = append(opts, trace.WithAttributes(
semconv.ExceptionType(typeStr(err)),
semconv.ExceptionMessage(err.Error()),
Expand Down
73 changes: 56 additions & 17 deletions module/apmotel/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,25 +702,64 @@ func TestSpanRecording(t *testing.T) {
}

func TestSpanRecordError(t *testing.T) {
tp, err := NewTracerProvider()
assert.NoError(t, err)
tracer := newTracer(tp.(*tracerProvider))
_, s := tracer.Start(context.Background(), "mySpan")

assert.Equal(t, []event(nil), s.(*span).events)

now := time.Now()
s.RecordError(errors.New("test"), trace.WithTimestamp(now))
assert.Equal(t, []event{
event{
Name: "exception",
Attributes: []attribute.KeyValue{
attribute.String("exception.type", "*errors.errorString"),
attribute.String("exception.message", "test"),
for _, tt := range []struct {
name string
getSpan func(context.Context, trace.Tracer) trace.Span
err error
getExpectedEvents func(time.Time) []event
}{
{
name: "with a valid error",
getSpan: func(ctx context.Context, t trace.Tracer) trace.Span {
_, s := t.Start(context.Background(), "mySpan")
return s
},
err: errors.New("test"),
getExpectedEvents: func(t time.Time) []event {
return []event{{
Name: "exception",
Attributes: []attribute.KeyValue{
attribute.String("exception.type", "*errors.errorString"),
attribute.String("exception.message", "test"),
},
Time: t,
}}
},
Time: now,
},
}, s.(*span).events)
{
name: "with a nil error",
getSpan: func(ctx context.Context, t trace.Tracer) trace.Span {
_, s := t.Start(context.Background(), "mySpan")
return s
},
err: nil,
getExpectedEvents: func(t time.Time) []event { return []event(nil) },
},
{
name: "with a non recording span",
getSpan: func(ctx context.Context, t trace.Tracer) trace.Span {
return &span{
tx: &apm.Transaction{},
}
},
err: errors.New("test"),
getExpectedEvents: func(t time.Time) []event { return []event(nil) },
},
} {
t.Run(tt.name, func(t *testing.T) {
tp, err := NewTracerProvider()
assert.NoError(t, err)
tracer := newTracer(tp.(*tracerProvider))
s := tt.getSpan(context.Background(), tracer)

assert.Equal(t, []event(nil), s.(*span).events)

now := time.Now()
s.RecordError(tt.err, trace.WithTimestamp(now))

assert.Equal(t, tt.getExpectedEvents(now), s.(*span).events)
})
}
}

func TestSpanSetStatus(t *testing.T) {
Expand Down

0 comments on commit 9adf983

Please sign in to comment.