Skip to content

Commit 95571ad

Browse files
committed
add high level interface
* standardized on encoding/decoding terminology * renamed encodings to serializing to avoid confusion with encode/decode terminology * added high level interfaces to top level protocols package that goes directly pdata <-> bytes
1 parent b647d08 commit 95571ad

File tree

12 files changed

+254
-111
lines changed

12 files changed

+254
-111
lines changed

protocols/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 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.
4+
5+
[serializing](serializing): Common interfaces for encoding/decoding bytes from/to data models.
6+
7+
[models](models): Common interfaces for encoding/decoding 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.

protocols/decoder.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 protocols
16+
17+
import (
18+
"go.opentelemetry.io/collector/consumer/pdata"
19+
"go.opentelemetry.io/collector/protocols/models"
20+
"go.opentelemetry.io/collector/protocols/serializing"
21+
)
22+
23+
type MetricsDecoder struct {
24+
mod models.MetricsDecoder
25+
enc serializing.MetricsDecoder
26+
}
27+
28+
type TracesDecoder struct {
29+
mod models.TracesDecoder
30+
enc serializing.TracesDecoder
31+
}
32+
33+
type LogsDecoder struct {
34+
mod models.LogsDecoder
35+
enc serializing.LogsDecoder
36+
}
37+
38+
// MetricsDecoder decodes bytes to pdata.
39+
func (t *MetricsDecoder) DecodeMetrics(data []byte) (pdata.Metrics, error) {
40+
model, err := t.enc.DecodeMetrics(data)
41+
if err != nil {
42+
return pdata.NewMetrics(), err
43+
}
44+
return t.mod.ToMetrics(model)
45+
}
46+
47+
// TracesDecoder decodes bytes to pdata.
48+
func (t *TracesDecoder) DecodeTraces(data []byte) (pdata.Traces, error) {
49+
model, err := t.enc.DecodeTraces(data)
50+
if err != nil {
51+
return pdata.NewTraces(), err
52+
}
53+
return t.mod.ToTraces(model)
54+
}
55+
56+
// LogsDecoder decodes bytes to pdata.
57+
func (t *LogsDecoder) DecodeLogs(data []byte) (pdata.Logs, error) {
58+
model, err := t.enc.DecodeLogs(data)
59+
if err != nil {
60+
return pdata.NewLogs(), err
61+
}
62+
return t.mod.ToLogs(model)
63+
}

protocols/encoder.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 protocols
16+
17+
import (
18+
"go.opentelemetry.io/collector/consumer/pdata"
19+
"go.opentelemetry.io/collector/protocols/models"
20+
"go.opentelemetry.io/collector/protocols/serializing"
21+
)
22+
23+
type MetricsEncoder struct {
24+
mod models.MetricsEncoder
25+
enc serializing.MetricsEncoder
26+
}
27+
28+
type TracesEncoder struct {
29+
mod models.TracesEncoder
30+
enc serializing.TracesEncoder
31+
}
32+
33+
type LogsEncoder struct {
34+
mod models.LogsEncoder
35+
enc serializing.LogsEncoder
36+
}
37+
38+
// EncodeMetrics encodes pdata to bytes.
39+
func (t *MetricsEncoder) EncodeMetrics(td pdata.Metrics) ([]byte, error) {
40+
out := t.mod.Type()
41+
if err := t.mod.FromMetrics(td, &out); err != nil {
42+
return nil, err
43+
}
44+
return t.enc.EncodeMetrics(out)
45+
}
46+
47+
// EncodeTraces encodes pdata to bytes.
48+
func (t *TracesEncoder) EncodeTraces(td pdata.Traces) ([]byte, error) {
49+
out := t.mod.Type()
50+
if err := t.mod.FromTraces(td, &out); err != nil {
51+
return nil, err
52+
}
53+
return t.enc.EncodeTraces(out)
54+
}
55+
56+
// EncodeLogs encodes pdata to bytes.
57+
func (t *LogsEncoder) EncodeLogs(td pdata.Logs) ([]byte, error) {
58+
out := t.mod.Type()
59+
if err := t.mod.FromLogs(td, &out); err != nil {
60+
return nil, err
61+
}
62+
return t.enc.EncodeLogs(out)
63+
}

protocols/models/decoder.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 models
16+
17+
import "go.opentelemetry.io/collector/consumer/pdata"
18+
19+
type MetricsDecoder interface {
20+
// ToMetrics converts a data model of another protocol into pdata.
21+
ToMetrics(src interface{}) (pdata.Metrics, error)
22+
// Type returns an instance of the model.
23+
Type() interface{}
24+
}
25+
26+
type TracesDecoder interface {
27+
// ToTraces converts a data model of another protocol into pdata.
28+
ToTraces(src interface{}) (pdata.Traces, error)
29+
// Type returns an instance of the model.
30+
Type() interface{}
31+
}
32+
33+
type LogsDecoder interface {
34+
// ToLogs converts a data model of another protocol into pdata.
35+
ToLogs(src interface{}) (pdata.Logs, error)
36+
// Type returns an instance of the model.
37+
Type() interface{}
38+
}

