Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for OTLP JSON encode/decode #3400

Merged
merged 1 commit into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions internal/otlp/json_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,18 @@ func newJSONDecoder() *jsonDecoder {

func (d *jsonDecoder) DecodeLogs(buf []byte) (interface{}, error) {
ld := &otlpcollectorlogs.ExportLogsServiceRequest{}
if err := d.delegate.Unmarshal(bytes.NewReader(buf), ld); err != nil {
return nil, err
}
return ld, nil
err := d.delegate.Unmarshal(bytes.NewReader(buf), ld)
return ld, err
Comment on lines +37 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not totally equivalent to the old code but I guess it is fine to return non-nil with an error. Should this behavior be documented in the DecodeLogs interface definition?

}

func (d *jsonDecoder) DecodeMetrics(buf []byte) (interface{}, error) {
md := &otlpcollectormetrics.ExportMetricsServiceRequest{}
if err := d.delegate.Unmarshal(bytes.NewReader(buf), md); err != nil {
return nil, err
}
return md, nil
err := d.delegate.Unmarshal(bytes.NewReader(buf), md)
return md, err
}

func (d *jsonDecoder) DecodeTraces(buf []byte) (interface{}, error) {
td := &otlpcollectortrace.ExportTraceServiceRequest{}
if err := d.delegate.Unmarshal(bytes.NewReader(buf), td); err != nil {
return nil, err
}
return td, nil
err := d.delegate.Unmarshal(bytes.NewReader(buf), td)
return td, err
}
18 changes: 6 additions & 12 deletions internal/otlp/json_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ func (e *jsonEncoder) EncodeLogs(modelData interface{}) ([]byte, error) {
return nil, model.NewErrIncompatibleType(&otlpcollectorlogs.ExportLogsServiceRequest{}, modelData)
}
buf := bytes.Buffer{}
if err := e.delegate.Marshal(&buf, ld); err != nil {
return nil, err
}
return buf.Bytes(), nil
err := e.delegate.Marshal(&buf, ld)
return buf.Bytes(), err
}

func (e *jsonEncoder) EncodeMetrics(modelData interface{}) ([]byte, error) {
Expand All @@ -51,10 +49,8 @@ func (e *jsonEncoder) EncodeMetrics(modelData interface{}) ([]byte, error) {
return nil, model.NewErrIncompatibleType(&otlpcollectormetrics.ExportMetricsServiceRequest{}, modelData)
}
buf := bytes.Buffer{}
if err := e.delegate.Marshal(&buf, md); err != nil {
return nil, err
}
return buf.Bytes(), nil
err := e.delegate.Marshal(&buf, md)
return buf.Bytes(), err
}

func (e *jsonEncoder) EncodeTraces(modelData interface{}) ([]byte, error) {
Expand All @@ -63,8 +59,6 @@ func (e *jsonEncoder) EncodeTraces(modelData interface{}) ([]byte, error) {
return nil, model.NewErrIncompatibleType(&otlpcollectortrace.ExportTraceServiceRequest{}, modelData)
}
buf := bytes.Buffer{}
if err := e.delegate.Marshal(&buf, td); err != nil {
return nil, err
}
return buf.Bytes(), nil
err := e.delegate.Marshal(&buf, td)
return buf.Bytes(), err
}
200 changes: 200 additions & 0 deletions internal/otlp/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// 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 otlp

import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/collector/consumer/pdata"
otlpcollectorlogs "go.opentelemetry.io/collector/internal/data/protogen/collector/logs/v1"
otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1"
otlpcollectortrace "go.opentelemetry.io/collector/internal/data/protogen/collector/trace/v1"
otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1"
otlplogs "go.opentelemetry.io/collector/internal/data/protogen/logs/v1"
otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1"
otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1"
otlptrace "go.opentelemetry.io/collector/internal/data/protogen/trace/v1"
"go.opentelemetry.io/collector/translator/conventions"
)

var tracesOTLP = &otlpcollectortrace.ExportTraceServiceRequest{
ResourceSpans: []*otlptrace.ResourceSpans{
{
Resource: otlpresource.Resource{
Attributes: []otlpcommon.KeyValue{
{
Key: conventions.AttributeHostName,
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "testHost"}},
},
},
},
InstrumentationLibrarySpans: []*otlptrace.InstrumentationLibrarySpans{
{
InstrumentationLibrary: otlpcommon.InstrumentationLibrary{
Name: "name",
Version: "version",
},
Spans: []*otlptrace.Span{
{
Name: "testSpan",
},
},
},
},
},
},
}

