Skip to content

Commit

Permalink
Remove duplicate code in zipkinv2 marshaler (#3784)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Aug 9, 2021
1 parent 9184f1e commit a00190f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
18 changes: 3 additions & 15 deletions translator/trace/zipkinv2/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,14 @@ func (j jsonUnmarshaler) UnmarshalTraces(buf []byte) (pdata.Traces, error) {
return j.toTranslator.ToTraces(spans)
}

type jsonMarshaler struct {
serializer zipkinreporter.JSONSerializer
fromTranslator FromTranslator
}

// MarshalTraces to JSON bytes.
func (j jsonMarshaler) MarshalTraces(td pdata.Traces) ([]byte, error) {
spans, err := j.fromTranslator.FromTraces(td)
if err != nil {
return nil, err
}
return j.serializer.Serialize(spans)
}

// NewJSONTracesUnmarshaler returns an unmarshaler for JSON bytes.
func NewJSONTracesUnmarshaler(parseStringTags bool) pdata.TracesUnmarshaler {
return jsonUnmarshaler{toTranslator: ToTranslator{ParseStringTags: parseStringTags}}
}

// NewJSONTracesMarshaler returns a marshaler to JSON bytes.
func NewJSONTracesMarshaler() pdata.TracesMarshaler {
return jsonMarshaler{}
return marshaler{
serializer: zipkinreporter.JSONSerializer{},
}
}
35 changes: 35 additions & 0 deletions translator/trace/zipkinv2/marshaler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package zipkinv2

import (
zipkinreporter "github.com/openzipkin/zipkin-go/reporter"

"go.opentelemetry.io/collector/model/pdata"
)

type marshaler struct {
serializer zipkinreporter.SpanSerializer
fromTranslator FromTranslator
}

// MarshalTraces to JSON bytes.
func (j marshaler) MarshalTraces(td pdata.Traces) ([]byte, error) {
spans, err := j.fromTranslator.FromTraces(td)
if err != nil {
return nil, err
}
return j.serializer.Serialize(spans)
}
18 changes: 3 additions & 15 deletions translator/trace/zipkinv2/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ func (p protobufUnmarshaler) UnmarshalTraces(buf []byte) (pdata.Traces, error) {
return p.toTranslator.ToTraces(spans)
}

type protobufMarshaler struct {
serializer zipkin_proto3.SpanSerializer
fromTranslator FromTranslator
}

// MarshalTraces to protobuf bytes.
func (p protobufMarshaler) MarshalTraces(td pdata.Traces) ([]byte, error) {
spans, err := p.fromTranslator.FromTraces(td)
if err != nil {
return nil, err
}
return p.serializer.Serialize(spans)
}

// NewProtobufTracesUnmarshaler returns an pdata.TracesUnmarshaler of protobuf bytes.
func NewProtobufTracesUnmarshaler(debugWasSet, parseStringTags bool) pdata.TracesUnmarshaler {
return protobufUnmarshaler{
Expand All @@ -61,5 +47,7 @@ func NewProtobufTracesUnmarshaler(debugWasSet, parseStringTags bool) pdata.Trace

// NewProtobufTracesMarshaler returns a new pdata.TracesMarshaler to protobuf bytes.
func NewProtobufTracesMarshaler() pdata.TracesMarshaler {
return protobufMarshaler{}
return marshaler{
serializer: zipkin_proto3.SpanSerializer{},
}
}

0 comments on commit a00190f

Please sign in to comment.