Skip to content

Commit

Permalink
Add WithStatus option to set Span status at the same time (open-telem…
Browse files Browse the repository at this point in the history
…etry#1677)

Signed-off-by: lastchiliarch <[email protected]>
  • Loading branch information
lastchiliarch committed Apr 17, 2021
1 parent 1d42dfa commit d3b58b6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
6 changes: 3 additions & 3 deletions sdk/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ func (s *span) End(options ...trace.SpanOption) {
}

// RecordError will record err as a span event for this span.
// this method does not change the Span status in default.
// If you want to change Span status, pass WithStatus as opts.
// this metod does nothing If this span is not being recorded or err is nil.
// This method does not change the Span status in default
// To change Span status, pass `WithStatus` as options.
// This method does nothing If this span is not being recorded or err is nil.
func (s *span) RecordError(err error, opts ...trace.EventOption) {
if s == nil || err == nil || !s.IsRecording() {
return
Expand Down
70 changes: 39 additions & 31 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,53 +1165,61 @@ func TestRecordErrorNil(t *testing.T) {
}

func TestRecordErrorWithStatus(t *testing.T) {
errTime := time.Now()
scenarios := []struct {
err error
typ string
msg string
err error
want *SpanSnapshot
}{
{
err: ottest.NewTestError("test error"),
typ: "go.opentelemetry.io/otel/internal/internaltest.TestError",
msg: "test error",
want: &SpanSnapshot{
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
Parent: sc.WithRemote(true),
Name: "span0",
StatusCode: codes.Error,
SpanKind: trace.SpanKindInternal,
MessageEvents: []trace.Event{
{
Name: semconv.ExceptionEventName,
Time: errTime,
Attributes: []attribute.KeyValue{
semconv.ExceptionTypeKey.String("go.opentelemetry.io/otel/internal/internaltest.TestError"),
semconv.ExceptionMessageKey.String("test error"),
},
},
},
InstrumentationLibrary: instrumentation.Library{Name: "RecordError"},
},
},
{
err: nil,
want: &SpanSnapshot{
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
Parent: sc.WithRemote(true),
Name: "span0",
StatusCode: codes.Unset,
SpanKind: trace.SpanKindInternal,
InstrumentationLibrary: instrumentation.Library{Name: "RecordError"},
},
},
}

for _, s := range scenarios {
te := NewTestExporter()
tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty()))
span := startSpan(tp, "RecordError")

errTime := time.Now()
span.RecordError(s.err, trace.WithTimestamp(errTime), trace.WithStatus(true))

got, err := endSpan(te, span)
if err != nil {
t.Fatal(err)
}

want := &SpanSnapshot{
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
Parent: sc.WithRemote(true),
Name: "span0",
StatusCode: codes.Error,
SpanKind: trace.SpanKindInternal,
MessageEvents: []trace.Event{
{
Name: semconv.ExceptionEventName,
Time: errTime,
Attributes: []attribute.KeyValue{
semconv.ExceptionTypeKey.String(s.typ),
semconv.ExceptionMessageKey.String(s.msg),
},
},
},
InstrumentationLibrary: instrumentation.Library{Name: "RecordError"},
}
if diff := cmpDiff(got, want); diff != "" {
if diff := cmpDiff(got, s.want); diff != "" {
t.Errorf("SpanErrorOptions: -got +want %s", diff)
}
}
Expand Down
2 changes: 1 addition & 1 deletion trace/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestNewSpanConfig(t *testing.T) {
}

withStatusOpt := WithStatus(true)
//just for coverage
// For coverage
withStatusOpt.private()

tests := []struct {
Expand Down

0 comments on commit d3b58b6

Please sign in to comment.