Skip to content

Commit

Permalink
Implement the SDK's Span.SetStatus method (#1112)
Browse files Browse the repository at this point in the history
Part of #954
  • Loading branch information
MrAlias authored Sep 25, 2024
1 parent 45cd62b commit bd728ab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
13 changes: 12 additions & 1 deletion sdk/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,18 @@ func (s *span) SetStatus(c codes.Code, msg string) {
if s == nil || !s.sampled {
return
}
/* TODO: implement */

stat := s.span.Status()
stat.SetMessage(msg)

switch c {
case codes.Unset:
stat.SetCode(ptrace.StatusCodeUnset)
case codes.Error:
stat.SetCode(ptrace.StatusCodeError)
case codes.Ok:
stat.SetCode(ptrace.StatusCodeOk)
}
}

func (s *span) SetAttributes(attrs ...attribute.KeyValue) {
Expand Down
20 changes: 20 additions & 0 deletions sdk/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,26 @@ func TestSpanIsRecording(t *testing.T) {
assert.False(t, s.IsRecording(), "unsampled span should not be recorded")
}

func TestSpanSetStatus(t *testing.T) {
s := spanBuilder{}.Build()

want := ptrace.NewStatus()
assert.Equal(t, want, s.span.Status(), "empty status should not be set")

msg := "test"
want.SetMessage(msg)

for c, p := range map[codes.Code]ptrace.StatusCode{
codes.Error: ptrace.StatusCodeError,
codes.Ok: ptrace.StatusCodeOk,
codes.Unset: ptrace.StatusCodeUnset,
} {
want.SetCode(p)
s.SetStatus(c, msg)
assert.Equalf(t, want, s.span.Status(), "code: %s, msg: %s", c, msg)
}
}

func TestSpanSetAttributes(t *testing.T) {
builder := spanBuilder{}

Expand Down

0 comments on commit bd728ab

Please sign in to comment.