Skip to content

Commit a7f7aba

Browse files
mrveeraMrAlias
andauthored
SpanStatus description set only when status code is set to Error (#1662)
* Fix #1658 SpanStatus description set only when status code is set to error * Update CHANGELOG.md Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent 05252f4 commit a7f7aba

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ 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+
15+
### Changed
16+
1417
- Added non-empty string check for trace `Attribute` keys. (#1659)
18+
- Add `description` to SpanStatus only when `StatusCode` is set to error. (#1662)
1519

1620
### Removed
1721

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: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,35 @@ func TestSetSpanStatus(t *testing.T) {
789789
}
790790
}
791791

792+
func TestSetSpanStatusWithoutMessageWhenStatusIsNotError(t *testing.T) {
793+
te := NewTestExporter()
794+
tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty()))
795+
796+
span := startSpan(tp, "SpanStatus")
797+
span.SetStatus(codes.Ok, "This message will be ignored")
798+
got, err := endSpan(te, span)
799+
if err != nil {
800+
t.Fatal(err)
801+
}
802+
803+
want := &export.SpanSnapshot{
804+
SpanContext: trace.SpanContext{
805+
TraceID: tid,
806+
TraceFlags: 0x1,
807+
},
808+
ParentSpanID: sid,
809+
Name: "span0",
810+
SpanKind: trace.SpanKindInternal,
811+
StatusCode: codes.Ok,
812+
StatusMessage: "",
813+
HasRemoteParent: true,
814+
InstrumentationLibrary: instrumentation.Library{Name: "SpanStatus"},
815+
}
816+
if diff := cmpDiff(got, want); diff != "" {
817+
t.Errorf("SetSpanStatus: -got +want %s", diff)
818+
}
819+
}
820+
792821
func cmpDiff(x, y interface{}) string {
793822
return cmp.Diff(x, y,
794823
cmp.AllowUnexported(attribute.Value{}),
@@ -1396,7 +1425,7 @@ func TestReadOnlySpan(t *testing.T) {
13961425
assert.Equal(t, kv.Key, ro.Events()[0].Attributes[0].Key)
13971426
assert.Equal(t, kv.Value, ro.Events()[0].Attributes[0].Value)
13981427
assert.Equal(t, codes.Ok, ro.StatusCode())
1399-
assert.Equal(t, "foo", ro.StatusMessage())
1428+
assert.Equal(t, "", ro.StatusMessage())
14001429
assert.Equal(t, "ReadOnlySpan", ro.InstrumentationLibrary().Name)
14011430
assert.Equal(t, "3", ro.InstrumentationLibrary().Version)
14021431
assert.Equal(t, kv.Key, ro.Resource().Attributes()[0].Key)

0 commit comments

Comments
 (0)