Skip to content

Commit

Permalink
Add Status type to SDK (#1874)
Browse files Browse the repository at this point in the history
Add Status type to SDK

Use this type to encapsulate the Span status similar to the Event type
encapsulating a Span event and the Link type a span link.

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
  • Loading branch information
MrAlias and Aneurysm9 committed May 3, 2021
1 parent f90d0d9 commit b7d02db
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 134 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
| 10 | Out of Range |
| 14 | Unavailable |
| 15 | Data Loss |
- The `Status` type was added to the `go.opentelemetry.io/otel/sdk/trace` package to represent the status of a span. (#1874)

### Changed

Expand All @@ -31,6 +32,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- BatchSpanProcessor now report export failures when calling `ForceFlush()` method. (#1860)
- `Set.Encoded(Encoder)` no longer caches the result of an encoding. (#1855)
- Renamed `CloudZoneKey` to `CloudAvailabilityZoneKey` in Resource semantic conventions according to spec. (#1871)
- The `StatusCode` and `StatusMessage` methods of the `ReadOnlySpan` interface and the `Span` produced by the `go.opentelemetry.io/otel/sdk/trace` package have been replaced with a single `Status` method.
This method returns the status of a span using the new `Status` type. (#1874)

### Deprecated

Expand Down
3 changes: 1 addition & 2 deletions exporters/otlp/internal/otlptest/data.go
Expand Up @@ -99,8 +99,7 @@ func SingleSpanSnapshot() []*tracesdk.SpanSnapshot {
Attributes: []attribute.KeyValue{},
MessageEvents: []tracesdk.Event{},
Links: []trace.Link{},
StatusCode: codes.Ok,
StatusMessage: "",
Status: tracesdk.Status{Code: codes.Ok},
DroppedAttributeCount: 0,
DroppedMessageEventCount: 0,
DroppedLinkCount: 0,
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/internal/transform/span.go
Expand Up @@ -108,7 +108,7 @@ func span(sd *tracesdk.SpanSnapshot) *tracepb.Span {
TraceId: tid[:],
SpanId: sid[:],
TraceState: sd.SpanContext.TraceState().String(),
Status: status(sd.StatusCode, sd.StatusMessage),
Status: status(sd.Status.Code, sd.Status.Description),
StartTimeUnixNano: uint64(sd.StartTime.UnixNano()),
EndTimeUnixNano: uint64(sd.EndTime.UnixNano()),
Links: links(sd.Links),
Expand Down
8 changes: 5 additions & 3 deletions exporters/otlp/internal/transform/span_test.go
Expand Up @@ -249,8 +249,10 @@ func TestSpanData(t *testing.T) {
},
},
},
StatusCode: codes.Error,
StatusMessage: "utterly unrecognized",
Status: tracesdk.Status{
Code: codes.Error,
Description: "utterly unrecognized",
},
Attributes: []attribute.KeyValue{
attribute.Int64("timeout_ns", 12e9),
},
Expand All @@ -276,7 +278,7 @@ func TestSpanData(t *testing.T) {
Kind: tracepb.Span_SPAN_KIND_SERVER,
StartTimeUnixNano: uint64(startTime.UnixNano()),
EndTimeUnixNano: uint64(endTime.UnixNano()),
Status: status(spanData.StatusCode, spanData.StatusMessage),
Status: status(spanData.Status.Code, spanData.Status.Description),
Events: spanEvents(spanData.MessageEvents),
Links: links(spanData.Links),
Attributes: Attributes(spanData.Attributes),
Expand Down
32 changes: 20 additions & 12 deletions exporters/otlp/otlp_span_test.go
Expand Up @@ -68,9 +68,11 @@ func TestExportSpans(t *testing.T) {
attribute.String("user", "alice"),
attribute.Bool("authenticated", true),
},
StatusCode: codes.Ok,
StatusMessage: "Ok",
Resource: resource.NewWithAttributes(attribute.String("instance", "tester-a")),
Status: tracesdk.Status{
Code: codes.Ok,
Description: "Ok",
},
Resource: resource.NewWithAttributes(attribute.String("instance", "tester-a")),
InstrumentationLibrary: instrumentation.Library{
Name: "lib-a",
Version: "v0.1.0",
Expand All @@ -90,9 +92,11 @@ func TestExportSpans(t *testing.T) {
attribute.String("user", "alice"),
attribute.Bool("authenticated", true),
},
StatusCode: codes.Ok,
StatusMessage: "Ok",
Resource: resource.NewWithAttributes(attribute.String("instance", "tester-a")),
Status: tracesdk.Status{
Code: codes.Ok,
Description: "Ok",
},
Resource: resource.NewWithAttributes(attribute.String("instance", "tester-a")),
InstrumentationLibrary: instrumentation.Library{
Name: "lib-b",
Version: "v0.1.0",
Expand All @@ -117,9 +121,11 @@ func TestExportSpans(t *testing.T) {
attribute.String("user", "alice"),
attribute.Bool("authenticated", true),
},
StatusCode: codes.Ok,
StatusMessage: "Ok",
Resource: resource.NewWithAttributes(attribute.String("instance", "tester-a")),
Status: tracesdk.Status{
Code: codes.Ok,
Description: "Ok",
},
Resource: resource.NewWithAttributes(attribute.String("instance", "tester-a")),
InstrumentationLibrary: instrumentation.Library{
Name: "lib-a",
Version: "v0.1.0",
Expand All @@ -139,9 +145,11 @@ func TestExportSpans(t *testing.T) {
attribute.String("user", "bob"),
attribute.Bool("authenticated", false),
},
StatusCode: codes.Error,
StatusMessage: "Unauthenticated",
Resource: resource.NewWithAttributes(attribute.String("instance", "tester-b")),
Status: tracesdk.Status{
Code: codes.Error,
Description: "Unauthenticated",
},
Resource: resource.NewWithAttributes(attribute.String("instance", "tester-b")),
InstrumentationLibrary: instrumentation.Library{
Name: "lib-a",
Version: "v1.1.0",
Expand Down
13 changes: 7 additions & 6 deletions exporters/stdout/trace_test.go
Expand Up @@ -64,10 +64,12 @@ func TestExporter_ExportSpan(t *testing.T) {
{Name: "foo", Attributes: []attribute.KeyValue{attribute.String("key", keyValue)}, Time: now},
{Name: "bar", Attributes: []attribute.KeyValue{attribute.Float64("double", doubleValue)}, Time: now},
},
SpanKind: trace.SpanKindInternal,
StatusCode: codes.Error,
StatusMessage: "interesting",
Resource: resource,
SpanKind: trace.SpanKindInternal,
Status: tracesdk.Status{
Code: codes.Error,
Description: "interesting",
},
Resource: resource,
}
if err := ex.ExportSpans(context.Background(), []*tracesdk.SpanSnapshot{testSpan}); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -129,8 +131,7 @@ func TestExporter_ExportSpan(t *testing.T) {
`}` +
`],` +
`"Links":null,` +
`"StatusCode":"Error",` +
`"StatusMessage":"interesting",` +
`"Status":{"Code":"Error","Description":"interesting"},` +
`"DroppedAttributeCount":0,` +
`"DroppedMessageEventCount":0,` +
`"DroppedLinkCount":0,` +
Expand Down
10 changes: 5 additions & 5 deletions exporters/trace/jaeger/jaeger.go
Expand Up @@ -170,13 +170,13 @@ func spanSnapshotToThrift(ss *sdktrace.SpanSnapshot) *gen.Span {
)
}

if ss.StatusCode != codes.Unset {
tags = append(tags, getInt64Tag(keyStatusCode, int64(ss.StatusCode)))
if ss.StatusMessage != "" {
tags = append(tags, getStringTag(keyStatusMessage, ss.StatusMessage))
if ss.Status.Code != codes.Unset {
tags = append(tags, getInt64Tag(keyStatusCode, int64(ss.Status.Code)))
if ss.Status.Description != "" {
tags = append(tags, getStringTag(keyStatusMessage, ss.Status.Description))
}

if ss.StatusCode == codes.Error {
if ss.Status.Code == codes.Error {
tags = append(tags, getBoolTag(keyError, true))
}
}
Expand Down
34 changes: 20 additions & 14 deletions exporters/trace/jaeger/jaeger_test.go
Expand Up @@ -229,11 +229,11 @@ func Test_spanSnapshotToThrift(t *testing.T) {
TraceID: traceID,
SpanID: spanID,
}),
Name: "/foo",
StartTime: now,
EndTime: now,
StatusCode: codes.Error,
SpanKind: trace.SpanKindClient,
Name: "/foo",
StartTime: now,
EndTime: now,
Status: sdktrace.Status{Code: codes.Error},
SpanKind: trace.SpanKindClient,
InstrumentationLibrary: instrumentation.Library{
Name: instrLibName,
Version: instrLibVersion,
Expand Down Expand Up @@ -287,9 +287,11 @@ func Test_spanSnapshotToThrift(t *testing.T) {
Time: now,
},
},
StatusCode: codes.Error,
StatusMessage: statusMessage,
SpanKind: trace.SpanKindClient,
Status: sdktrace.Status{
Code: codes.Error,
Description: statusMessage,
},
SpanKind: trace.SpanKindClient,
InstrumentationLibrary: instrumentation.Library{
Name: instrLibName,
Version: instrLibVersion,
Expand Down Expand Up @@ -370,9 +372,11 @@ func Test_spanSnapshotToThrift(t *testing.T) {
Attributes: []attribute.KeyValue{
attribute.Array("arr", []int{0, 1, 2, 3}),
},
StatusCode: codes.Unset,
StatusMessage: statusMessage,
SpanKind: trace.SpanKindInternal,
Status: sdktrace.Status{
Code: codes.Unset,
Description: statusMessage,
},
SpanKind: trace.SpanKindInternal,
InstrumentationLibrary: instrumentation.Library{
Name: instrLibName,
Version: instrLibVersion,
Expand Down Expand Up @@ -421,9 +425,11 @@ func Test_spanSnapshotToThrift(t *testing.T) {
attribute.Int64("rk2", rv2),
semconv.ServiceNameKey.String("service name"),
),
StatusCode: codes.Unset,
StatusMessage: statusMessage,
SpanKind: trace.SpanKindInternal,
Status: sdktrace.Status{
Code: codes.Unset,
Description: statusMessage,
},
SpanKind: trace.SpanKindInternal,
InstrumentationLibrary: instrumentation.Library{
Name: instrLibName,
Version: instrLibVersion,
Expand Down
8 changes: 4 additions & 4 deletions exporters/trace/zipkin/model.go
Expand Up @@ -187,12 +187,12 @@ func toZipkinTags(data *tracesdk.SpanSnapshot) map[string]string {
}
}

if data.StatusCode != codes.Unset {
m["otel.status_code"] = data.StatusCode.String()
if data.Status.Code != codes.Unset {
m["otel.status_code"] = data.Status.Code.String()
}

if data.StatusCode == codes.Error {
m["error"] = data.StatusMessage
if data.Status.Code == codes.Error {
m["error"] = data.Status.Description
} else {
delete(m, "error")
}
Expand Down
73 changes: 45 additions & 28 deletions exporters/trace/zipkin/model_test.go
Expand Up @@ -74,9 +74,11 @@ func TestModelConversion(t *testing.T) {
Attributes: nil,
},
},
StatusCode: codes.Error,
StatusMessage: "404, file not found",
Resource: resource,
Status: tracesdk.Status{
Code: codes.Error,
Description: "404, file not found",
},
Resource: resource,
},
// span data with no parent (same as typical, but has
// invalid parent)
Expand Down Expand Up @@ -107,9 +109,11 @@ func TestModelConversion(t *testing.T) {
Attributes: nil,
},
},
StatusCode: codes.Error,
StatusMessage: "404, file not found",
Resource: resource,
Status: tracesdk.Status{
Code: codes.Error,
Description: "404, file not found",
},
Resource: resource,
},
// span data of unspecified kind
{
Expand Down Expand Up @@ -143,9 +147,11 @@ func TestModelConversion(t *testing.T) {
Attributes: nil,
},
},
StatusCode: codes.Error,
StatusMessage: "404, file not found",
Resource: resource,
Status: tracesdk.Status{
Code: codes.Error,
Description: "404, file not found",
},
Resource: resource,
},
// span data of internal kind
{
Expand Down Expand Up @@ -179,9 +185,11 @@ func TestModelConversion(t *testing.T) {
Attributes: nil,
},
},
StatusCode: codes.Error,
StatusMessage: "404, file not found",
Resource: resource,
Status: tracesdk.Status{
Code: codes.Error,
Description: "404, file not found",
},
Resource: resource,
},
// span data of client kind
{
Expand Down Expand Up @@ -218,9 +226,11 @@ func TestModelConversion(t *testing.T) {
Attributes: nil,
},
},
StatusCode: codes.Error,
StatusMessage: "404, file not found",
Resource: resource,
Status: tracesdk.Status{
Code: codes.Error,
Description: "404, file not found",
},
Resource: resource,
},
// span data of producer kind
{
Expand Down Expand Up @@ -254,9 +264,11 @@ func TestModelConversion(t *testing.T) {
Attributes: nil,
},
},
StatusCode: codes.Error,
StatusMessage: "404, file not found",
Resource: resource,
Status: tracesdk.Status{
Code: codes.Error,
Description: "404, file not found",
},
Resource: resource,
},
// span data of consumer kind
{
Expand Down Expand Up @@ -290,9 +302,11 @@ func TestModelConversion(t *testing.T) {
Attributes: nil,
},
},
StatusCode: codes.Error,
StatusMessage: "404, file not found",
Resource: resource,
Status: tracesdk.Status{
Code: codes.Error,
Description: "404, file not found",
},
Resource: resource,
},
// span data with no events
{
Expand All @@ -313,9 +327,11 @@ func TestModelConversion(t *testing.T) {
attribute.String("attr2", "bar"),
},
MessageEvents: nil,
StatusCode: codes.Error,
StatusMessage: "404, file not found",
Resource: resource,
Status: tracesdk.Status{
Code: codes.Error,
Description: "404, file not found",
},
Resource: resource,
},
// span data with an "error" attribute set to "false"
{
Expand Down Expand Up @@ -348,8 +364,7 @@ func TestModelConversion(t *testing.T) {
Attributes: nil,
},
},
StatusCode: codes.Unset,
Resource: resource,
Resource: resource,
},
}

Expand Down Expand Up @@ -759,8 +774,10 @@ func TestTagsTransformation(t *testing.T) {
attribute.String("key", keyValue),
attribute.Bool("error", true),
},
StatusCode: codes.Error,
StatusMessage: statusMessage,
Status: tracesdk.Status{
Code: codes.Error,
Description: statusMessage,
},
},
want: map[string]string{
"error": statusMessage,
Expand Down

0 comments on commit b7d02db

Please sign in to comment.