Skip to content

Commit 8e30da1

Browse files
committed
Fix #1658 SpanStatus description set only when status code is set to error
1 parent 7153ef2 commit 8e30da1

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1111
## Added
1212

1313
- Added `Marshler` config option to `otlphttp` to enable otlp over json or protobufs. (#1586)
14+
- Add `description` to SpanStatus only when `StatusCode` is set to error.
1415
### Removed
1516

1617
- Removed the exported `SimpleSpanProcessor` and `BatchSpanProcessor` structs.

sdk/trace/span.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,17 @@ func (s *span) IsRecording() bool {
170170

171171
// SetStatus sets the status of this span in the form of a code and a
172172
// message. This overrides the existing value of this span's status if one
173-
// exists. If this span is not being recorded than this method does nothing.
173+
// exists. Message will be set only if status is error. If this span is not being
174+
// recorded than this method does nothing.
174175
func (s *span) SetStatus(code codes.Code, msg string) {
175176
if !s.IsRecording() {
176177
return
177178
}
178179
s.mu.Lock()
179180
s.statusCode = code
180-
s.statusMessage = msg
181+
if code == codes.Error {
182+
s.statusMessage = msg
183+
}
181184
s.mu.Unlock()
182185
}
183186

sdk/trace/trace_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,35 @@ func TestSetSpanStatus(t *testing.T) {
744744
}
745745
}
746746

747+
func TestSetSpanStatusWithoutMessageWhenStatusIsNotError(t *testing.T) {
748+
te := NewTestExporter()
749+
tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty()))
750+
751+
span := startSpan(tp, "SpanStatus")
752+
span.SetStatus(codes.Ok, "This message will be ignored")
753+
got, err := endSpan(te, span)
754+
if err != nil {
755+
t.Fatal(err)
756+
}
757+
758+
want := &export.SpanSnapshot{
759+
SpanContext: trace.SpanContext{
760+
TraceID: tid,
761+
TraceFlags: 0x1,
762+
},
763+
ParentSpanID: sid,
764+
Name: "span0",
765+
SpanKind: trace.SpanKindInternal,
766+
StatusCode: codes.Ok,
767+
StatusMessage: "",
768+
HasRemoteParent: true,
769+
InstrumentationLibrary: instrumentation.Library{Name: "SpanStatus"},
770+
}
771+
if diff := cmpDiff(got, want); diff != "" {
772+
t.Errorf("SetSpanStatus: -got +want %s", diff)
773+
}
774+
}
775+
747776
func cmpDiff(x, y interface{}) string {
748777
return cmp.Diff(x, y,
749778
cmp.AllowUnexported(attribute.Value{}),

0 commit comments

Comments
 (0)