diff --git a/internal/otlp/json_decoder.go b/internal/otlp/json_decoder.go index 7f0b5429de5..e691d6ecbf9 100644 --- a/internal/otlp/json_decoder.go +++ b/internal/otlp/json_decoder.go @@ -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 } 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 } diff --git a/internal/otlp/json_encoder.go b/internal/otlp/json_encoder.go index 6fbe8427047..e2b06573582 100644 --- a/internal/otlp/json_encoder.go +++ b/internal/otlp/json_encoder.go @@ -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) { @@ -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) { @@ -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 } diff --git a/internal/otlp/json_test.go b/internal/otlp/json_test.go new file mode 100644 index 00000000000..b4365f32e34 --- /dev/null +++ b/internal/otlp/json_test.go @@ -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":{}}]}]}]}` + +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)) +}