Skip to content

Commit eeb837e

Browse files
authored
zipkinv1 implement directly Unmarshaler interface (#3504)
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent b24db54 commit eeb837e

File tree

5 files changed

+31
-53
lines changed

5 files changed

+31
-53
lines changed

translator/trace/zipkinv1/json.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,31 @@ var (
4040
msgZipkinV1SpanIDError = "zipkinV1 span id"
4141
msgZipkinV1ParentIDError = "zipkinV1 span parentId"
4242
// Generic hex to ID conversion errors
43-
errHexTraceIDWrongLen = errors.New("hex traceId span has wrong length (expected 16 or 32)")
44-
errHexTraceIDParsing = errors.New("failed to parse hex traceId")
45-
errHexTraceIDZero = errors.New("traceId is zero")
46-
errHexIDWrongLen = errors.New("hex Id has wrong length (expected 16)")
47-
errHexIDParsing = errors.New("failed to parse hex Id")
48-
errHexIDZero = errors.New("ID is zero")
49-
_ pdata.TracesDecoder = (*jsonDecoder)(nil)
43+
errHexTraceIDWrongLen = errors.New("hex traceId span has wrong length (expected 16 or 32)")
44+
errHexTraceIDParsing = errors.New("failed to parse hex traceId")
45+
errHexTraceIDZero = errors.New("traceId is zero")
46+
errHexIDWrongLen = errors.New("hex Id has wrong length (expected 16)")
47+
errHexIDParsing = errors.New("failed to parse hex Id")
48+
errHexIDZero = errors.New("ID is zero")
5049
)
5150

52-
type jsonDecoder struct {
51+
type jsonUnmarshaler struct {
5352
// ParseStringTags should be set to true if tags should be converted to numbers when possible.
5453
ParseStringTags bool
5554
}
5655

57-
// DecodeTraces from JSON bytes.
58-
func (j jsonDecoder) DecodeTraces(buf []byte) (interface{}, error) {
59-
return v1JSONBatchToOCProto(buf, j.ParseStringTags)
56+
// UnmarshalTraces from JSON bytes.
57+
func (j jsonUnmarshaler) UnmarshalTraces(buf []byte) (pdata.Traces, error) {
58+
tds, err := v1JSONBatchToOCProto(buf, j.ParseStringTags)
59+
if err != nil {
60+
return pdata.Traces{}, err
61+
}
62+
return toTraces(tds)
6063
}
6164

6265
// NewJSONTracesUnmarshaler returns an unmarshaler for Zipkin JSON.
6366
func NewJSONTracesUnmarshaler(parseStringTags bool) pdata.TracesUnmarshaler {
64-
return pdata.NewTracesUnmarshaler(jsonDecoder{ParseStringTags: parseStringTags}, toTranslator{})
67+
return jsonUnmarshaler{ParseStringTags: parseStringTags}
6568
}
6669

6770
// Trace translation from Zipkin V1 is a bit of special case since there is no model

translator/trace/zipkinv1/json_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,17 @@ func TestSingleJSONV1BatchToTraces(t *testing.T) {
3838
blob, err := ioutil.ReadFile("./testdata/zipkin_v1_single_batch.json")
3939
require.NoError(t, err, "Failed to load test data")
4040

41-
got, err := jsonDecoder{ParseStringTags: false}.DecodeTraces(blob)
41+
td, err := NewJSONTracesUnmarshaler(false).UnmarshalTraces(blob)
4242
require.NoError(t, err, "Failed to translate zipkinv1 to OC proto")
43-
44-
td := got.([]traceData)
45-
spanCount := 0
46-
47-
for _, data := range td {
48-
spanCount += len(data.Spans)
49-
}
50-
51-
assert.Equal(t, 5, spanCount)
43+
assert.Equal(t, 5, td.SpanCount())
5244
}
5345

5446
func TestErrorSpanToTraces(t *testing.T) {
5547
blob, err := ioutil.ReadFile("./testdata/zipkin_v1_error_batch.json")
5648
require.NoError(t, err, "Failed to load test data")
5749

58-
got, err := jsonDecoder{ParseStringTags: false}.DecodeTraces(blob)
50+
_, err = NewJSONTracesUnmarshaler(false).UnmarshalTraces(blob)
5951
assert.Error(t, err, "Should have generated error")
60-
assert.Nil(t, got)
6152
}
6253

6354
func Test_hexIDToOCID(t *testing.T) {

translator/trace/zipkinv1/thrift.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,24 @@ import (
3232
"go.opentelemetry.io/collector/model/pdata"
3333
)
3434

35-
var _ pdata.TracesDecoder = (*thriftDecoder)(nil)
35+
type thriftUnmarshaler struct{}
3636

37-
type thriftDecoder struct{}
38-
39-
// DecodeTraces from Thrift bytes.
40-
func (t thriftDecoder) DecodeTraces(buf []byte) (interface{}, error) {
37+
// UnmarshalTraces from Thrift bytes.
38+
func (t thriftUnmarshaler) UnmarshalTraces(buf []byte) (pdata.Traces, error) {
4139
spans, err := jaegerzipkin.DeserializeThrift(buf)
4240
if err != nil {
43-
return nil, err
41+
return pdata.Traces{}, err
42+
}
43+
tds, err := v1ThriftBatchToOCProto(spans)
44+
if err != nil {
45+
return pdata.Traces{}, err
4446
}
45-
return v1ThriftBatchToOCProto(spans)
47+
return toTraces(tds)
4648
}
4749

4850
// NewThriftTracesUnmarshaler returns an unmarshaler for Zipkin Thrift.
4951
func NewThriftTracesUnmarshaler() pdata.TracesUnmarshaler {
50-
return pdata.NewTracesUnmarshaler(thriftDecoder{}, toTranslator{})
52+
return thriftUnmarshaler{}
5153
}
5254

5355
// v1ThriftBatchToOCProto converts Zipkin v1 spans to OC Proto.

translator/trace/zipkinv1/thrift_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,9 @@ func TestV1ThriftToTraces(t *testing.T) {
5151
require.NoError(t, json.Unmarshal(blob, &zSpans), "failed to unmarshal json test file")
5252
thriftBytes := zipkin.SerializeThrift(zSpans)
5353

54-
got, err := thriftDecoder{}.DecodeTraces(thriftBytes)
54+
td, err := thriftUnmarshaler{}.UnmarshalTraces(thriftBytes)
5555
require.NoError(t, err, "Failed to translate zipkinv1 thrift to OC proto")
56-
57-
td := got.([]traceData)
58-
spanCount := 0
59-
60-
for _, data := range td {
61-
spanCount += len(data.Spans)
62-
}
63-
64-
assert.Equal(t, 5, spanCount)
56+
assert.Equal(t, 5, td.SpanCount())
6557
}
6658

6759
func TestZipkinThriftFallbackToLocalComponent(t *testing.T) {

translator/trace/zipkinv1/to_translator.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,7 @@ import (
1919
"go.opentelemetry.io/collector/translator/internaldata"
2020
)
2121

22-
var _ pdata.ToTracesTranslator = (*toTranslator)(nil)
23-
24-
type toTranslator struct{}
25-
26-
// ToTraces converts converts traceData to pdata.Traces.
27-
func (t toTranslator) ToTraces(src interface{}) (pdata.Traces, error) {
28-
ocTraces, ok := src.([]traceData)
29-
if !ok {
30-
return pdata.Traces{}, pdata.NewErrIncompatibleType([]traceData{}, src)
31-
}
32-
22+
func toTraces(ocTraces []traceData) (pdata.Traces, error) {
3323
td := pdata.NewTraces()
3424

3525
for _, trace := range ocTraces {

0 commit comments

Comments
 (0)