Skip to content

Commit db093a6

Browse files
Introduce model translation and encoding interfaces (#3200)
* [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. * use encode/decode consistently * review feedback * 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. * 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 * cleanup * Apply suggestions from code review Co-authored-by: Tigran Najaryan <4194920+tigrannajaryan@users.noreply.github.com> * renamings * reword error * cleanup * return interface instead of out parameter * review feedback * serialize -> marshal * put in internal * lint Co-authored-by: Tigran Najaryan <4194920+tigrannajaryan@users.noreply.github.com>
1 parent f7674b2 commit db093a6

File tree

9 files changed

+258
-0
lines changed

9 files changed

+258
-0
lines changed

internal/model/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Protocols
2+
3+
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.
4+
5+
[serializer](serializer): Common interfaces for serializing/deserializing bytes from/to protocol-specific data models.
6+
7+
[translator](translator): Common interfaces for translating protocol-specific data models from/to pdata.
8+
9+
This package provides higher level APIs that do both encoding of bytes and data model if going directly pdata ⇔ bytes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package serializer
16+
17+
// MetricsUnmarshaler decodes bytes into protocol-specific data model.
18+
type MetricsUnmarshaler interface {
19+
UnmarshalMetrics(buf []byte) (interface{}, error)
20+
}
21+
22+
// TracesUnmarshaler decodes bytes into protocol-specific data model.
23+
type TracesUnmarshaler interface {
24+
UnmarshalTraces(buf []byte) (interface{}, error)
25+
}
26+
27+
// LogsUnmarshaler decodes bytes into protocol-specific data model.
28+
type LogsUnmarshaler interface {
29+
UnmarshalLogs(buf []byte) (interface{}, error)
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package serializer
16+
17+
// MetricsMarshaler encodes protocol-specific data model into bytes.
18+
type MetricsMarshaler interface {
19+
MarshalMetrics(model interface{}) ([]byte, error)
20+
}
21+
22+
// TracesMarshaler encodes protocol-specific data model into bytes.
23+
type TracesMarshaler interface {
24+
MarshalTraces(model interface{}) ([]byte, error)
25+
}
26+
27+
// LogsMarshaler encodes protocol-specific data model into bytes.
28+
type LogsMarshaler interface {
29+
MarshalLogs(model interface{}) ([]byte, error)
30+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package serializer
16+
17+
import "fmt"
18+
19+
// Encoding is the encoding format that a model is serialized to.
20+
type Encoding string
21+
22+
const (
23+
Protobuf Encoding = "protobuf"
24+
JSON Encoding = "json"
25+
Thrift Encoding = "thrift"
26+
)
27+
28+
func (e Encoding) String() string {
29+
return string(e)
30+
}
31+
32+
// ErrUnavailableEncoding is returned when the requested encoding is not supported.
33+
type ErrUnavailableEncoding struct {
34+
encoding Encoding
35+
}
36+
37+
func (e *ErrUnavailableEncoding) Error() string {
38+
return fmt.Sprintf("unsupported encoding %q", e.encoding)
39+
}
40+
41+
func NewErrUnavailableEncoding(encoding Encoding) *ErrUnavailableEncoding {
42+
return &ErrUnavailableEncoding{encoding: encoding}
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package serializer
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestNewErrUnavailableEncoding(t *testing.T) {
24+
err := NewErrUnavailableEncoding("unknown")
25+
assert.IsType(t, &ErrUnavailableEncoding{}, err)
26+
assert.EqualError(t, err, `unsupported encoding "unknown"`)
27+
}
28+
29+
func TestEncoding_String(t *testing.T) {
30+
assert.Equal(t, "protobuf", Protobuf.String())
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package translator
16+
17+
import "go.opentelemetry.io/collector/consumer/pdata"
18+
19+
type MetricsDecoder interface {
20+
// ToMetrics converts a protocol-specific data model into pdata.
21+
ToMetrics(src interface{}) (pdata.Metrics, error)
22+
}
23+
24+
type TracesDecoder interface {
25+
// ToTraces converts a protocol-specific data model into pdata.
26+
ToTraces(src interface{}) (pdata.Traces, error)
27+
}
28+
29+
type LogsDecoder interface {
30+
// ToLogs converts a protocol-specific data model into pdata.
31+
ToLogs(src interface{}) (pdata.Logs, error)
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package translator
16+
17+
import "go.opentelemetry.io/collector/consumer/pdata"
18+
19+
type MetricsEncoder interface {
20+
// FromMetrics converts pdata to protocol-specific data model.
21+
FromMetrics(md pdata.Metrics) (interface{}, error)
22+
}
23+
24+
type TracesEncoder interface {
25+
// FromTraces converts pdata to protocol-specific data model.
26+
FromTraces(td pdata.Traces) (interface{}, error)
27+
}
28+
29+
type LogsEncoder interface {
30+
// FromLogs converts pdata to protocol-specific data model.
31+
FromLogs(ld pdata.Logs) (interface{}, error)
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package translator
16+
17+
import (
18+
"fmt"
19+
)
20+
21+
// NewErrIncompatibleType returns errIncompatibleType instance
22+
func NewErrIncompatibleType(expected, given interface{}) error {
23+
return fmt.Errorf("expected model type %T but given %T", expected, given)
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package translator
16+
17+
import (
18+
"testing"
19+
20+
zipkinmodel "github.com/openzipkin/zipkin-go/model"
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestNewErrIncompatibleType(t *testing.T) {
25+
err := NewErrIncompatibleType([]*zipkinmodel.SpanModel{}, "given")
26+
assert.EqualError(t, err, "expected model type []*model.SpanModel but given string")
27+
}

0 commit comments

Comments
 (0)