Skip to content

Commit

Permalink
Added MarshalerSizer Interface to facilitate creation of Sizer implem…
Browse files Browse the repository at this point in the history
…entations (#5929)

* Added NewProtoSizer to instantiate Sizer implementations

Signed-off-by: Corbin Phelps <[email protected]>

* Updated changelog with PR number

Signed-off-by: Corbin Phelps <[email protected]>

* Refactored to create a MarshalSizer interface that is returned by NewProtoMarshaller

Signed-off-by: Corbin Phelps <[email protected]>

* Updated NewProtoMarshaler comments to inidicate return interface

Signed-off-by: Corbin Phelps <[email protected]>

* Corrected interface name

Signed-off-by: Corbin Phelps <[email protected]>

Signed-off-by: Corbin Phelps <[email protected]>
  • Loading branch information
Corbin Phelps authored Aug 18, 2022
1 parent 41bf088 commit 22ca5e0
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pdata/plog/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions pdata/plog/pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pdata/plog/pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pdata/pmetric/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions pdata/pmetric/pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pdata/pmetric/pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pdata/ptrace/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions pdata/ptrace/pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pdata/ptrace/pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 22ca5e0

Please sign in to comment.