From 985605f119a7521a8fca8713649be24ab2cce388 Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Fri, 14 May 2021 19:57:39 -0400 Subject: [PATCH 01/15] [wip] Introduce model translation and encodings interfaces This is a somewhat more limited version of #3044. It decouples translation of models from encodings. Models refers to the in-memory representation of a protcol like the zipkin v2 SpanModel. After translating from pdata to the model an encoding is used to serialize the model to a particular byte representation (protobuf, JSON, etc.). The reverse also applies in deserializing an encoding of bytes to a model then translating the model to pdata. --- protocols/encodings/decoder.go | 35 ++++++++++++++++ protocols/encodings/encoder.go | 32 ++++++++++++++ protocols/encodings/encodings.go | 38 +++++++++++++++++ protocols/models/models.go | 51 +++++++++++++++++++++++ protocols/zipkinv2/encodings.go | 71 ++++++++++++++++++++++++++++++++ protocols/zipkinv2/models.go | 50 ++++++++++++++++++++++ 6 files changed, 277 insertions(+) create mode 100644 protocols/encodings/decoder.go create mode 100644 protocols/encodings/encoder.go create mode 100644 protocols/encodings/encodings.go create mode 100644 protocols/models/models.go create mode 100644 protocols/zipkinv2/encodings.go create mode 100644 protocols/zipkinv2/models.go diff --git a/protocols/encodings/decoder.go b/protocols/encodings/decoder.go new file mode 100644 index 00000000000..1f31d005cec --- /dev/null +++ b/protocols/encodings/decoder.go @@ -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 encodings + +import "go.opentelemetry.io/collector/consumer/pdata" + +// MetricsDecoder converts another protocol to pdata. +type MetricsDecoder interface { + // UnmarshalMetrics converts data in another protocol to pdata. + UnmarshalMetrics(bytes []byte) (pdata.Metrics, error) +} + +// TracesDecoder converts another type of data to pdata. +type TracesDecoder interface { + // UnmarshalTraces converts data in another protocol to pdata. + UnmarshalTraces(bytes []byte) (pdata.Traces, error) +} + +// LogsDecoder converts another type of data to pdata. +type LogsDecoder interface { + // UnmarshalLogs converts a data model of another protocol into pdata. + UnmarshalLogs(bytes []byte) (pdata.Logs, error) +} diff --git a/protocols/encodings/encoder.go b/protocols/encodings/encoder.go new file mode 100644 index 00000000000..3eda97ea93e --- /dev/null +++ b/protocols/encodings/encoder.go @@ -0,0 +1,32 @@ +// 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 encodings + +import "go.opentelemetry.io/collector/consumer/pdata" + +// MetricsEncoder converts pdata to another type. +type MetricsEncoder interface { + MarshalMetrics(md pdata.Metrics) ([]byte, error) +} + +// TracesEncoder converts pdata to another type. +type TracesEncoder interface { + MarshalTraces(md pdata.Traces) ([]byte, error) +} + +// LogsEncoder converts pdata to another type. +type LogsEncoder interface { + MarshalLogs(md pdata.Logs) ([]byte, error) +} diff --git a/protocols/encodings/encodings.go b/protocols/encodings/encodings.go new file mode 100644 index 00000000000..d9824faa188 --- /dev/null +++ b/protocols/encodings/encodings.go @@ -0,0 +1,38 @@ +// 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 encodings + +import "fmt" + +// Encoding is the format that a protocol is serialized to. +type Encoding string + +const ( + Protobuf Encoding = "protobuf" + JSON Encoding = "json" + Thrift Encoding = "thrift" +) + +func (e Encoding) String() string { + return string(e) +} + +type ErrInvalidEncoding struct { + Encoding Encoding +} + +func (e *ErrInvalidEncoding) Error() string { + return fmt.Sprintf("unsupported encoding %q", e.Encoding) +} diff --git a/protocols/models/models.go b/protocols/models/models.go new file mode 100644 index 00000000000..5f26dc5850b --- /dev/null +++ b/protocols/models/models.go @@ -0,0 +1,51 @@ +// 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 models + +import ( + "fmt" + "go.opentelemetry.io/collector/consumer/pdata" +) + +type MetricsModelTranslator interface { + // MetricsFromModel converts a data model of another protocol into pdata. + MetricsFromModel(src interface{}) (pdata.Metrics, error) + // MetricsToModel converts pdata to data model. + MetricsToModel(md pdata.Metrics, out interface{}) error +} + +type TracesModelTranslator interface { + // TracesFromModel converts a data model of another protocol into pdata. + TracesFromModel(src interface{}) (pdata.Traces, error) + // TracesToModel converts pdata to data model. + TracesToModel(md pdata.Traces, out interface{}) error +} + +type LogsModelTranslator interface { + // LogsFromModel converts a data model of another protocol into pdata. + LogsFromModel(src interface{}) (pdata.Logs, error) + // LogsToModel converts pdata to data model. + LogsToModel(md pdata.Logs, out interface{}) error +} + +// ErrIncompatibleType details a type conversion error during translation. +type ErrIncompatibleType struct { + Model interface{} + // TODO: maybe do expected vs. actual? +} + +func (i *ErrIncompatibleType) Error() string { + return fmt.Sprintf("model type %T is incompatible", i.Model) +} diff --git a/protocols/zipkinv2/encodings.go b/protocols/zipkinv2/encodings.go new file mode 100644 index 00000000000..7783fdffac0 --- /dev/null +++ b/protocols/zipkinv2/encodings.go @@ -0,0 +1,71 @@ +// 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" + "github.com/openzipkin/zipkin-go/proto/zipkin_proto3" + + "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/protocols/encodings" + "go.opentelemetry.io/collector/protocols/models" +) + +var ( + _ encodings.TracesEncoder = (*Encoder)(nil) + _ encodings.TracesDecoder = (*Encoder)(nil) +) + +type Encoder struct { + models.TracesModelTranslator + // Encoding is the format Zipkin is serialized to. + Encoding encodings.Encoding +} + +func (z *Encoder) UnmarshalTraces(bytes []byte) (pdata.Traces, error) { + switch z.Encoding { + case encodings.Protobuf: + spans, err := zipkin_proto3.ParseSpans(bytes, false) + if err != nil { + return pdata.NewTraces(), err + } + return z.TracesFromModel(spans) + case encodings.JSON: + var spans []*zipkinmodel.SpanModel + if err := json.Unmarshal(bytes, &spans); err != nil { + return pdata.NewTraces(), err + } + return z.TracesFromModel(spans) + default: + return pdata.NewTraces(), &encodings.ErrInvalidEncoding{Encoding: z.Encoding} + } +} + +func (z *Encoder) MarshalTraces(td pdata.Traces) ([]byte, error) { + switch z.Encoding { + // TODO + // case protocols.Protobuf: + case encodings.JSON: + var spans []*zipkinmodel.SpanModel + if err := z.TracesToModel(td, &spans); err != nil { + return nil, err + } + return json.Marshal(spans) + default: + return nil, &encodings.ErrInvalidEncoding{Encoding: z.Encoding} + } +} diff --git a/protocols/zipkinv2/models.go b/protocols/zipkinv2/models.go new file mode 100644 index 00000000000..9ec81e344d8 --- /dev/null +++ b/protocols/zipkinv2/models.go @@ -0,0 +1,50 @@ +// 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 ( + zipkinmodel "github.com/openzipkin/zipkin-go/model" + + "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/protocols/models" + "go.opentelemetry.io/collector/translator/trace/zipkin" +) + +var ( + _ models.TracesModelTranslator = (*Model)(nil) +) + +type Model struct { + // ParseStringTags is true if string tags should be automatically converted to numbers. + ParseStringTags bool +} + +func (z *Model) TracesFromModel(src interface{}) (pdata.Traces, error) { + if model, ok := src.([]*zipkinmodel.SpanModel); ok { + return zipkin.V2SpansToInternalTraces(model, z.ParseStringTags) + } + return pdata.NewTraces(), &models.ErrIncompatibleType{Model: src} +} + +func (z *Model) TracesToModel(td pdata.Traces, out interface{}) error { + if model, ok := out.(*[]*zipkinmodel.SpanModel); ok { + sm, err := zipkin.InternalTracesToZipkinSpans(td) + if err != nil { + return err + } + *model = sm + } + return &models.ErrIncompatibleType{Model: out} +} From deaafb68338a506a78c55cb2589436500b5c84f5 Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Mon, 17 May 2021 13:41:34 -0400 Subject: [PATCH 02/15] use encode/decode consistently --- protocols/encodings/decoder.go | 15 ++++++--------- protocols/encodings/encoder.go | 12 ++++++------ protocols/zipkinv2/encodings.go | 4 ++-- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/protocols/encodings/decoder.go b/protocols/encodings/decoder.go index 1f31d005cec..08837c0e43e 100644 --- a/protocols/encodings/decoder.go +++ b/protocols/encodings/decoder.go @@ -16,20 +16,17 @@ package encodings import "go.opentelemetry.io/collector/consumer/pdata" -// MetricsDecoder converts another protocol to pdata. +// MetricsDecoder deserializes bytes into pdata. type MetricsDecoder interface { - // UnmarshalMetrics converts data in another protocol to pdata. - UnmarshalMetrics(bytes []byte) (pdata.Metrics, error) + DecodeMetrics(bytes []byte) (pdata.Metrics, error) } -// TracesDecoder converts another type of data to pdata. +// TracesDecoder deserializes bytes into pdata. type TracesDecoder interface { - // UnmarshalTraces converts data in another protocol to pdata. - UnmarshalTraces(bytes []byte) (pdata.Traces, error) + DecodeTraces(bytes []byte) (pdata.Traces, error) } -// LogsDecoder converts another type of data to pdata. +// LogsDecoder deserializes bytes into pdata. type LogsDecoder interface { - // UnmarshalLogs converts a data model of another protocol into pdata. - UnmarshalLogs(bytes []byte) (pdata.Logs, error) + DecodeLogs(bytes []byte) (pdata.Logs, error) } diff --git a/protocols/encodings/encoder.go b/protocols/encodings/encoder.go index 3eda97ea93e..429f061d4b3 100644 --- a/protocols/encodings/encoder.go +++ b/protocols/encodings/encoder.go @@ -16,17 +16,17 @@ package encodings import "go.opentelemetry.io/collector/consumer/pdata" -// MetricsEncoder converts pdata to another type. +// MetricsEncoder serializes pdata into bytes. type MetricsEncoder interface { - MarshalMetrics(md pdata.Metrics) ([]byte, error) + EncodeMetrics(md pdata.Metrics) ([]byte, error) } -// TracesEncoder converts pdata to another type. +// TracesEncoder serializes pdata into bytes. type TracesEncoder interface { - MarshalTraces(md pdata.Traces) ([]byte, error) + EncodeTraces(td pdata.Traces) ([]byte, error) } -// LogsEncoder converts pdata to another type. +// LogsEncoder serializes pdata into bytes. type LogsEncoder interface { - MarshalLogs(md pdata.Logs) ([]byte, error) + EncodeLogs(ld pdata.Logs) ([]byte, error) } diff --git a/protocols/zipkinv2/encodings.go b/protocols/zipkinv2/encodings.go index 7783fdffac0..12c952f6bea 100644 --- a/protocols/zipkinv2/encodings.go +++ b/protocols/zipkinv2/encodings.go @@ -36,7 +36,7 @@ type Encoder struct { Encoding encodings.Encoding } -func (z *Encoder) UnmarshalTraces(bytes []byte) (pdata.Traces, error) { +func (z *Encoder) DecodeTraces(bytes []byte) (pdata.Traces, error) { switch z.Encoding { case encodings.Protobuf: spans, err := zipkin_proto3.ParseSpans(bytes, false) @@ -55,7 +55,7 @@ func (z *Encoder) UnmarshalTraces(bytes []byte) (pdata.Traces, error) { } } -func (z *Encoder) MarshalTraces(td pdata.Traces) ([]byte, error) { +func (z *Encoder) EncodeTraces(td pdata.Traces) ([]byte, error) { switch z.Encoding { // TODO // case protocols.Protobuf: From 2cf2810755d73b095330a87c8c6e88ddeab767cc Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Mon, 17 May 2021 14:19:24 -0400 Subject: [PATCH 03/15] review feedback --- protocols/{encodings => encoding}/decoder.go | 8 +++---- protocols/{encodings => encoding}/encoder.go | 8 +++---- .../encodings.go => encoding/encoding.go} | 21 ++++++++++--------- protocols/zipkinv2/encodings.go | 18 ++++++++-------- 4 files changed, 28 insertions(+), 27 deletions(-) rename protocols/{encodings => encoding}/decoder.go (84%) rename protocols/{encodings => encoding}/encoder.go (85%) rename protocols/{encodings/encodings.go => encoding/encoding.go} (64%) diff --git a/protocols/encodings/decoder.go b/protocols/encoding/decoder.go similarity index 84% rename from protocols/encodings/decoder.go rename to protocols/encoding/decoder.go index 08837c0e43e..2eb6091f6c9 100644 --- a/protocols/encodings/decoder.go +++ b/protocols/encoding/decoder.go @@ -12,21 +12,21 @@ // See the License for the specific language governing permissions and // limitations under the License. -package encodings +package encoding import "go.opentelemetry.io/collector/consumer/pdata" -// MetricsDecoder deserializes bytes into pdata. +// MetricsDecoder decodes bytes into pdata. type MetricsDecoder interface { DecodeMetrics(bytes []byte) (pdata.Metrics, error) } -// TracesDecoder deserializes bytes into pdata. +// TracesDecoder decodes bytes into pdata. type TracesDecoder interface { DecodeTraces(bytes []byte) (pdata.Traces, error) } -// LogsDecoder deserializes bytes into pdata. +// LogsDecoder decodes bytes into pdata. type LogsDecoder interface { DecodeLogs(bytes []byte) (pdata.Logs, error) } diff --git a/protocols/encodings/encoder.go b/protocols/encoding/encoder.go similarity index 85% rename from protocols/encodings/encoder.go rename to protocols/encoding/encoder.go index 429f061d4b3..9aea9ba3c97 100644 --- a/protocols/encodings/encoder.go +++ b/protocols/encoding/encoder.go @@ -12,21 +12,21 @@ // See the License for the specific language governing permissions and // limitations under the License. -package encodings +package encoding import "go.opentelemetry.io/collector/consumer/pdata" -// MetricsEncoder serializes pdata into bytes. +// MetricsEncoder encodes pdata into bytes. type MetricsEncoder interface { EncodeMetrics(md pdata.Metrics) ([]byte, error) } -// TracesEncoder serializes pdata into bytes. +// TracesEncoder encodes pdata into bytes. type TracesEncoder interface { EncodeTraces(td pdata.Traces) ([]byte, error) } -// LogsEncoder serializes pdata into bytes. +// LogsEncoder encodes pdata into bytes. type LogsEncoder interface { EncodeLogs(ld pdata.Logs) ([]byte, error) } diff --git a/protocols/encodings/encodings.go b/protocols/encoding/encoding.go similarity index 64% rename from protocols/encodings/encodings.go rename to protocols/encoding/encoding.go index d9824faa188..68ffbc82bba 100644 --- a/protocols/encodings/encodings.go +++ b/protocols/encoding/encoding.go @@ -12,27 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -package encodings +package encoding import "fmt" -// Encoding is the format that a protocol is serialized to. -type Encoding string +// Type is the encoding format that a model is serialized to. +type Type string const ( - Protobuf Encoding = "protobuf" - JSON Encoding = "json" - Thrift Encoding = "thrift" + Protobuf Type = "protobuf" + JSON Type = "json" + Thrift Type = "thrift" ) -func (e Encoding) String() string { +func (e Type) String() string { return string(e) } -type ErrInvalidEncoding struct { - Encoding Encoding +// ErrUnavailableEncoding is returned when the requested encoding is not present. +type ErrUnavailableEncoding struct { + Encoding Type } -func (e *ErrInvalidEncoding) Error() string { +func (e *ErrUnavailableEncoding) Error() string { return fmt.Sprintf("unsupported encoding %q", e.Encoding) } diff --git a/protocols/zipkinv2/encodings.go b/protocols/zipkinv2/encodings.go index 12c952f6bea..26ef9d8e346 100644 --- a/protocols/zipkinv2/encodings.go +++ b/protocols/zipkinv2/encodings.go @@ -21,37 +21,37 @@ import ( "github.com/openzipkin/zipkin-go/proto/zipkin_proto3" "go.opentelemetry.io/collector/consumer/pdata" - "go.opentelemetry.io/collector/protocols/encodings" + "go.opentelemetry.io/collector/protocols/encoding" "go.opentelemetry.io/collector/protocols/models" ) var ( - _ encodings.TracesEncoder = (*Encoder)(nil) - _ encodings.TracesDecoder = (*Encoder)(nil) + _ encoding.TracesEncoder = (*Encoder)(nil) + _ encoding.TracesDecoder = (*Encoder)(nil) ) type Encoder struct { models.TracesModelTranslator // Encoding is the format Zipkin is serialized to. - Encoding encodings.Encoding + Encoding encoding.Type } func (z *Encoder) DecodeTraces(bytes []byte) (pdata.Traces, error) { switch z.Encoding { - case encodings.Protobuf: + case encoding.Protobuf: spans, err := zipkin_proto3.ParseSpans(bytes, false) if err != nil { return pdata.NewTraces(), err } return z.TracesFromModel(spans) - case encodings.JSON: + case encoding.JSON: var spans []*zipkinmodel.SpanModel if err := json.Unmarshal(bytes, &spans); err != nil { return pdata.NewTraces(), err } return z.TracesFromModel(spans) default: - return pdata.NewTraces(), &encodings.ErrInvalidEncoding{Encoding: z.Encoding} + return pdata.NewTraces(), &encoding.ErrUnavailableEncoding{Encoding: z.Encoding} } } @@ -59,13 +59,13 @@ func (z *Encoder) EncodeTraces(td pdata.Traces) ([]byte, error) { switch z.Encoding { // TODO // case protocols.Protobuf: - case encodings.JSON: + case encoding.JSON: var spans []*zipkinmodel.SpanModel if err := z.TracesToModel(td, &spans); err != nil { return nil, err } return json.Marshal(spans) default: - return nil, &encodings.ErrInvalidEncoding{Encoding: z.Encoding} + return nil, &encoding.ErrUnavailableEncoding{Encoding: z.Encoding} } } From b647d089d09f9664921f9017b4d937064aa00803 Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Mon, 17 May 2021 15:10:53 -0400 Subject: [PATCH 04/15] decouple encoding from model Before the encoder interfaces took pdata and serialized it by calling the translator. This fully separates the concerns by having model do pure translation, encoding do pure serialization, and the transcoder doing both. Without this there was no way to use the encoder if you already had a model. Only updates traces for feedback purposes. --- protocols/encoding/decoder.go | 8 +-- protocols/encoding/encoder.go | 8 +-- protocols/models/models.go | 2 + protocols/protocols.go | 57 +++++++++++++++++++ .../zipkinv2/{encodings.go => encoding.go} | 27 ++++----- protocols/zipkinv2/models.go | 4 ++ 6 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 protocols/protocols.go rename protocols/zipkinv2/{encodings.go => encoding.go} (70%) diff --git a/protocols/encoding/decoder.go b/protocols/encoding/decoder.go index 2eb6091f6c9..4830de68268 100644 --- a/protocols/encoding/decoder.go +++ b/protocols/encoding/decoder.go @@ -14,19 +14,17 @@ package encoding -import "go.opentelemetry.io/collector/consumer/pdata" - // MetricsDecoder decodes bytes into pdata. type MetricsDecoder interface { - DecodeMetrics(bytes []byte) (pdata.Metrics, error) + DecodeMetrics(bytes []byte) (interface{}, error) } // TracesDecoder decodes bytes into pdata. type TracesDecoder interface { - DecodeTraces(bytes []byte) (pdata.Traces, error) + DecodeTraces(bytes []byte) (interface{}, error) } // LogsDecoder decodes bytes into pdata. type LogsDecoder interface { - DecodeLogs(bytes []byte) (pdata.Logs, error) + DecodeLogs(bytes []byte) (interface{}, error) } diff --git a/protocols/encoding/encoder.go b/protocols/encoding/encoder.go index 9aea9ba3c97..f701d20cac4 100644 --- a/protocols/encoding/encoder.go +++ b/protocols/encoding/encoder.go @@ -14,19 +14,17 @@ package encoding -import "go.opentelemetry.io/collector/consumer/pdata" - // MetricsEncoder encodes pdata into bytes. type MetricsEncoder interface { - EncodeMetrics(md pdata.Metrics) ([]byte, error) + EncodeMetrics(model interface{}) ([]byte, error) } // TracesEncoder encodes pdata into bytes. type TracesEncoder interface { - EncodeTraces(td pdata.Traces) ([]byte, error) + EncodeTraces(model interface{}) ([]byte, error) } // LogsEncoder encodes pdata into bytes. type LogsEncoder interface { - EncodeLogs(ld pdata.Logs) ([]byte, error) + EncodeLogs(model interface{}) ([]byte, error) } diff --git a/protocols/models/models.go b/protocols/models/models.go index 5f26dc5850b..251d13c55a6 100644 --- a/protocols/models/models.go +++ b/protocols/models/models.go @@ -31,6 +31,8 @@ type TracesModelTranslator interface { TracesFromModel(src interface{}) (pdata.Traces, error) // TracesToModel converts pdata to data model. TracesToModel(md pdata.Traces, out interface{}) error + + Type() interface{} } type LogsModelTranslator interface { diff --git a/protocols/protocols.go b/protocols/protocols.go new file mode 100644 index 00000000000..84b806f974e --- /dev/null +++ b/protocols/protocols.go @@ -0,0 +1,57 @@ +// 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 protocols + +import ( + "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/protocols/encoding" + "go.opentelemetry.io/collector/protocols/models" + "go.opentelemetry.io/collector/protocols/zipkinv2" +) + +type Transcoder struct { + models.TracesModelTranslator + encoding.TracesEncoder + encoding.TracesDecoder +} + +func (t *Transcoder) UnmarshalTraces(data []byte) (pdata.Traces, error) { + model, err := t.DecodeTraces(data) + if err != nil { + return pdata.NewTraces(), err + } + return t.TracesFromModel(model) +} + +func (t *Transcoder) MarshalTraces(td pdata.Traces) ([]byte, error) { + out := t.Type() + if err := t.TracesToModel(td, &out); err != nil { + return nil, err + } + return t.EncodeTraces(out) +} + +func test() { + translator := &zipkinv2.Model{ParseStringTags: false} + encodingDecoding := &zipkinv2.Encoder{ + Encoding: encoding.JSON, + } + + tc := &Transcoder{ + TracesModelTranslator: translator, + TracesEncoder: encodingDecoding, + TracesDecoder: encodingDecoding, + } +} diff --git a/protocols/zipkinv2/encodings.go b/protocols/zipkinv2/encoding.go similarity index 70% rename from protocols/zipkinv2/encodings.go rename to protocols/zipkinv2/encoding.go index 26ef9d8e346..88e8542f0b7 100644 --- a/protocols/zipkinv2/encodings.go +++ b/protocols/zipkinv2/encoding.go @@ -20,7 +20,6 @@ import ( zipkinmodel "github.com/openzipkin/zipkin-go/model" "github.com/openzipkin/zipkin-go/proto/zipkin_proto3" - "go.opentelemetry.io/collector/consumer/pdata" "go.opentelemetry.io/collector/protocols/encoding" "go.opentelemetry.io/collector/protocols/models" ) @@ -31,39 +30,35 @@ var ( ) type Encoder struct { - models.TracesModelTranslator // Encoding is the format Zipkin is serialized to. Encoding encoding.Type } -func (z *Encoder) DecodeTraces(bytes []byte) (pdata.Traces, error) { +func (z *Encoder) DecodeTraces(bytes []byte) (interface{}, error) { switch z.Encoding { case encoding.Protobuf: - spans, err := zipkin_proto3.ParseSpans(bytes, false) - if err != nil { - return pdata.NewTraces(), err - } - return z.TracesFromModel(spans) + return zipkin_proto3.ParseSpans(bytes, false) case encoding.JSON: var spans []*zipkinmodel.SpanModel if err := json.Unmarshal(bytes, &spans); err != nil { - return pdata.NewTraces(), err + return nil, err } - return z.TracesFromModel(spans) + return spans, nil default: - return pdata.NewTraces(), &encoding.ErrUnavailableEncoding{Encoding: z.Encoding} + return nil, &encoding.ErrUnavailableEncoding{Encoding: z.Encoding} } } -func (z *Encoder) EncodeTraces(td pdata.Traces) ([]byte, error) { +func (z *Encoder) EncodeTraces(model interface{}) ([]byte, error) { + spans, ok := model.([]*zipkinmodel.SpanModel) + if !ok { + return nil, &models.ErrIncompatibleType{Model: spans} + } + switch z.Encoding { // TODO // case protocols.Protobuf: case encoding.JSON: - var spans []*zipkinmodel.SpanModel - if err := z.TracesToModel(td, &spans); err != nil { - return nil, err - } return json.Marshal(spans) default: return nil, &encoding.ErrUnavailableEncoding{Encoding: z.Encoding} diff --git a/protocols/zipkinv2/models.go b/protocols/zipkinv2/models.go index 9ec81e344d8..a0e2bd67eea 100644 --- a/protocols/zipkinv2/models.go +++ b/protocols/zipkinv2/models.go @@ -31,6 +31,10 @@ type Model struct { ParseStringTags bool } +func (z *Model) Type() interface{} { + return []*zipkinmodel.SpanModel{} +} + func (z *Model) TracesFromModel(src interface{}) (pdata.Traces, error) { if model, ok := src.([]*zipkinmodel.SpanModel); ok { return zipkin.V2SpansToInternalTraces(model, z.ParseStringTags) From c22633a9133644fad93f52d8aa733391910bfee9 Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Mon, 17 May 2021 23:03:16 -0400 Subject: [PATCH 05/15] add high level interface * standardized on encoding/decoding terminology * renamed encodings to bytes to avoid confusion with encode/decode terminology * added high level interfaces to top level protocols package that goes directly pdata <-> bytes --- protocols/README.md | 9 ++++ protocols/{encoding => bytes}/decoder.go | 8 +-- protocols/{encoding => bytes}/encoder.go | 8 +-- protocols/{encoding => bytes}/encoding.go | 12 +++-- protocols/decoder.go | 63 ++++++++++++++++++++++ protocols/encoder.go | 63 ++++++++++++++++++++++ protocols/models/decoder.go | 38 +++++++++++++ protocols/models/encoder.go | 38 +++++++++++++ protocols/models/models.go | 38 ++++--------- protocols/protocols.go | 57 -------------------- protocols/zipkinv2/encoding.go | 66 ----------------------- protocols/zipkinv2/models.go | 54 ------------------- 12 files changed, 238 insertions(+), 216 deletions(-) create mode 100644 protocols/README.md rename protocols/{encoding => bytes}/decoder.go (84%) rename protocols/{encoding => bytes}/encoder.go (84%) rename protocols/{encoding => bytes}/encoding.go (80%) create mode 100644 protocols/decoder.go create mode 100644 protocols/encoder.go create mode 100644 protocols/models/decoder.go create mode 100644 protocols/models/encoder.go delete mode 100644 protocols/protocols.go delete mode 100644 protocols/zipkinv2/encoding.go delete mode 100644 protocols/zipkinv2/models.go diff --git a/protocols/README.md b/protocols/README.md new file mode 100644 index 00000000000..870618da5e9 --- /dev/null +++ b/protocols/README.md @@ -0,0 +1,9 @@ +# Protocols + +This package provides common ways for decoding bytes into data models (e.g. Zipkin Span). These data models can then be decoded into internal pdata representations. Similarly, pdata can be encoded into a data model which can then be encoded into bytes. + +[bytes](bytes): Common interfaces for encoding/decoding bytes from/to data models. + +[models](models): Common interfaces for encoding/decoding data models from/to pdata. + +This package provides higher level APIs that do both encoding of bytes and data model if going directly pdata <-> bytes. \ No newline at end of file diff --git a/protocols/encoding/decoder.go b/protocols/bytes/decoder.go similarity index 84% rename from protocols/encoding/decoder.go rename to protocols/bytes/decoder.go index 4830de68268..021b22c4cea 100644 --- a/protocols/encoding/decoder.go +++ b/protocols/bytes/decoder.go @@ -12,19 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -package encoding +package bytes -// MetricsDecoder decodes bytes into pdata. +// MetricsDecoder decodes bytes into data model. type MetricsDecoder interface { DecodeMetrics(bytes []byte) (interface{}, error) } -// TracesDecoder decodes bytes into pdata. +// TracesDecoder decodes bytes into data model. type TracesDecoder interface { DecodeTraces(bytes []byte) (interface{}, error) } -// LogsDecoder decodes bytes into pdata. +// LogsDecoder decodes bytes into data model. type LogsDecoder interface { DecodeLogs(bytes []byte) (interface{}, error) } diff --git a/protocols/encoding/encoder.go b/protocols/bytes/encoder.go similarity index 84% rename from protocols/encoding/encoder.go rename to protocols/bytes/encoder.go index f701d20cac4..2fd6ae51fbe 100644 --- a/protocols/encoding/encoder.go +++ b/protocols/bytes/encoder.go @@ -12,19 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -package encoding +package bytes -// MetricsEncoder encodes pdata into bytes. +// MetricsEncoder encodes data model into bytes. type MetricsEncoder interface { EncodeMetrics(model interface{}) ([]byte, error) } -// TracesEncoder encodes pdata into bytes. +// TracesEncoder encodes data model into bytes. type TracesEncoder interface { EncodeTraces(model interface{}) ([]byte, error) } -// LogsEncoder encodes pdata into bytes. +// LogsEncoder encodes data model into bytes. type LogsEncoder interface { EncodeLogs(model interface{}) ([]byte, error) } diff --git a/protocols/encoding/encoding.go b/protocols/bytes/encoding.go similarity index 80% rename from protocols/encoding/encoding.go rename to protocols/bytes/encoding.go index 68ffbc82bba..ea6d7e8fb01 100644 --- a/protocols/encoding/encoding.go +++ b/protocols/bytes/encoding.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package encoding +package bytes import "fmt" @@ -29,11 +29,15 @@ func (e Type) String() string { return string(e) } -// ErrUnavailableEncoding is returned when the requested encoding is not present. +// ErrUnavailableEncoding is returned when the requested encoding is not supported. type ErrUnavailableEncoding struct { - Encoding Type + encoding Type } func (e *ErrUnavailableEncoding) Error() string { - return fmt.Sprintf("unsupported encoding %q", e.Encoding) + return fmt.Sprintf("unsupported encoding %q", e.encoding) } + +func NewErrUnavailableEncoding(encoding Type) *ErrUnavailableEncoding { + return &ErrUnavailableEncoding{encoding: encoding} +} \ No newline at end of file diff --git a/protocols/decoder.go b/protocols/decoder.go new file mode 100644 index 00000000000..50b9c84510d --- /dev/null +++ b/protocols/decoder.go @@ -0,0 +1,63 @@ +// 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 protocols + +import ( + "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/protocols/bytes" + "go.opentelemetry.io/collector/protocols/models" +) + +type MetricsDecoder struct { + mod models.MetricsDecoder + enc bytes.MetricsDecoder +} + +type TracesDecoder struct { + mod models.TracesDecoder + enc bytes.TracesDecoder +} + +type LogsDecoder struct { + mod models.LogsDecoder + enc bytes.LogsDecoder +} + +// DecodeMetrics decodes bytes to pdata. +func (t *MetricsDecoder) DecodeMetrics(data []byte) (pdata.Metrics, error) { + model, err := t.enc.DecodeMetrics(data) + if err != nil { + return pdata.NewMetrics(), err + } + return t.mod.ToMetrics(model) +} + +// DecodeTraces decodes bytes to pdata. +func (t *TracesDecoder) DecodeTraces(data []byte) (pdata.Traces, error) { + model, err := t.enc.DecodeTraces(data) + if err != nil { + return pdata.NewTraces(), err + } + return t.mod.ToTraces(model) +} + +// DecodeLogs decodes bytes to pdata. +func (t *LogsDecoder) DecodeLogs(data []byte) (pdata.Logs, error) { + model, err := t.enc.DecodeLogs(data) + if err != nil { + return pdata.NewLogs(), err + } + return t.mod.ToLogs(model) +} \ No newline at end of file diff --git a/protocols/encoder.go b/protocols/encoder.go new file mode 100644 index 00000000000..121c1d5e0e8 --- /dev/null +++ b/protocols/encoder.go @@ -0,0 +1,63 @@ +// 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 protocols + +import ( + "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/protocols/models" + "go.opentelemetry.io/collector/protocols/bytes" +) + +type MetricsEncoder struct { + mod models.MetricsEncoder + enc bytes.MetricsEncoder +} + +type TracesEncoder struct { + mod models.TracesEncoder + enc bytes.TracesEncoder +} + +type LogsEncoder struct { + mod models.LogsEncoder + enc bytes.LogsEncoder +} + +// EncodeMetrics encodes pdata to bytes. +func (t *MetricsEncoder) EncodeMetrics(td pdata.Metrics) ([]byte, error) { + out := t.mod.Type() + if err := t.mod.FromMetrics(td, &out); err != nil { + return nil, err + } + return t.enc.EncodeMetrics(out) +} + +// EncodeTraces encodes pdata to bytes. +func (t *TracesEncoder) EncodeTraces(td pdata.Traces) ([]byte, error) { + out := t.mod.Type() + if err := t.mod.FromTraces(td, &out); err != nil { + return nil, err + } + return t.enc.EncodeTraces(out) +} + +// EncodeLogs encodes pdata to bytes. +func (t *LogsEncoder) EncodeLogs(td pdata.Logs) ([]byte, error) { + out := t.mod.Type() + if err := t.mod.FromLogs(td, &out); err != nil { + return nil, err + } + return t.enc.EncodeLogs(out) +} diff --git a/protocols/models/decoder.go b/protocols/models/decoder.go new file mode 100644 index 00000000000..cc0902e26de --- /dev/null +++ b/protocols/models/decoder.go @@ -0,0 +1,38 @@ +// 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 models + +import "go.opentelemetry.io/collector/consumer/pdata" + +type MetricsDecoder interface { + // ToMetrics converts a data model of another protocol into pdata. + ToMetrics(src interface{}) (pdata.Metrics, error) + // Type returns an instance of the model. + Type() interface{} +} + +type TracesDecoder interface { + // ToTraces converts a data model of another protocol into pdata. + ToTraces(src interface{}) (pdata.Traces, error) + // Type returns an instance of the model. + Type() interface{} +} + +type LogsDecoder interface { + // ToLogs converts a data model of another protocol into pdata. + ToLogs(src interface{}) (pdata.Logs, error) + // Type returns an instance of the model. + Type() interface{} +} \ No newline at end of file diff --git a/protocols/models/encoder.go b/protocols/models/encoder.go new file mode 100644 index 00000000000..3e8ee061594 --- /dev/null +++ b/protocols/models/encoder.go @@ -0,0 +1,38 @@ +// 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 models + +import "go.opentelemetry.io/collector/consumer/pdata" + +type MetricsEncoder interface { + // FromMetrics converts pdata to data model. + FromMetrics(md pdata.Metrics, out interface{}) error + // Type returns an instance of the model. + Type() interface{} +} + +type TracesEncoder interface { + // FromTraces converts pdata to data model. + FromTraces(md pdata.Traces, out interface{}) error + // Type returns an instance of the model. + Type() interface{} +} + +type LogsEncoder interface { + // FromLogs converts pdata to data model. + FromLogs(md pdata.Logs, out interface{}) error + // Type returns an instance of the model. + Type() interface{} +} diff --git a/protocols/models/models.go b/protocols/models/models.go index 251d13c55a6..2963393fede 100644 --- a/protocols/models/models.go +++ b/protocols/models/models.go @@ -16,38 +16,22 @@ package models import ( "fmt" - "go.opentelemetry.io/collector/consumer/pdata" ) -type MetricsModelTranslator interface { - // MetricsFromModel converts a data model of another protocol into pdata. - MetricsFromModel(src interface{}) (pdata.Metrics, error) - // MetricsToModel converts pdata to data model. - MetricsToModel(md pdata.Metrics, out interface{}) error -} - -type TracesModelTranslator interface { - // TracesFromModel converts a data model of another protocol into pdata. - TracesFromModel(src interface{}) (pdata.Traces, error) - // TracesToModel converts pdata to data model. - TracesToModel(md pdata.Traces, out interface{}) error - - Type() interface{} -} - -type LogsModelTranslator interface { - // LogsFromModel converts a data model of another protocol into pdata. - LogsFromModel(src interface{}) (pdata.Logs, error) - // LogsToModel converts pdata to data model. - LogsToModel(md pdata.Logs, out interface{}) error -} - // ErrIncompatibleType details a type conversion error during translation. type ErrIncompatibleType struct { - Model interface{} - // TODO: maybe do expected vs. actual? + given interface{} + expected interface{} } func (i *ErrIncompatibleType) Error() string { - return fmt.Sprintf("model type %T is incompatible", i.Model) + return fmt.Sprintf("model type %T is expected but given %T ", i.expected, i.given) } + +// NewErrIncompatibleType returns ErrIncompatibleType instance +func NewErrIncompatibleType(expected, given interface{}) *ErrIncompatibleType { + return &ErrIncompatibleType{ + given: given, + expected: expected, + } +} \ No newline at end of file diff --git a/protocols/protocols.go b/protocols/protocols.go deleted file mode 100644 index 84b806f974e..00000000000 --- a/protocols/protocols.go +++ /dev/null @@ -1,57 +0,0 @@ -// 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 protocols - -import ( - "go.opentelemetry.io/collector/consumer/pdata" - "go.opentelemetry.io/collector/protocols/encoding" - "go.opentelemetry.io/collector/protocols/models" - "go.opentelemetry.io/collector/protocols/zipkinv2" -) - -type Transcoder struct { - models.TracesModelTranslator - encoding.TracesEncoder - encoding.TracesDecoder -} - -func (t *Transcoder) UnmarshalTraces(data []byte) (pdata.Traces, error) { - model, err := t.DecodeTraces(data) - if err != nil { - return pdata.NewTraces(), err - } - return t.TracesFromModel(model) -} - -func (t *Transcoder) MarshalTraces(td pdata.Traces) ([]byte, error) { - out := t.Type() - if err := t.TracesToModel(td, &out); err != nil { - return nil, err - } - return t.EncodeTraces(out) -} - -func test() { - translator := &zipkinv2.Model{ParseStringTags: false} - encodingDecoding := &zipkinv2.Encoder{ - Encoding: encoding.JSON, - } - - tc := &Transcoder{ - TracesModelTranslator: translator, - TracesEncoder: encodingDecoding, - TracesDecoder: encodingDecoding, - } -} diff --git a/protocols/zipkinv2/encoding.go b/protocols/zipkinv2/encoding.go deleted file mode 100644 index 88e8542f0b7..00000000000 --- a/protocols/zipkinv2/encoding.go +++ /dev/null @@ -1,66 +0,0 @@ -// 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" - "github.com/openzipkin/zipkin-go/proto/zipkin_proto3" - - "go.opentelemetry.io/collector/protocols/encoding" - "go.opentelemetry.io/collector/protocols/models" -) - -var ( - _ encoding.TracesEncoder = (*Encoder)(nil) - _ encoding.TracesDecoder = (*Encoder)(nil) -) - -type Encoder struct { - // Encoding is the format Zipkin is serialized to. - Encoding encoding.Type -} - -func (z *Encoder) DecodeTraces(bytes []byte) (interface{}, error) { - switch z.Encoding { - case encoding.Protobuf: - return zipkin_proto3.ParseSpans(bytes, false) - case encoding.JSON: - var spans []*zipkinmodel.SpanModel - if err := json.Unmarshal(bytes, &spans); err != nil { - return nil, err - } - return spans, nil - default: - return nil, &encoding.ErrUnavailableEncoding{Encoding: z.Encoding} - } -} - -func (z *Encoder) EncodeTraces(model interface{}) ([]byte, error) { - spans, ok := model.([]*zipkinmodel.SpanModel) - if !ok { - return nil, &models.ErrIncompatibleType{Model: spans} - } - - switch z.Encoding { - // TODO - // case protocols.Protobuf: - case encoding.JSON: - return json.Marshal(spans) - default: - return nil, &encoding.ErrUnavailableEncoding{Encoding: z.Encoding} - } -} diff --git a/protocols/zipkinv2/models.go b/protocols/zipkinv2/models.go deleted file mode 100644 index a0e2bd67eea..00000000000 --- a/protocols/zipkinv2/models.go +++ /dev/null @@ -1,54 +0,0 @@ -// 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 ( - zipkinmodel "github.com/openzipkin/zipkin-go/model" - - "go.opentelemetry.io/collector/consumer/pdata" - "go.opentelemetry.io/collector/protocols/models" - "go.opentelemetry.io/collector/translator/trace/zipkin" -) - -var ( - _ models.TracesModelTranslator = (*Model)(nil) -) - -type Model struct { - // ParseStringTags is true if string tags should be automatically converted to numbers. - ParseStringTags bool -} - -func (z *Model) Type() interface{} { - return []*zipkinmodel.SpanModel{} -} - -func (z *Model) TracesFromModel(src interface{}) (pdata.Traces, error) { - if model, ok := src.([]*zipkinmodel.SpanModel); ok { - return zipkin.V2SpansToInternalTraces(model, z.ParseStringTags) - } - return pdata.NewTraces(), &models.ErrIncompatibleType{Model: src} -} - -func (z *Model) TracesToModel(td pdata.Traces, out interface{}) error { - if model, ok := out.(*[]*zipkinmodel.SpanModel); ok { - sm, err := zipkin.InternalTracesToZipkinSpans(td) - if err != nil { - return err - } - *model = sm - } - return &models.ErrIncompatibleType{Model: out} -} From 2cbc215c87859f9213b0e76549808cd0c68b3f47 Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Tue, 18 May 2021 13:00:41 -0400 Subject: [PATCH 06/15] cleanup --- protocols/README.md | 4 +- protocols/decoder.go | 63 ------------------- protocols/encoder.go | 63 ------------------- protocols/{bytes => encoding}/decoder.go | 2 +- protocols/{bytes => encoding}/encoder.go | 2 +- protocols/{bytes => encoding}/encoding.go | 4 +- protocols/encoding/encoding_test.go | 31 +++++++++ protocols/{models => translation}/decoder.go | 4 +- protocols/{models => translation}/encoder.go | 2 +- .../models.go => translation/translation.go} | 8 +-- protocols/translation/translation_test.go | 28 +++++++++ 11 files changed, 72 insertions(+), 139 deletions(-) delete mode 100644 protocols/decoder.go delete mode 100644 protocols/encoder.go rename protocols/{bytes => encoding}/decoder.go (98%) rename protocols/{bytes => encoding}/encoder.go (98%) rename protocols/{bytes => encoding}/encoding.go (98%) create mode 100644 protocols/encoding/encoding_test.go rename protocols/{models => translation}/decoder.go (98%) rename protocols/{models => translation}/encoder.go (98%) rename protocols/{models/models.go => translation/translation.go} (88%) create mode 100644 protocols/translation/translation_test.go diff --git a/protocols/README.md b/protocols/README.md index 870618da5e9..681cb95adba 100644 --- a/protocols/README.md +++ b/protocols/README.md @@ -2,8 +2,8 @@ This package provides common ways for decoding bytes into data models (e.g. Zipkin Span). These data models can then be decoded into internal pdata representations. Similarly, pdata can be encoded into a data model which can then be encoded into bytes. -[bytes](bytes): Common interfaces for encoding/decoding bytes from/to data models. +[encoding](encoding): Common interfaces for encoding/decoding bytes from/to data models. -[models](models): Common interfaces for encoding/decoding data models from/to pdata. +[translation](translation): Common interfaces for encoding/decoding data models from/to pdata. This package provides higher level APIs that do both encoding of bytes and data model if going directly pdata <-> bytes. \ No newline at end of file diff --git a/protocols/decoder.go b/protocols/decoder.go deleted file mode 100644 index 50b9c84510d..00000000000 --- a/protocols/decoder.go +++ /dev/null @@ -1,63 +0,0 @@ -// 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 protocols - -import ( - "go.opentelemetry.io/collector/consumer/pdata" - "go.opentelemetry.io/collector/protocols/bytes" - "go.opentelemetry.io/collector/protocols/models" -) - -type MetricsDecoder struct { - mod models.MetricsDecoder - enc bytes.MetricsDecoder -} - -type TracesDecoder struct { - mod models.TracesDecoder - enc bytes.TracesDecoder -} - -type LogsDecoder struct { - mod models.LogsDecoder - enc bytes.LogsDecoder -} - -// DecodeMetrics decodes bytes to pdata. -func (t *MetricsDecoder) DecodeMetrics(data []byte) (pdata.Metrics, error) { - model, err := t.enc.DecodeMetrics(data) - if err != nil { - return pdata.NewMetrics(), err - } - return t.mod.ToMetrics(model) -} - -// DecodeTraces decodes bytes to pdata. -func (t *TracesDecoder) DecodeTraces(data []byte) (pdata.Traces, error) { - model, err := t.enc.DecodeTraces(data) - if err != nil { - return pdata.NewTraces(), err - } - return t.mod.ToTraces(model) -} - -// DecodeLogs decodes bytes to pdata. -func (t *LogsDecoder) DecodeLogs(data []byte) (pdata.Logs, error) { - model, err := t.enc.DecodeLogs(data) - if err != nil { - return pdata.NewLogs(), err - } - return t.mod.ToLogs(model) -} \ No newline at end of file diff --git a/protocols/encoder.go b/protocols/encoder.go deleted file mode 100644 index 121c1d5e0e8..00000000000 --- a/protocols/encoder.go +++ /dev/null @@ -1,63 +0,0 @@ -// 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 protocols - -import ( - "go.opentelemetry.io/collector/consumer/pdata" - "go.opentelemetry.io/collector/protocols/models" - "go.opentelemetry.io/collector/protocols/bytes" -) - -type MetricsEncoder struct { - mod models.MetricsEncoder - enc bytes.MetricsEncoder -} - -type TracesEncoder struct { - mod models.TracesEncoder - enc bytes.TracesEncoder -} - -type LogsEncoder struct { - mod models.LogsEncoder - enc bytes.LogsEncoder -} - -// EncodeMetrics encodes pdata to bytes. -func (t *MetricsEncoder) EncodeMetrics(td pdata.Metrics) ([]byte, error) { - out := t.mod.Type() - if err := t.mod.FromMetrics(td, &out); err != nil { - return nil, err - } - return t.enc.EncodeMetrics(out) -} - -// EncodeTraces encodes pdata to bytes. -func (t *TracesEncoder) EncodeTraces(td pdata.Traces) ([]byte, error) { - out := t.mod.Type() - if err := t.mod.FromTraces(td, &out); err != nil { - return nil, err - } - return t.enc.EncodeTraces(out) -} - -// EncodeLogs encodes pdata to bytes. -func (t *LogsEncoder) EncodeLogs(td pdata.Logs) ([]byte, error) { - out := t.mod.Type() - if err := t.mod.FromLogs(td, &out); err != nil { - return nil, err - } - return t.enc.EncodeLogs(out) -} diff --git a/protocols/bytes/decoder.go b/protocols/encoding/decoder.go similarity index 98% rename from protocols/bytes/decoder.go rename to protocols/encoding/decoder.go index 021b22c4cea..e2a65ee3ae2 100644 --- a/protocols/bytes/decoder.go +++ b/protocols/encoding/decoder.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package bytes +package encoding // MetricsDecoder decodes bytes into data model. type MetricsDecoder interface { diff --git a/protocols/bytes/encoder.go b/protocols/encoding/encoder.go similarity index 98% rename from protocols/bytes/encoder.go rename to protocols/encoding/encoder.go index 2fd6ae51fbe..81d1d93d0b3 100644 --- a/protocols/bytes/encoder.go +++ b/protocols/encoding/encoder.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package bytes +package encoding // MetricsEncoder encodes data model into bytes. type MetricsEncoder interface { diff --git a/protocols/bytes/encoding.go b/protocols/encoding/encoding.go similarity index 98% rename from protocols/bytes/encoding.go rename to protocols/encoding/encoding.go index ea6d7e8fb01..a055cae0ea6 100644 --- a/protocols/bytes/encoding.go +++ b/protocols/encoding/encoding.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package bytes +package encoding import "fmt" @@ -40,4 +40,4 @@ func (e *ErrUnavailableEncoding) Error() string { func NewErrUnavailableEncoding(encoding Type) *ErrUnavailableEncoding { return &ErrUnavailableEncoding{encoding: encoding} -} \ No newline at end of file +} diff --git a/protocols/encoding/encoding_test.go b/protocols/encoding/encoding_test.go new file mode 100644 index 00000000000..eda9badaa6d --- /dev/null +++ b/protocols/encoding/encoding_test.go @@ -0,0 +1,31 @@ +// 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 encoding + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewErrUnavailableEncoding(t *testing.T) { + err := NewErrUnavailableEncoding("unknown") + assert.IsType(t, &ErrUnavailableEncoding{}, err) + assert.EqualError(t, err, `unsupported encoding "unknown"`) +} + +func TestType_String(t *testing.T) { + assert.Equal(t, "protobuf", Protobuf.String()) +} diff --git a/protocols/models/decoder.go b/protocols/translation/decoder.go similarity index 98% rename from protocols/models/decoder.go rename to protocols/translation/decoder.go index cc0902e26de..40a484097ea 100644 --- a/protocols/models/decoder.go +++ b/protocols/translation/decoder.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package models +package translation import "go.opentelemetry.io/collector/consumer/pdata" @@ -35,4 +35,4 @@ type LogsDecoder interface { ToLogs(src interface{}) (pdata.Logs, error) // Type returns an instance of the model. Type() interface{} -} \ No newline at end of file +} diff --git a/protocols/models/encoder.go b/protocols/translation/encoder.go similarity index 98% rename from protocols/models/encoder.go rename to protocols/translation/encoder.go index 3e8ee061594..743d965ce68 100644 --- a/protocols/models/encoder.go +++ b/protocols/translation/encoder.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package models +package translation import "go.opentelemetry.io/collector/consumer/pdata" diff --git a/protocols/models/models.go b/protocols/translation/translation.go similarity index 88% rename from protocols/models/models.go rename to protocols/translation/translation.go index 2963393fede..f77ed8953ef 100644 --- a/protocols/models/models.go +++ b/protocols/translation/translation.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package models +package translation import ( "fmt" @@ -20,12 +20,12 @@ import ( // ErrIncompatibleType details a type conversion error during translation. type ErrIncompatibleType struct { - given interface{} + given interface{} expected interface{} } func (i *ErrIncompatibleType) Error() string { - return fmt.Sprintf("model type %T is expected but given %T ", i.expected, i.given) + return fmt.Sprintf("model type %T is expected but given %T", i.expected, i.given) } // NewErrIncompatibleType returns ErrIncompatibleType instance @@ -34,4 +34,4 @@ func NewErrIncompatibleType(expected, given interface{}) *ErrIncompatibleType { given: given, expected: expected, } -} \ No newline at end of file +} diff --git a/protocols/translation/translation_test.go b/protocols/translation/translation_test.go new file mode 100644 index 00000000000..6fb0e1fec70 --- /dev/null +++ b/protocols/translation/translation_test.go @@ -0,0 +1,28 @@ +// 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 translation + +import ( + "testing" + + zipkinmodel "github.com/openzipkin/zipkin-go/model" + "github.com/stretchr/testify/assert" +) + +func TestNewErrIncompatibleType(t *testing.T) { + err := NewErrIncompatibleType([]*zipkinmodel.SpanModel{}, "given") + assert.IsType(t, &ErrIncompatibleType{}, err) + assert.EqualError(t, err, "model type []*model.SpanModel is expected but given string") +} From dd2068dac5f10c43f1df04dfc6d7aff09001b391 Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Wed, 19 May 2021 17:42:41 -0400 Subject: [PATCH 07/15] Apply suggestions from code review Co-authored-by: Tigran Najaryan <4194920+tigrannajaryan@users.noreply.github.com> --- protocols/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocols/README.md b/protocols/README.md index 681cb95adba..231c50580a8 100644 --- a/protocols/README.md +++ b/protocols/README.md @@ -1,9 +1,9 @@ # Protocols -This package provides common ways for decoding bytes into data models (e.g. Zipkin Span). These data models can then be decoded into internal pdata representations. Similarly, pdata can be encoded into a data model which can then be encoded into bytes. +This package provides common ways for decoding serialized bytes into protocol-specific in-memory data models (e.g. Zipkin Span). These data models can then be decoded into internal pdata representations. Similarly, pdata can be encoded into a data model which can then be encoded into bytes. [encoding](encoding): Common interfaces for encoding/decoding bytes from/to data models. [translation](translation): Common interfaces for encoding/decoding data models from/to pdata. -This package provides higher level APIs that do both encoding of bytes and data model if going directly pdata <-> bytes. \ No newline at end of file +This package provides higher level APIs that do both encoding of bytes and data model if going directly pdata <-> bytes. From 2cdfcc4cdd4abc5c1840204d84cbbad33c779a2f Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Wed, 19 May 2021 17:50:01 -0400 Subject: [PATCH 08/15] renamings --- model/README.md | 9 +++++++++ .../serializer/deserialize.go | 18 +++++++++--------- .../serializer/serialize.go | 18 +++++++++--------- .../serializer/serializer.go | 16 ++++++++-------- .../serializer/serializer_test.go | 0 .../translator}/decoder.go | 10 +++++----- .../translator}/encoder.go | 12 ++++++------ .../translator/translator.go | 0 .../translator/translator_test.go | 0 protocols/README.md | 9 --------- 10 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 model/README.md rename protocols/encoding/encoder.go => model/serializer/deserialize.go (59%) rename protocols/encoding/decoder.go => model/serializer/serialize.go (60%) rename protocols/encoding/encoding.go => model/serializer/serializer.go (75%) rename protocols/encoding/encoding_test.go => model/serializer/serializer_test.go (100%) rename {protocols/translation => model/translator}/decoder.go (87%) rename {protocols/translation => model/translator}/encoder.go (83%) rename protocols/translation/translation.go => model/translator/translator.go (100%) rename protocols/translation/translation_test.go => model/translator/translator_test.go (100%) delete mode 100644 protocols/README.md diff --git a/model/README.md b/model/README.md new file mode 100644 index 00000000000..7b468d5a43b --- /dev/null +++ b/model/README.md @@ -0,0 +1,9 @@ +# Protocols + +This package provides common ways for decoding serialized bytes into protocol-specific in-memory data models (e.g. Zipkin Span). These data models can then be translated to internal pdata representations. Similarly, pdata can be translated from a data model which can then be serialized into bytes. + +[serializer](serializer): Common interfaces for serializing/deserializing bytes from/to data models. + +[translator](translator): Common interfaces for translating protocol-specific data models from/to pdata. + +This package provides higher level APIs that do both encoding of bytes and data model if going directly pdata ⇔ bytes. diff --git a/protocols/encoding/encoder.go b/model/serializer/deserialize.go similarity index 59% rename from protocols/encoding/encoder.go rename to model/serializer/deserialize.go index 81d1d93d0b3..90a5e3e7d97 100644 --- a/protocols/encoding/encoder.go +++ b/model/serializer/deserialize.go @@ -14,17 +14,17 @@ package encoding -// MetricsEncoder encodes data model into bytes. -type MetricsEncoder interface { - EncodeMetrics(model interface{}) ([]byte, error) +// MetricsDeserializer decodes bytes into data model. +type MetricsDeserializer interface { + DeserializeMetrics(bytes []byte) (interface{}, error) } -// TracesEncoder encodes data model into bytes. -type TracesEncoder interface { - EncodeTraces(model interface{}) ([]byte, error) +// TracesDeserializer decodes bytes into data model. +type TracesDeserializer interface { + DeserializeTraces(bytes []byte) (interface{}, error) } -// LogsEncoder encodes data model into bytes. -type LogsEncoder interface { - EncodeLogs(model interface{}) ([]byte, error) +// LogsDeserializer decodes bytes into data model. +type LogsDeserializer interface { + DeserializeLogs(bytes []byte) (interface{}, error) } diff --git a/protocols/encoding/decoder.go b/model/serializer/serialize.go similarity index 60% rename from protocols/encoding/decoder.go rename to model/serializer/serialize.go index e2a65ee3ae2..660b38a5ca4 100644 --- a/protocols/encoding/decoder.go +++ b/model/serializer/serialize.go @@ -14,17 +14,17 @@ package encoding -// MetricsDecoder decodes bytes into data model. -type MetricsDecoder interface { - DecodeMetrics(bytes []byte) (interface{}, error) +// MetricsSerializer encodes data model into bytes. +type MetricsSerializer interface { + SerializeMetrics(model interface{}) ([]byte, error) } -// TracesDecoder decodes bytes into data model. -type TracesDecoder interface { - DecodeTraces(bytes []byte) (interface{}, error) +// TracesSerializer encodes data model into bytes. +type TracesSerializer interface { + SerializeTraces(model interface{}) ([]byte, error) } -// LogsDecoder decodes bytes into data model. -type LogsDecoder interface { - DecodeLogs(bytes []byte) (interface{}, error) +// LogsSerializer encodes data model into bytes. +type LogsSerializer interface { + SerializeLogs(model interface{}) ([]byte, error) } diff --git a/protocols/encoding/encoding.go b/model/serializer/serializer.go similarity index 75% rename from protocols/encoding/encoding.go rename to model/serializer/serializer.go index a055cae0ea6..791737e27d8 100644 --- a/protocols/encoding/encoding.go +++ b/model/serializer/serializer.go @@ -16,28 +16,28 @@ package encoding import "fmt" -// Type is the encoding format that a model is serialized to. -type Type string +// Encoding is the encoding format that a model is serialized to. +type Encoding string const ( - Protobuf Type = "protobuf" - JSON Type = "json" - Thrift Type = "thrift" + Protobuf Encoding = "protobuf" + JSON Encoding = "json" + Thrift Encoding = "thrift" ) -func (e Type) String() string { +func (e Encoding) String() string { return string(e) } // ErrUnavailableEncoding is returned when the requested encoding is not supported. type ErrUnavailableEncoding struct { - encoding Type + encoding Encoding } func (e *ErrUnavailableEncoding) Error() string { return fmt.Sprintf("unsupported encoding %q", e.encoding) } -func NewErrUnavailableEncoding(encoding Type) *ErrUnavailableEncoding { +func NewErrUnavailableEncoding(encoding Encoding) *ErrUnavailableEncoding { return &ErrUnavailableEncoding{encoding: encoding} } diff --git a/protocols/encoding/encoding_test.go b/model/serializer/serializer_test.go similarity index 100% rename from protocols/encoding/encoding_test.go rename to model/serializer/serializer_test.go diff --git a/protocols/translation/decoder.go b/model/translator/decoder.go similarity index 87% rename from protocols/translation/decoder.go rename to model/translator/decoder.go index 40a484097ea..ff09534730f 100644 --- a/protocols/translation/decoder.go +++ b/model/translator/decoder.go @@ -20,19 +20,19 @@ type MetricsDecoder interface { // ToMetrics converts a data model of another protocol into pdata. ToMetrics(src interface{}) (pdata.Metrics, error) // Type returns an instance of the model. - Type() interface{} + NewModel() interface{} } type TracesDecoder interface { // ToTraces converts a data model of another protocol into pdata. ToTraces(src interface{}) (pdata.Traces, error) - // Type returns an instance of the model. - Type() interface{} + // NewModel returns an instance of the model. + NewModel() interface{} } type LogsDecoder interface { // ToLogs converts a data model of another protocol into pdata. ToLogs(src interface{}) (pdata.Logs, error) - // Type returns an instance of the model. - Type() interface{} + // NewModel returns an instance of the model. + NewModel() interface{} } diff --git a/protocols/translation/encoder.go b/model/translator/encoder.go similarity index 83% rename from protocols/translation/encoder.go rename to model/translator/encoder.go index 743d965ce68..a60a4ad5257 100644 --- a/protocols/translation/encoder.go +++ b/model/translator/encoder.go @@ -19,20 +19,20 @@ import "go.opentelemetry.io/collector/consumer/pdata" type MetricsEncoder interface { // FromMetrics converts pdata to data model. FromMetrics(md pdata.Metrics, out interface{}) error - // Type returns an instance of the model. - Type() interface{} + // NewModel returns an instance of the model. + NewModel() interface{} } type TracesEncoder interface { // FromTraces converts pdata to data model. FromTraces(md pdata.Traces, out interface{}) error - // Type returns an instance of the model. - Type() interface{} + // NewModel returns an instance of the model. + NewModel() interface{} } type LogsEncoder interface { // FromLogs converts pdata to data model. FromLogs(md pdata.Logs, out interface{}) error - // Type returns an instance of the model. - Type() interface{} + // NewModel returns an instance of the model. + NewModel() interface{} } diff --git a/protocols/translation/translation.go b/model/translator/translator.go similarity index 100% rename from protocols/translation/translation.go rename to model/translator/translator.go diff --git a/protocols/translation/translation_test.go b/model/translator/translator_test.go similarity index 100% rename from protocols/translation/translation_test.go rename to model/translator/translator_test.go diff --git a/protocols/README.md b/protocols/README.md deleted file mode 100644 index 231c50580a8..00000000000 --- a/protocols/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Protocols - -This package provides common ways for decoding serialized bytes into protocol-specific in-memory data models (e.g. Zipkin Span). These data models can then be decoded into internal pdata representations. Similarly, pdata can be encoded into a data model which can then be encoded into bytes. - -[encoding](encoding): Common interfaces for encoding/decoding bytes from/to data models. - -[translation](translation): Common interfaces for encoding/decoding data models from/to pdata. - -This package provides higher level APIs that do both encoding of bytes and data model if going directly pdata <-> bytes. From 78a9a4c18889b56ece7aa72a1d1f58f5112e00db Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Wed, 19 May 2021 18:13:13 -0400 Subject: [PATCH 09/15] reword error --- model/serializer/serializer_test.go | 2 +- model/translator/translator.go | 2 +- model/translator/translator_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/model/serializer/serializer_test.go b/model/serializer/serializer_test.go index eda9badaa6d..db052ed7eb1 100644 --- a/model/serializer/serializer_test.go +++ b/model/serializer/serializer_test.go @@ -26,6 +26,6 @@ func TestNewErrUnavailableEncoding(t *testing.T) { assert.EqualError(t, err, `unsupported encoding "unknown"`) } -func TestType_String(t *testing.T) { +func TestEncoding_String(t *testing.T) { assert.Equal(t, "protobuf", Protobuf.String()) } diff --git a/model/translator/translator.go b/model/translator/translator.go index f77ed8953ef..29159eb13ce 100644 --- a/model/translator/translator.go +++ b/model/translator/translator.go @@ -25,7 +25,7 @@ type ErrIncompatibleType struct { } func (i *ErrIncompatibleType) Error() string { - return fmt.Sprintf("model type %T is expected but given %T", i.expected, i.given) + return fmt.Sprintf("expected model type %T but given %T", i.expected, i.given) } // NewErrIncompatibleType returns ErrIncompatibleType instance diff --git a/model/translator/translator_test.go b/model/translator/translator_test.go index 6fb0e1fec70..00d174b7fd1 100644 --- a/model/translator/translator_test.go +++ b/model/translator/translator_test.go @@ -24,5 +24,5 @@ import ( func TestNewErrIncompatibleType(t *testing.T) { err := NewErrIncompatibleType([]*zipkinmodel.SpanModel{}, "given") assert.IsType(t, &ErrIncompatibleType{}, err) - assert.EqualError(t, err, "model type []*model.SpanModel is expected but given string") + assert.EqualError(t, err, "expected model type []*model.SpanModel but given string") } From a1f47362fe7e12ec242da740dd64ce43375007cf Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Wed, 19 May 2021 18:19:06 -0400 Subject: [PATCH 10/15] cleanup --- model/README.md | 2 +- model/serializer/deserialize.go | 6 +++--- model/serializer/serialize.go | 6 +++--- model/translator/decoder.go | 8 ++++---- model/translator/encoder.go | 6 +++--- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/model/README.md b/model/README.md index 7b468d5a43b..e7dd5eac5aa 100644 --- a/model/README.md +++ b/model/README.md @@ -2,7 +2,7 @@ This package provides common ways for decoding serialized bytes into protocol-specific in-memory data models (e.g. Zipkin Span). These data models can then be translated to internal pdata representations. Similarly, pdata can be translated from a data model which can then be serialized into bytes. -[serializer](serializer): Common interfaces for serializing/deserializing bytes from/to data models. +[serializer](serializer): Common interfaces for serializing/deserializing bytes from/to protocol-specific data models. [translator](translator): Common interfaces for translating protocol-specific data models from/to pdata. diff --git a/model/serializer/deserialize.go b/model/serializer/deserialize.go index 90a5e3e7d97..7528955c17a 100644 --- a/model/serializer/deserialize.go +++ b/model/serializer/deserialize.go @@ -14,17 +14,17 @@ package encoding -// MetricsDeserializer decodes bytes into data model. +// MetricsDeserializer decodes bytes into protocol-specific data model. type MetricsDeserializer interface { DeserializeMetrics(bytes []byte) (interface{}, error) } -// TracesDeserializer decodes bytes into data model. +// TracesDeserializer decodes bytes into protocol-specific data model. type TracesDeserializer interface { DeserializeTraces(bytes []byte) (interface{}, error) } -// LogsDeserializer decodes bytes into data model. +// LogsDeserializer decodes bytes into protocol-specific data model. type LogsDeserializer interface { DeserializeLogs(bytes []byte) (interface{}, error) } diff --git a/model/serializer/serialize.go b/model/serializer/serialize.go index 660b38a5ca4..57448773f8b 100644 --- a/model/serializer/serialize.go +++ b/model/serializer/serialize.go @@ -14,17 +14,17 @@ package encoding -// MetricsSerializer encodes data model into bytes. +// MetricsSerializer encodes protocol-specific data model into bytes. type MetricsSerializer interface { SerializeMetrics(model interface{}) ([]byte, error) } -// TracesSerializer encodes data model into bytes. +// TracesSerializer encodes protocol-specific data model into bytes. type TracesSerializer interface { SerializeTraces(model interface{}) ([]byte, error) } -// LogsSerializer encodes data model into bytes. +// LogsSerializer encodes protocol-specific data model into bytes. type LogsSerializer interface { SerializeLogs(model interface{}) ([]byte, error) } diff --git a/model/translator/decoder.go b/model/translator/decoder.go index ff09534730f..5e75d835d39 100644 --- a/model/translator/decoder.go +++ b/model/translator/decoder.go @@ -17,21 +17,21 @@ package translation import "go.opentelemetry.io/collector/consumer/pdata" type MetricsDecoder interface { - // ToMetrics converts a data model of another protocol into pdata. + // ToMetrics converts a protocol-specific data model into pdata. ToMetrics(src interface{}) (pdata.Metrics, error) - // Type returns an instance of the model. + // NewModel returns an instance of the model. NewModel() interface{} } type TracesDecoder interface { - // ToTraces converts a data model of another protocol into pdata. + // ToTraces converts a protocol-specific data model into pdata. ToTraces(src interface{}) (pdata.Traces, error) // NewModel returns an instance of the model. NewModel() interface{} } type LogsDecoder interface { - // ToLogs converts a data model of another protocol into pdata. + // ToLogs converts a protocol-specific data model into pdata. ToLogs(src interface{}) (pdata.Logs, error) // NewModel returns an instance of the model. NewModel() interface{} diff --git a/model/translator/encoder.go b/model/translator/encoder.go index a60a4ad5257..763292a51dd 100644 --- a/model/translator/encoder.go +++ b/model/translator/encoder.go @@ -17,21 +17,21 @@ package translation import "go.opentelemetry.io/collector/consumer/pdata" type MetricsEncoder interface { - // FromMetrics converts pdata to data model. + // FromMetrics converts pdata to protocol-specific data model. FromMetrics(md pdata.Metrics, out interface{}) error // NewModel returns an instance of the model. NewModel() interface{} } type TracesEncoder interface { - // FromTraces converts pdata to data model. + // FromTraces converts pdata to protocol-specific data model. FromTraces(md pdata.Traces, out interface{}) error // NewModel returns an instance of the model. NewModel() interface{} } type LogsEncoder interface { - // FromLogs converts pdata to data model. + // FromLogs converts pdata to protocol-specific data model. FromLogs(md pdata.Logs, out interface{}) error // NewModel returns an instance of the model. NewModel() interface{} From b38458887a6eac88e10e7ac679ef93d8f2a327d9 Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Wed, 19 May 2021 19:39:56 -0400 Subject: [PATCH 11/15] return interface instead of out parameter --- model/translator/decoder.go | 6 ------ model/translator/encoder.go | 12 +++--------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/model/translator/decoder.go b/model/translator/decoder.go index 5e75d835d39..70262702e83 100644 --- a/model/translator/decoder.go +++ b/model/translator/decoder.go @@ -19,20 +19,14 @@ import "go.opentelemetry.io/collector/consumer/pdata" type MetricsDecoder interface { // ToMetrics converts a protocol-specific data model into pdata. ToMetrics(src interface{}) (pdata.Metrics, error) - // NewModel returns an instance of the model. - NewModel() interface{} } type TracesDecoder interface { // ToTraces converts a protocol-specific data model into pdata. ToTraces(src interface{}) (pdata.Traces, error) - // NewModel returns an instance of the model. - NewModel() interface{} } type LogsDecoder interface { // ToLogs converts a protocol-specific data model into pdata. ToLogs(src interface{}) (pdata.Logs, error) - // NewModel returns an instance of the model. - NewModel() interface{} } diff --git a/model/translator/encoder.go b/model/translator/encoder.go index 763292a51dd..7fdd0ae19bb 100644 --- a/model/translator/encoder.go +++ b/model/translator/encoder.go @@ -18,21 +18,15 @@ import "go.opentelemetry.io/collector/consumer/pdata" type MetricsEncoder interface { // FromMetrics converts pdata to protocol-specific data model. - FromMetrics(md pdata.Metrics, out interface{}) error - // NewModel returns an instance of the model. - NewModel() interface{} + FromMetrics(md pdata.Metrics) (interface{}, error) } type TracesEncoder interface { // FromTraces converts pdata to protocol-specific data model. - FromTraces(md pdata.Traces, out interface{}) error - // NewModel returns an instance of the model. - NewModel() interface{} + FromTraces(td pdata.Traces) (interface{}, error) } type LogsEncoder interface { // FromLogs converts pdata to protocol-specific data model. - FromLogs(md pdata.Logs, out interface{}) error - // NewModel returns an instance of the model. - NewModel() interface{} + FromLogs(ld pdata.Logs) (interface{}, error) } From 1aa9a13132ece638afe1ae10de544ba3b2752e1d Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Wed, 19 May 2021 19:48:12 -0400 Subject: [PATCH 12/15] review feedback --- model/serializer/deserialize.go | 8 ++++---- model/serializer/serialize.go | 2 +- model/serializer/serializer.go | 2 +- model/serializer/serializer_test.go | 2 +- model/translator/decoder.go | 2 +- model/translator/encoder.go | 2 +- model/translator/translator.go | 14 +++++++------- model/translator/translator_test.go | 4 ++-- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/model/serializer/deserialize.go b/model/serializer/deserialize.go index 7528955c17a..44af06bb4cb 100644 --- a/model/serializer/deserialize.go +++ b/model/serializer/deserialize.go @@ -12,19 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -package encoding +package serializer // MetricsDeserializer decodes bytes into protocol-specific data model. type MetricsDeserializer interface { - DeserializeMetrics(bytes []byte) (interface{}, error) + DeserializeMetrics(buf []byte) (interface{}, error) } // TracesDeserializer decodes bytes into protocol-specific data model. type TracesDeserializer interface { - DeserializeTraces(bytes []byte) (interface{}, error) + DeserializeTraces(buf []byte) (interface{}, error) } // LogsDeserializer decodes bytes into protocol-specific data model. type LogsDeserializer interface { - DeserializeLogs(bytes []byte) (interface{}, error) + DeserializeLogs(buf []byte) (interface{}, error) } diff --git a/model/serializer/serialize.go b/model/serializer/serialize.go index 57448773f8b..ad4d487e876 100644 --- a/model/serializer/serialize.go +++ b/model/serializer/serialize.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package encoding +package serializer // MetricsSerializer encodes protocol-specific data model into bytes. type MetricsSerializer interface { diff --git a/model/serializer/serializer.go b/model/serializer/serializer.go index 791737e27d8..11cfa08c8c7 100644 --- a/model/serializer/serializer.go +++ b/model/serializer/serializer.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package encoding +package serializer import "fmt" diff --git a/model/serializer/serializer_test.go b/model/serializer/serializer_test.go index db052ed7eb1..50f00591d9e 100644 --- a/model/serializer/serializer_test.go +++ b/model/serializer/serializer_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package encoding +package serializer import ( "testing" diff --git a/model/translator/decoder.go b/model/translator/decoder.go index 70262702e83..5159db5575a 100644 --- a/model/translator/decoder.go +++ b/model/translator/decoder.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package translation +package translator import "go.opentelemetry.io/collector/consumer/pdata" diff --git a/model/translator/encoder.go b/model/translator/encoder.go index 7fdd0ae19bb..fc1f8f1e3c4 100644 --- a/model/translator/encoder.go +++ b/model/translator/encoder.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package translation +package translator import "go.opentelemetry.io/collector/consumer/pdata" diff --git a/model/translator/translator.go b/model/translator/translator.go index 29159eb13ce..8b5b85a42e9 100644 --- a/model/translator/translator.go +++ b/model/translator/translator.go @@ -12,25 +12,25 @@ // See the License for the specific language governing permissions and // limitations under the License. -package translation +package translator import ( "fmt" ) -// ErrIncompatibleType details a type conversion error during translation. -type ErrIncompatibleType struct { +// errIncompatibleType details a type conversion error during translation. +type errIncompatibleType struct { given interface{} expected interface{} } -func (i *ErrIncompatibleType) Error() string { +func (i *errIncompatibleType) Error() string { return fmt.Sprintf("expected model type %T but given %T", i.expected, i.given) } -// NewErrIncompatibleType returns ErrIncompatibleType instance -func NewErrIncompatibleType(expected, given interface{}) *ErrIncompatibleType { - return &ErrIncompatibleType{ +// NewErrIncompatibleType returns errIncompatibleType instance +func NewErrIncompatibleType(expected, given interface{}) *errIncompatibleType { + return &errIncompatibleType{ given: given, expected: expected, } diff --git a/model/translator/translator_test.go b/model/translator/translator_test.go index 00d174b7fd1..f4581861e32 100644 --- a/model/translator/translator_test.go +++ b/model/translator/translator_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package translation +package translator import ( "testing" @@ -23,6 +23,6 @@ import ( func TestNewErrIncompatibleType(t *testing.T) { err := NewErrIncompatibleType([]*zipkinmodel.SpanModel{}, "given") - assert.IsType(t, &ErrIncompatibleType{}, err) + assert.IsType(t, &errIncompatibleType{}, err) assert.EqualError(t, err, "expected model type []*model.SpanModel but given string") } From 8cadcad0e53f9aae7881d15d87ad528a75501531 Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Wed, 19 May 2021 23:15:55 -0400 Subject: [PATCH 13/15] serialize -> marshal --- model/serializer/deserialize.go | 18 +++++++++--------- model/serializer/serialize.go | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/model/serializer/deserialize.go b/model/serializer/deserialize.go index 44af06bb4cb..49e9706e21a 100644 --- a/model/serializer/deserialize.go +++ b/model/serializer/deserialize.go @@ -14,17 +14,17 @@ package serializer -// MetricsDeserializer decodes bytes into protocol-specific data model. -type MetricsDeserializer interface { - DeserializeMetrics(buf []byte) (interface{}, error) +// MetricsUnmarshaler decodes bytes into protocol-specific data model. +type MetricsUnmarshaler interface { + UnmarshalMetrics(buf []byte) (interface{}, error) } -// TracesDeserializer decodes bytes into protocol-specific data model. -type TracesDeserializer interface { - DeserializeTraces(buf []byte) (interface{}, error) +// TracesUnmarshaler decodes bytes into protocol-specific data model. +type TracesUnmarshaler interface { + UnmarshalTraces(buf []byte) (interface{}, error) } -// LogsDeserializer decodes bytes into protocol-specific data model. -type LogsDeserializer interface { - DeserializeLogs(buf []byte) (interface{}, error) +// LogsUnmarshaler decodes bytes into protocol-specific data model. +type LogsUnmarshaler interface { + UnmarshalLogs(buf []byte) (interface{}, error) } diff --git a/model/serializer/serialize.go b/model/serializer/serialize.go index ad4d487e876..6438c33a96a 100644 --- a/model/serializer/serialize.go +++ b/model/serializer/serialize.go @@ -14,17 +14,17 @@ package serializer -// MetricsSerializer encodes protocol-specific data model into bytes. -type MetricsSerializer interface { - SerializeMetrics(model interface{}) ([]byte, error) +// MetricsMarshaler encodes protocol-specific data model into bytes. +type MetricsMarshaler interface { + MarshalMetrics(model interface{}) ([]byte, error) } -// TracesSerializer encodes protocol-specific data model into bytes. -type TracesSerializer interface { - SerializeTraces(model interface{}) ([]byte, error) +// TracesMarshaler encodes protocol-specific data model into bytes. +type TracesMarshaler interface { + MarshalTraces(model interface{}) ([]byte, error) } -// LogsSerializer encodes protocol-specific data model into bytes. -type LogsSerializer interface { - SerializeLogs(model interface{}) ([]byte, error) +// LogsMarshaler encodes protocol-specific data model into bytes. +type LogsMarshaler interface { + MarshalLogs(model interface{}) ([]byte, error) } From 3460c5d78e0d6d3ce90a71b5368bef56f41f4fc4 Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Thu, 20 May 2021 12:29:26 -0400 Subject: [PATCH 14/15] put in internal --- {model => internal/model}/README.md | 0 {model => internal/model}/serializer/deserialize.go | 0 {model => internal/model}/serializer/serialize.go | 0 {model => internal/model}/serializer/serializer.go | 0 {model => internal/model}/serializer/serializer_test.go | 0 {model => internal/model}/translator/decoder.go | 0 {model => internal/model}/translator/encoder.go | 0 {model => internal/model}/translator/translator.go | 0 {model => internal/model}/translator/translator_test.go | 0 9 files changed, 0 insertions(+), 0 deletions(-) rename {model => internal/model}/README.md (100%) rename {model => internal/model}/serializer/deserialize.go (100%) rename {model => internal/model}/serializer/serialize.go (100%) rename {model => internal/model}/serializer/serializer.go (100%) rename {model => internal/model}/serializer/serializer_test.go (100%) rename {model => internal/model}/translator/decoder.go (100%) rename {model => internal/model}/translator/encoder.go (100%) rename {model => internal/model}/translator/translator.go (100%) rename {model => internal/model}/translator/translator_test.go (100%) diff --git a/model/README.md b/internal/model/README.md similarity index 100% rename from model/README.md rename to internal/model/README.md diff --git a/model/serializer/deserialize.go b/internal/model/serializer/deserialize.go similarity index 100% rename from model/serializer/deserialize.go rename to internal/model/serializer/deserialize.go diff --git a/model/serializer/serialize.go b/internal/model/serializer/serialize.go similarity index 100% rename from model/serializer/serialize.go rename to internal/model/serializer/serialize.go diff --git a/model/serializer/serializer.go b/internal/model/serializer/serializer.go similarity index 100% rename from model/serializer/serializer.go rename to internal/model/serializer/serializer.go diff --git a/model/serializer/serializer_test.go b/internal/model/serializer/serializer_test.go similarity index 100% rename from model/serializer/serializer_test.go rename to internal/model/serializer/serializer_test.go diff --git a/model/translator/decoder.go b/internal/model/translator/decoder.go similarity index 100% rename from model/translator/decoder.go rename to internal/model/translator/decoder.go diff --git a/model/translator/encoder.go b/internal/model/translator/encoder.go similarity index 100% rename from model/translator/encoder.go rename to internal/model/translator/encoder.go diff --git a/model/translator/translator.go b/internal/model/translator/translator.go similarity index 100% rename from model/translator/translator.go rename to internal/model/translator/translator.go diff --git a/model/translator/translator_test.go b/internal/model/translator/translator_test.go similarity index 100% rename from model/translator/translator_test.go rename to internal/model/translator/translator_test.go From a4345e0180f30a4a571bf83f2c9f86a809f75a64 Mon Sep 17 00:00:00 2001 From: Jay Camp Date: Thu, 20 May 2021 12:53:07 -0400 Subject: [PATCH 15/15] lint --- internal/model/translator/translator.go | 17 ++--------------- internal/model/translator/translator_test.go | 1 - 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/internal/model/translator/translator.go b/internal/model/translator/translator.go index 8b5b85a42e9..f8ca2804e0d 100644 --- a/internal/model/translator/translator.go +++ b/internal/model/translator/translator.go @@ -18,20 +18,7 @@ import ( "fmt" ) -// errIncompatibleType details a type conversion error during translation. -type errIncompatibleType struct { - given interface{} - expected interface{} -} - -func (i *errIncompatibleType) Error() string { - return fmt.Sprintf("expected model type %T but given %T", i.expected, i.given) -} - // NewErrIncompatibleType returns errIncompatibleType instance -func NewErrIncompatibleType(expected, given interface{}) *errIncompatibleType { - return &errIncompatibleType{ - given: given, - expected: expected, - } +func NewErrIncompatibleType(expected, given interface{}) error { + return fmt.Errorf("expected model type %T but given %T", expected, given) } diff --git a/internal/model/translator/translator_test.go b/internal/model/translator/translator_test.go index f4581861e32..6b010e48a50 100644 --- a/internal/model/translator/translator_test.go +++ b/internal/model/translator/translator_test.go @@ -23,6 +23,5 @@ import ( func TestNewErrIncompatibleType(t *testing.T) { err := NewErrIncompatibleType([]*zipkinmodel.SpanModel{}, "given") - assert.IsType(t, &errIncompatibleType{}, err) assert.EqualError(t, err, "expected model type []*model.SpanModel but given string") }