diff --git a/CHANGELOG.md b/CHANGELOG.md index d60b436a695..51f98cd48f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ ### 💡 Enhancements 💡 +- Added `MarshalerSizer` interface to `ptrace`, `plog`, and `pmetric` packages. `NewProtoMarshaler` now returns a `MarshalerSizer` (#5929) - Add support to unmarshalls bytes into pmetric.Metrics with `jsoniter` in jsonUnmarshaler(#5433) - Add httpprovider to allow loading config files stored in HTTP (#5810) - Added `service.telemetry.traces.propagators` configuration to set propagators for collector's internal spans. (#5572) diff --git a/pdata/plog/encoding.go b/pdata/plog/encoding.go index 4143190ed0b..69a71c260c4 100644 --- a/pdata/plog/encoding.go +++ b/pdata/plog/encoding.go @@ -14,6 +14,12 @@ package plog // import "go.opentelemetry.io/collector/pdata/plog" +// MarshalerSizer is the interface that groups the basic Marshal and Size methods +type MarshalerSizer interface { + Marshaler + Sizer +} + // Marshaler marshals pdata.Logs into bytes. type Marshaler interface { // MarshalLogs the given pdata.Logs into bytes. diff --git a/pdata/plog/pb.go b/pdata/plog/pb.go index 8c14b0d0385..a12c967edcc 100644 --- a/pdata/plog/pb.go +++ b/pdata/plog/pb.go @@ -19,12 +19,12 @@ import ( otlplogs "go.opentelemetry.io/collector/pdata/internal/data/protogen/logs/v1" ) -// NewProtoMarshaler returns a Marshaler. Marshals to OTLP binary protobuf bytes. -func NewProtoMarshaler() Marshaler { +// NewProtoMarshaler returns a MarshalerSizer. +// Marshals to OTLP binary protobuf bytes and calculates the size of the marshaled Logs. +func NewProtoMarshaler() MarshalerSizer { return newPbMarshaler() } -// TODO(#3842): Figure out how we want to represent/return *Sizers. type pbMarshaler struct{} func newPbMarshaler() *pbMarshaler { diff --git a/pdata/plog/pb_test.go b/pdata/plog/pb_test.go index c650b7506f5..64dbe7b2bdc 100644 --- a/pdata/plog/pb_test.go +++ b/pdata/plog/pb_test.go @@ -31,12 +31,11 @@ func TestProtoLogsUnmarshaler_error(t *testing.T) { } func TestProtoSizer(t *testing.T) { - sizer := NewProtoMarshaler().(Sizer) marshaler := NewProtoMarshaler() ld := NewLogs() ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().SetSeverityText("error") - size := sizer.LogsSize(ld) + size := marshaler.LogsSize(ld) bytes, err := marshaler.MarshalLogs(ld) require.NoError(t, err) diff --git a/pdata/pmetric/encoding.go b/pdata/pmetric/encoding.go index 5c9f9b31786..51c5acec3a0 100644 --- a/pdata/pmetric/encoding.go +++ b/pdata/pmetric/encoding.go @@ -14,6 +14,12 @@ package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" +// MarshalerSizer is the interface that groups the basic Marshal and Size methods +type MarshalerSizer interface { + Marshaler + Sizer +} + // Marshaler marshals pmetric.Metrics into bytes. type Marshaler interface { // MarshalMetrics the given pmetric.Metrics into bytes. diff --git a/pdata/pmetric/pb.go b/pdata/pmetric/pb.go index 40fe605080c..db05b35c253 100644 --- a/pdata/pmetric/pb.go +++ b/pdata/pmetric/pb.go @@ -19,12 +19,12 @@ import ( otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" ) -// NewProtoMarshaler returns a Marshaler. Marshals to OTLP binary protobuf bytes. -func NewProtoMarshaler() Marshaler { +// NewProtoMarshaler returns a MarshalerSizer. +// Marshals to OTLP binary protobuf bytes and calculates the size of the marshaled Metrics. +func NewProtoMarshaler() MarshalerSizer { return newPbMarshaler() } -// TODO(#3842): Figure out how we want to represent/return *Sizers. type pbMarshaler struct{} func newPbMarshaler() *pbMarshaler { diff --git a/pdata/pmetric/pb_test.go b/pdata/pmetric/pb_test.go index b50041e17ce..3be66f16abc 100644 --- a/pdata/pmetric/pb_test.go +++ b/pdata/pmetric/pb_test.go @@ -31,12 +31,11 @@ func TestProtoMetricsUnmarshaler_error(t *testing.T) { } func TestProtoSizer(t *testing.T) { - sizer := NewProtoMarshaler().(Sizer) marshaler := NewProtoMarshaler() md := NewMetrics() md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("foo") - size := sizer.MetricsSize(md) + size := marshaler.MetricsSize(md) bytes, err := marshaler.MarshalMetrics(md) require.NoError(t, err) diff --git a/pdata/ptrace/encoding.go b/pdata/ptrace/encoding.go index e1c2d72a33d..25fa14e0cdf 100644 --- a/pdata/ptrace/encoding.go +++ b/pdata/ptrace/encoding.go @@ -14,6 +14,12 @@ package ptrace // import "go.opentelemetry.io/collector/pdata/ptrace" +// MarshalerSizer is the interface that groups the basic Marshal and Size methods +type MarshalerSizer interface { + Marshaler + Sizer +} + // Marshaler marshals pdata.Traces into bytes. type Marshaler interface { // MarshalTraces the given pdata.Traces into bytes. diff --git a/pdata/ptrace/pb.go b/pdata/ptrace/pb.go index acc4440d7ce..e164145904a 100644 --- a/pdata/ptrace/pb.go +++ b/pdata/ptrace/pb.go @@ -19,12 +19,12 @@ import ( otlptrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/trace/v1" ) -// NewProtoMarshaler returns a Marshaler. Marshals to OTLP binary protobuf bytes. -func NewProtoMarshaler() Marshaler { +// NewProtoMarshaler returns a MarshalerSizer. +// Marshals to OTLP binary protobuf bytes and calculates the size of the marshaled Traces. +func NewProtoMarshaler() MarshalerSizer { return newPbMarshaler() } -// TODO(#3842): Figure out how we want to represent/return *Sizers. type pbMarshaler struct{} func newPbMarshaler() *pbMarshaler { diff --git a/pdata/ptrace/pb_test.go b/pdata/ptrace/pb_test.go index 19d85643fca..cb50d5a5f77 100644 --- a/pdata/ptrace/pb_test.go +++ b/pdata/ptrace/pb_test.go @@ -31,13 +31,12 @@ func TestProtoTracesUnmarshaler_error(t *testing.T) { } func TestProtoSizer(t *testing.T) { - sizer := NewProtoMarshaler().(Sizer) marshaler := NewProtoMarshaler() td := NewTraces() rms := td.ResourceSpans() rms.AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty().SetName("foo") - size := sizer.TracesSize(td) + size := marshaler.TracesSize(td) bytes, err := marshaler.MarshalTraces(td) require.NoError(t, err)