protocols/models/encoder.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 models
16+
17+
import "go.opentelemetry.io/collector/consumer/pdata"
18+
19+
type MetricsEncoder interface {
20+
// FromMetrics converts pdata to data model.
21+
FromMetrics(md pdata.Metrics, out interface{}) error
22+
// Type returns an instance of the model.
23+
Type() interface{}
24+
}
25+
26+
type TracesEncoder interface {
27+
// FromTraces converts pdata to data model.
28+
FromTraces(md pdata.Traces, out interface{}) error
29+
// Type returns an instance of the model.
30+
Type() interface{}
31+
}
32+
33+
type LogsEncoder interface {
34+
// FromLogs converts pdata to data model.
35+
FromLogs(md pdata.Logs, out interface{}) error
36+
// Type returns an instance of the model.
37+
Type() interface{}
38+
}

protocols/models/models.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,22 @@ package models
1616

1717
import (
1818
"fmt"
19-
"go.opentelemetry.io/collector/consumer/pdata"
2019
)
2120

22-
type MetricsModelTranslator interface {
23-
// MetricsFromModel converts a data model of another protocol into pdata.
24-
MetricsFromModel(src interface{}) (pdata.Metrics, error)
25-
// MetricsToModel converts pdata to data model.
26-
MetricsToModel(md pdata.Metrics, out interface{}) error
27-
}
28-
29-
type TracesModelTranslator interface {
30-
// TracesFromModel converts a data model of another protocol into pdata.
31-
TracesFromModel(src interface{}) (pdata.Traces, error)
32-
// TracesToModel converts pdata to data model.
33-
TracesToModel(md pdata.Traces, out interface{}) error
34-
35-
Type() interface{}
36-
}
37-
38-
type LogsModelTranslator interface {
39-
// LogsFromModel converts a data model of another protocol into pdata.
40-
LogsFromModel(src interface{}) (pdata.Logs, error)
41-
// LogsToModel converts pdata to data model.
42-
LogsToModel(md pdata.Logs, out interface{}) error
43-
}
44-
4521
// ErrIncompatibleType details a type conversion error during translation.
4622
type ErrIncompatibleType struct {
47-
Model interface{}
48-
// TODO: maybe do expected vs. actual?
23+
given interface{}
24+
expected interface{}
4925
}
5026

5127
func (i *ErrIncompatibleType) Error() string {
52-
return fmt.Sprintf("model type %T is incompatible", i.Model)
28+
return fmt.Sprintf("model type %T is expected but given %T ", i.expected, i.given)
5329
}
30+
31+
// NewErrIncompatibleType returns ErrIncompatibleType instance
32+
func NewErrIncompatibleType(expected, given interface{}) *ErrIncompatibleType {
33+
return &ErrIncompatibleType{
34+
given: given,
35+
expected: expected,
36+
}
37+
}

protocols/protocols.go

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package encoding
15+
package serializing
1616

17-
// MetricsDecoder decodes bytes into pdata.
17+
// MetricsDecoder decodes bytes into data model.
1818
type MetricsDecoder interface {
1919
DecodeMetrics(bytes []byte) (interface{}, error)
2020
}
2121

22-
// TracesDecoder decodes bytes into pdata.
22+
// TracesDecoder decodes bytes into data model.
2323
type TracesDecoder interface {
2424
DecodeTraces(bytes []byte) (interface{}, error)
2525
}
2626

27-
// LogsDecoder decodes bytes into pdata.
27+
// LogsDecoder decodes bytes into data model.
2828
type LogsDecoder interface {
2929
DecodeLogs(bytes []byte) (interface{}, error)
3030
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package encoding
15+
package serializing
1616

17-
// MetricsEncoder encodes pdata into bytes.
17+
// MetricsEncoder encodes data model into bytes.
1818
type MetricsEncoder interface {
1919
EncodeMetrics(model interface{}) ([]byte, error)
2020
}
2121

22-
// TracesEncoder encodes pdata into bytes.
22+
// TracesEncoder encodes data model into bytes.
2323
type TracesEncoder interface {
2424
EncodeTraces(model interface{}) ([]byte, error)
2525
}
2626

27-
// LogsEncoder encodes pdata into bytes.
27+
// LogsEncoder encodes data model into bytes.
2828
type LogsEncoder interface {
2929
EncodeLogs(model interface{}) ([]byte, error)
3030
}

0 commit comments

Comments
 (0)