var tracesJSON = `{"resourceSpans":[{"resource":{"attributes":[{"key":"host.name","value":{"stringValue":"testHost"}}]},"instrumentationLibrarySpans":[{"instrumentationLibrary":{"name":"name","version":"version"},"spans":[{"traceId":"","spanId":"","parentSpanId":"","name":"testSpan","status":{}}]}]}]}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, pity it has to be a single line. We cannot use pretty-printed version here?


var metricsOTLP = &otlpcollectormetrics.ExportMetricsServiceRequest{
ResourceMetrics: []*otlpmetrics.ResourceMetrics{
{
Resource: otlpresource.Resource{
Attributes: []otlpcommon.KeyValue{
{
Key: conventions.AttributeHostName,
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "testHost"}},
},
},
},
InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{
{
InstrumentationLibrary: otlpcommon.InstrumentationLibrary{
Name: "name",
Version: "version",
},
Metrics: []*otlpmetrics.Metric{
{
Name: "testMetric",
},
},
},
},
},
},
}

var metricsJSON = `{"resourceMetrics":[{"resource":{"attributes":[{"key":"host.name","value":{"stringValue":"testHost"}}]},"instrumentationLibraryMetrics":[{"instrumentationLibrary":{"name":"name","version":"version"},"metrics":[{"name":"testMetric"}]}]}]}`

var logsOTLP = &otlpcollectorlogs.ExportLogsServiceRequest{
ResourceLogs: []*otlplogs.ResourceLogs{
{
Resource: otlpresource.Resource{
Attributes: []otlpcommon.KeyValue{
{
Key: conventions.AttributeHostName,
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "testHost"}},
},
},
},
InstrumentationLibraryLogs: []*otlplogs.InstrumentationLibraryLogs{
{
InstrumentationLibrary: otlpcommon.InstrumentationLibrary{
Name: "name",
Version: "version",
},
Logs: []*otlplogs.LogRecord{
{
Name: "testMetric",
},
},
},
},
},
},
}

var logsJSON = `{"resourceLogs":[{"resource":{"attributes":[{"key":"host.name","value":{"stringValue":"testHost"}}]},"instrumentationLibraryLogs":[{"instrumentationLibrary":{"name":"name","version":"version"},"logs":[{"name":"testMetric","body":{},"traceId":"","spanId":""}]}]}]}`

func TestTracesJSON(t *testing.T) {
encoder := newJSONEncoder()
jsonBuf, err := encoder.EncodeTraces(tracesOTLP)
assert.NoError(t, err)

decoder := newJSONDecoder()
var got interface{}
got, err = decoder.DecodeTraces(jsonBuf)
assert.NoError(t, err)

assert.EqualValues(t, tracesOTLP, got)
}

func TestMetricsJSON(t *testing.T) {
encoder := newJSONEncoder()
jsonBuf, err := encoder.EncodeMetrics(metricsOTLP)
assert.NoError(t, err)

decoder := newJSONDecoder()
var got interface{}
got, err = decoder.DecodeMetrics(jsonBuf)
assert.NoError(t, err)

assert.EqualValues(t, metricsOTLP, got)
}

func TestLogsJSON(t *testing.T) {
encoder := newJSONEncoder()
jsonBuf, err := encoder.EncodeLogs(logsOTLP)
assert.NoError(t, err)

decoder := newJSONDecoder()
var got interface{}
got, err = decoder.DecodeLogs(jsonBuf)
assert.NoError(t, err)

assert.EqualValues(t, logsOTLP, got)
}

func TestTracesJSON_InvalidInterface(t *testing.T) {
encoder := newJSONEncoder()
_, err := encoder.EncodeTraces(pdata.NewTraces())
assert.Error(t, err)
}

func TestMetricsJSON_InvalidInterface(t *testing.T) {
encoder := newJSONEncoder()
_, err := encoder.EncodeMetrics(pdata.NewMetrics())
assert.Error(t, err)
}

func TestLogsJSON_InvalidInterface(t *testing.T) {
encoder := newJSONEncoder()
_, err := encoder.EncodeLogs(pdata.NewLogs())
assert.Error(t, err)
}

func TestTracesJSON_Encode(t *testing.T) {
encoder := newJSONEncoder()
jsonBuf, err := encoder.EncodeTraces(tracesOTLP)
assert.NoError(t, err)
assert.Equal(t, tracesJSON, string(jsonBuf))
}

func TestMetricsJSON_Encode(t *testing.T) {
encoder := newJSONEncoder()
jsonBuf, err := encoder.EncodeMetrics(metricsOTLP)
assert.NoError(t, err)
assert.Equal(t, metricsJSON, string(jsonBuf))
}

func TestLogsJSON_Encode(t *testing.T) {
encoder := newJSONEncoder()
jsonBuf, err := encoder.EncodeLogs(logsOTLP)
assert.NoError(t, err)
assert.Equal(t, logsJSON, string(jsonBuf))
}