Skip to content

Commit 202ece9

Browse files
authored
Merge pull request #26 from zalando/fix_error_attribute
Fix error attribute parsing and remove early returns
2 parents 3bcf8e5 + 52b9227 commit 202ece9

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
lines changed

internal/lightstep_common/common.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package lightstep_common
22

33
import (
44
"errors"
5+
6+
"go.opentelemetry.io/collector/pdata/pcommon"
57
"go.opentelemetry.io/collector/pdata/ptrace"
68
)
79

@@ -25,3 +27,14 @@ type ProjectTraces struct {
2527
type OtelTransformer interface {
2628
ToOtel() (*ProjectTraces, error)
2729
}
30+
31+
func IsErrorAttributeValueActuallyError(value pcommon.Value) bool {
32+
switch value.Type() {
33+
case pcommon.ValueTypeBool:
34+
return value.Bool()
35+
case pcommon.ValueTypeStr:
36+
return value.AsString() == "true"
37+
default:
38+
return false
39+
}
40+
}

internal/lightstep_pb/transform.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package lightstep_pb
33
import (
44
"context"
55
"encoding/binary"
6+
"strings"
7+
"time"
8+
"unicode/utf8"
9+
610
lightstepConstants "github.com/lightstep/lightstep-tracer-go/constants"
711
"go.opentelemetry.io/collector/pdata/pcommon"
812
"go.opentelemetry.io/collector/pdata/ptrace"
913
"go.opentelemetry.io/otel/codes"
1014
"go.uber.org/zap"
11-
"strings"
12-
"time"
13-
"unicode/utf8"
1415

1516
lightstepCommon "github.com/zalando/otelcol-lightstep-receiver/internal/lightstep_common"
1617
pb "github.com/zalando/otelcol-lightstep-receiver/internal/lightstep_pb/collectorpb"
@@ -54,26 +55,27 @@ func (r *Request) ToOtel(ctx context.Context) (*lightstepCommon.ProjectTraces, e
5455

5556
if r.orig.Auth == nil || r.orig.Auth.AccessToken == "" {
5657
span.SetStatus(codes.Error, lightstepCommon.ErrNoAccessToken.Error())
57-
return result, lightstepCommon.ErrNoAccessToken
58+
} else {
59+
result.AccessToken = r.orig.Auth.AccessToken
5860
}
59-
result.AccessToken = r.orig.Auth.AccessToken
6061

6162
data := ptrace.NewTraces()
6263
rs := data.ResourceSpans().AppendEmpty()
6364
rAttr := rs.Resource().Attributes()
6465

6566
nonUtf8Keys, err := r.kvToAttr(r.orig.Reporter.Tags, &rAttr)
66-
serviceName, ok := rAttr.Get(lightstepConstants.ComponentNameKey)
67-
result.ServiceName = serviceName.Str()
68-
if !ok {
69-
return result, lightstepCommon.ErrNoServiceName
70-
}
7167
if err != nil {
7268
span.SetStatus(codes.Error, "non-utf8-keys")
7369
r.reportNonUtf8(result.ServiceName, nonUtf8Keys)
7470
}
7571

76-
rAttr.PutStr("service.name", serviceName.Str())
72+
serviceName, ok := rAttr.Get(lightstepConstants.ComponentNameKey)
73+
if ok {
74+
result.ServiceName = serviceName.Str()
75+
rAttr.PutStr("service.name", serviceName.Str())
76+
} else {
77+
span.SetStatus(codes.Error, lightstepCommon.ErrNoServiceName.Error())
78+
}
7779

7880
if r.orig.InternalMetrics != nil {
7981
for _, m := range r.orig.InternalMetrics.Counts {
@@ -106,9 +108,11 @@ func (r *Request) ToOtel(ctx context.Context) (*lightstepCommon.ProjectTraces, e
106108
r.reportNonUtf8(result.ServiceName, nonUtf8Keys)
107109
}
108110

109-
if _, ok := attr.Get("error"); ok {
110-
s.Status().SetCode(ptrace.StatusCodeError)
111-
attr.Remove("error")
111+
if value, ok := attr.Get("error"); ok {
112+
if lightstepCommon.IsErrorAttributeValueActuallyError(value) {
113+
s.Status().SetCode(ptrace.StatusCodeError)
114+
attr.Remove("error")
115+
}
112116
}
113117

114118
for _, log := range span.Logs {

internal/lightstep_pb/transform_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package lightstep_pb
22

33
import (
44
"context"
5+
"testing"
6+
57
"github.com/golang/protobuf/ptypes/timestamp"
68
"github.com/matryer/is"
79
"go.opentelemetry.io/collector/component"
@@ -10,9 +12,7 @@ import (
1012
"go.opentelemetry.io/collector/receiver"
1113
"go.opentelemetry.io/otel/trace/noop"
1214
"go.uber.org/zap"
13-
"testing"
1415

15-
lightstepCommon "github.com/zalando/otelcol-lightstep-receiver/internal/lightstep_common"
1616
pb "github.com/zalando/otelcol-lightstep-receiver/internal/lightstep_pb/collectorpb"
1717
"github.com/zalando/otelcol-lightstep-receiver/internal/telemetry"
1818
)
@@ -181,7 +181,7 @@ func TestTransformation_NoServiceName(t *testing.T) {
181181
telemetry: initTelemetry(),
182182
}
183183
_, err := rq.ToOtel(context.Background())
184-
is.Equal(err, lightstepCommon.ErrNoServiceName)
184+
is.Equal(err, nil)
185185
}
186186

187187
func TestTransformation_NoAccessToken(t *testing.T) {
@@ -198,7 +198,7 @@ func TestTransformation_NoAccessToken(t *testing.T) {
198198
telemetry: initTelemetry(),
199199
}
200200
_, err := rq.ToOtel(context.Background())
201-
is.Equal(err, lightstepCommon.ErrNoAccessToken)
201+
is.Equal(err, nil)
202202
}
203203

204204
func TestTransformation_GetClientDropSpans(t *testing.T) {

internal/lightstep_thrift/transform.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package lightstep_thrift
33
import (
44
"context"
55
"encoding/hex"
6+
"strings"
7+
"time"
8+
69
lightstepConstants "github.com/lightstep/lightstep-tracer-go/constants"
710
"go.opentelemetry.io/collector/pdata/pcommon"
811
"go.opentelemetry.io/collector/pdata/ptrace"
912
"go.opentelemetry.io/otel/codes"
1013
"go.uber.org/zap"
11-
"strings"
12-
"time"
1314

1415
lightstepCommon "github.com/zalando/otelcol-lightstep-receiver/internal/lightstep_common"
1516
"github.com/zalando/otelcol-lightstep-receiver/internal/lightstep_thrift/collectorthrift"
@@ -47,22 +48,23 @@ func (tr *Request) ToOtel(ctx context.Context) (*lightstepCommon.ProjectTraces,
4748
result := &lightstepCommon.ProjectTraces{}
4849

4950
if tr.auth == nil || tr.auth.AccessToken == nil {
50-
return result, lightstepCommon.ErrNoAccessToken
51+
span.SetStatus(codes.Error, lightstepCommon.ErrNoAccessToken.Error())
52+
} else {
53+
result.AccessToken = tr.auth.GetAccessToken()
5154
}
52-
result.AccessToken = tr.auth.GetAccessToken()
5355

5456
data := ptrace.NewTraces()
5557
rs := data.ResourceSpans().AppendEmpty()
5658
rAttr := rs.Resource().Attributes()
5759

5860
tr.kvToAttr(tr.orig.Runtime.Attrs, &rAttr)
5961
serviceName, ok := rAttr.Get(lightstepConstants.ComponentNameKey)
60-
result.ServiceName = serviceName.Str()
61-
if !ok {
62+
if ok {
63+
result.ServiceName = serviceName.Str()
64+
rAttr.PutStr("service.name", serviceName.Str())
65+
} else {
6266
span.SetStatus(codes.Error, lightstepCommon.ErrNoServiceName.Error())
63-
return result, lightstepCommon.ErrNoServiceName
6467
}
65-
rAttr.PutStr("service.name", serviceName.Str())
6668

6769
if tr.orig.InternalMetrics != nil {
6870
for _, m := range tr.orig.InternalMetrics.Counts {
@@ -92,9 +94,11 @@ func (tr *Request) ToOtel(ctx context.Context) (*lightstepCommon.ProjectTraces,
9294
attr.Remove("parent_span_guid")
9395
}
9496

95-
if _, ok := attr.Get("error"); ok {
96-
s.Status().SetCode(ptrace.StatusCodeError)
97-
attr.Remove("error")
97+
if value, ok := attr.Get("error"); ok {
98+
if lightstepCommon.IsErrorAttributeValueActuallyError(value) {
99+
s.Status().SetCode(ptrace.StatusCodeError)
100+
attr.Remove("error")
101+
}
98102
}
99103

100104
for _, log := range span.LogRecords {

0 commit comments

Comments
 (0)