Skip to content

Commit c1573c2

Browse files
committed
Add WithStatus option to set Span status at the same time (open-telemetry#1677)
Signed-off-by: lastchiliarch <lastchiliarch@163.com>
1 parent 1d42dfa commit c1573c2

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

sdk/trace/span.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ func (s *span) End(options ...trace.SpanOption) {
245245
}
246246

247247
// RecordError will record err as a span event for this span.
248-
// this method does not change the Span status in default.
249-
// If you want to change Span status, pass WithStatus as opts.
250-
// this metod does nothing If this span is not being recorded or err is nil.
248+
// This method does not change the Span status in default
249+
// To change Span status, pass `WithStatus` as options.
250+
// This method does nothing If this span is not being recorded or err is nil.
251251
func (s *span) RecordError(err error, opts ...trace.EventOption) {
252252
if s == nil || err == nil || !s.IsRecording() {
253253
return

sdk/trace/trace_test.go

+40-31
Original file line numberDiff line numberDiff line change
@@ -1165,53 +1165,62 @@ func TestRecordErrorNil(t *testing.T) {
11651165
}
11661166

11671167
func TestRecordErrorWithStatus(t *testing.T) {
1168+
errTime := time.Now()
11681169
scenarios := []struct {
1169-
err error
1170-
typ string
1171-
msg string
1170+
err error
1171+
want *SpanSnapshot
11721172
}{
11731173
{
11741174
err: ottest.NewTestError("test error"),
1175-
typ: "go.opentelemetry.io/otel/internal/internaltest.TestError",
1176-
msg: "test error",
1175+
want: &SpanSnapshot{
1176+
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
1177+
TraceID: tid,
1178+
TraceFlags: 0x1,
1179+
}),
1180+
Parent: sc.WithRemote(true),
1181+
Name: "span0",
1182+
StatusCode: codes.Error,
1183+
SpanKind: trace.SpanKindInternal,
1184+
MessageEvents: []trace.Event{
1185+
{
1186+
Name: semconv.ExceptionEventName,
1187+
Time: errTime,
1188+
Attributes: []attribute.KeyValue{
1189+
semconv.ExceptionTypeKey.String("go.opentelemetry.io/otel/internal/internaltest.TestError"),
1190+
semconv.ExceptionMessageKey.String("test error"),
1191+
},
1192+
},
1193+
},
1194+
InstrumentationLibrary: instrumentation.Library{Name: "RecordError"},
1195+
},
1196+
},
1197+
{
1198+
err: nil,
1199+
want: &SpanSnapshot{
1200+
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
1201+
TraceID: tid,
1202+
TraceFlags: 0x1,
1203+
}),
1204+
Parent: sc.WithRemote(true),
1205+
Name: "span0",
1206+
StatusCode: codes.Unset,
1207+
SpanKind: trace.SpanKindInternal,
1208+
InstrumentationLibrary: instrumentation.Library{Name: "RecordError"},
1209+
},
11771210
},
11781211
}
11791212

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

1185-
errTime := time.Now()
1217+
span := startSpan(tp, "RecordError")
11861218
span.RecordError(s.err, trace.WithTimestamp(errTime), trace.WithStatus(true))
1187-
11881219
got, err := endSpan(te, span)
11891220
if err != nil {
11901221
t.Fatal(err)
11911222
}
1192-
1193-
want := &SpanSnapshot{
1194-
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
1195-
TraceID: tid,
1196-
TraceFlags: 0x1,
1197-
}),
1198-
Parent: sc.WithRemote(true),
1199-
Name: "span0",
1200-
StatusCode: codes.Error,
1201-
SpanKind: trace.SpanKindInternal,
1202-
MessageEvents: []trace.Event{
1203-
{
1204-
Name: semconv.ExceptionEventName,
1205-
Time: errTime,
1206-
Attributes: []attribute.KeyValue{
1207-
semconv.ExceptionTypeKey.String(s.typ),
1208-
semconv.ExceptionMessageKey.String(s.msg),
1209-
},
1210-
},
1211-
},
1212-
InstrumentationLibrary: instrumentation.Library{Name: "RecordError"},
1213-
}
1214-
if diff := cmpDiff(got, want); diff != "" {
1223+
if diff := cmpDiff(got, s.want); diff != "" {
12151224
t.Errorf("SpanErrorOptions: -got +want %s", diff)
12161225
}
12171226
}

trace/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestNewSpanConfig(t *testing.T) {
4141
}
4242

4343
withStatusOpt := WithStatus(true)
44-
//just for coverage
44+
// For coverage
4545
withStatusOpt.private()
4646

4747
tests := []struct {

0 commit comments

Comments
 (0)