Skip to content

Commit

Permalink
Fix #143 - Span status should map rpc status. (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuereth committed Mar 5, 2021
1 parent f288a1b commit 8f77e63
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 8 additions & 2 deletions exporter/trace/cloudtrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/api/option"
tracepb "google.golang.org/genproto/googleapis/devtools/cloudtrace/v2"
codepb "google.golang.org/genproto/googleapis/rpc/code"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
Expand All @@ -53,7 +54,9 @@ func TestExporter_ExportSpan(t *testing.T) {
assert.NoError(t, err)

_, span := otel.Tracer("test-tracer").Start(context.Background(), "test-span")
span.SetStatus(codes.Ok, "Status Message")
// NOTE: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#set-status
// Status message MUST only be used with error code, so this will be dropped.
span.SetStatus(codes.Ok, "Status message")
span.End()
assert.True(t, span.SpanContext().IsValid())

Expand All @@ -64,7 +67,10 @@ func TestExporter_ExportSpan(t *testing.T) {
// wait exporter to shutdown (closes grpc connection)
shutdown()
assert.EqualValues(t, 2, mock.GetNumSpans())
assert.EqualValues(t, "Status Message", mock.GetSpan(0).GetStatus().Message)
// Note: Go returns empty string for an unset member.
assert.EqualValues(t, codepb.Code_OK, mock.GetSpan(0).GetStatus().Code)
assert.EqualValues(t, "", mock.GetSpan(0).GetStatus().Message)
assert.EqualValues(t, codepb.Code_UNKNOWN, mock.GetSpan(1).GetStatus().Code)
assert.EqualValues(t, "Error Message", mock.GetSpan(1).GetStatus().Message)
}

Expand Down
14 changes: 9 additions & 5 deletions exporter/trace/trace_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
timestamppb "github.com/golang/protobuf/ptypes/timestamp"
wrapperspb "github.com/golang/protobuf/ptypes/wrappers"
tracepb "google.golang.org/genproto/googleapis/devtools/cloudtrace/v2"
codepb "google.golang.org/genproto/googleapis/rpc/code"
statuspb "google.golang.org/genproto/googleapis/rpc/status"
)

Expand Down Expand Up @@ -115,11 +116,14 @@ func protoFromSpanSnapshot(s *export.SpanSnapshot, projectID string, format Disp
if s.ParentSpanID != s.SpanContext.SpanID && s.ParentSpanID.IsValid() {
sp.ParentSpanId = s.ParentSpanID.String()
}
if s.StatusCode != codes.Ok {
sp.Status = &statuspb.Status{Code: int32(s.StatusCode), Message: s.StatusMessage}
}
if s.StatusCode == codes.Ok && s.StatusMessage != "" {
sp.Status = &statuspb.Status{Code: int32(s.StatusCode), Message: s.StatusMessage}
if s.StatusCode == codes.Ok {
sp.Status = &statuspb.Status{Code: int32(codepb.Code_OK)}
} else if s.StatusCode == codes.Unset {
// Don't set status code.
} else if s.StatusCode == codes.Error {
sp.Status = &statuspb.Status{Code: int32(codepb.Code_UNKNOWN), Message: s.StatusMessage}
} else {
sp.Status = &statuspb.Status{Code: int32(codepb.Code_UNKNOWN)}
}

copyAttributes(&sp.Attributes, s.Attributes)
Expand Down

0 comments on commit 8f77e63

Please sign in to comment.