-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zipkinv2: add encoders, decoders, marshalers
Also update zipkin receiver to use them.
- Loading branch information
Showing
9 changed files
with
320 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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 ( | ||
"encoding/json" | ||
|
||
zipkinmodel "github.com/openzipkin/zipkin-go/model" | ||
zipkinreporter "github.com/openzipkin/zipkin-go/reporter" | ||
|
||
"go.opentelemetry.io/collector/internal/model" | ||
) | ||
|
||
var _ model.TracesDecoder = (*jsonDecoder)(nil) | ||
|
||
type jsonDecoder struct{} | ||
|
||
// DecodeTraces from JSON bytes to zipkin model. | ||
func (j jsonDecoder) DecodeTraces(buf []byte) (interface{}, error) { | ||
var spans []*zipkinmodel.SpanModel | ||
if err := json.Unmarshal(buf, &spans); err != nil { | ||
return nil, err | ||
} | ||
return spans, nil | ||
} | ||
|
||
var _ model.TracesEncoder = (*jsonEncoder)(nil) | ||
|
||
type jsonEncoder struct { | ||
serializer zipkinreporter.JSONSerializer | ||
} | ||
|
||
// EncodeTraces from zipkin model to bytes. | ||
func (j jsonEncoder) EncodeTraces(mod interface{}) ([]byte, error) { | ||
spans, ok := mod.([]*zipkinmodel.SpanModel) | ||
if !ok { | ||
return nil, model.NewErrIncompatibleType([]*zipkinmodel.SpanModel{}, mod) | ||
} | ||
return j.serializer.Serialize(spans) | ||
} | ||
|
||
// NewJSONTracesUnmarshaler returns an unmarshaler for JSON bytes. | ||
func NewJSONTracesUnmarshaler(parseStringTags bool) model.TracesUnmarshaler { | ||
return model.NewTracesUnmarshaler(jsonDecoder{}, ToTranslator{ParseStringTags: parseStringTags}) | ||
} | ||
|
||
// NewJSONTracesMarshaler returns a marshaler to JSON bytes. | ||
func NewJSONTracesMarshaler() model.TracesMarshaler { | ||
return model.NewTracesMarshaler(jsonEncoder{}, FromTranslator{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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 ( | ||
"io/ioutil" | ||
"testing" | ||
|
||
zipkinmodel "github.com/openzipkin/zipkin-go/model" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/internal/model" | ||
) | ||
|
||
func TestJSONDecoder_DecodeTraces(t *testing.T) { | ||
data, err := ioutil.ReadFile("testdata/zipkin_v2_single.json") | ||
require.NoError(t, err) | ||
decoder := jsonDecoder{} | ||
spans, err := decoder.DecodeTraces(data) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, spans) | ||
assert.IsType(t, []*zipkinmodel.SpanModel{}, spans) | ||
} | ||
|
||
func TestJSONDecoder_DecodeTracesError(t *testing.T) { | ||
decoder := jsonDecoder{} | ||
spans, err := decoder.DecodeTraces([]byte("{")) | ||
assert.Error(t, err) | ||
assert.Nil(t, spans) | ||
} | ||
|
||
func TestJSONEncoder_EncodeTraces(t *testing.T) { | ||
encoder := jsonEncoder{} | ||
buf, err := encoder.EncodeTraces(generateSpanErrorTags()) | ||
assert.NoError(t, err) | ||
assert.Greater(t, len(buf), 1) | ||
} | ||
|
||
func TestJSONEncoder_EncodeTracesError(t *testing.T) { | ||
encoder := jsonEncoder{} | ||
buf, err := encoder.EncodeTraces(nil) | ||
assert.Error(t, err) | ||
assert.Nil(t, buf) | ||
} | ||
|
||
func TestNewJSONTracesUnmarshaler(t *testing.T) { | ||
m := NewJSONTracesUnmarshaler(false) | ||
assert.NotNil(t, m) | ||
assert.Implements(t, (*model.TracesUnmarshaler)(nil), m) | ||
} | ||
|
||
func TestNewJSONTracesMarshaler(t *testing.T) { | ||
m := NewJSONTracesMarshaler() | ||
assert.NotNil(t, m) | ||
assert.Implements(t, (*model.TracesMarshaler)(nil), m) | ||
} |
Oops, something went wrong.