Skip to content

Commit aa450a0

Browse files
authored
all: remove OtlpProtoSize in favor of Sizer interface (#3818)
* all: remove OtlpProtoSize in favor of Sizer interface * add {Metrics|Traces|Logs}Sizer, fix test commenting * move size tests to pb_test, fix metrics+logs parameter names * uncommented batch_processor tests + adjusted them to use *Sizer * I forgot BenchmarkTraceSizeBytes :( * Add docs for NewProtobof*Sizer + update docs for NewProtobuf*Marshaler * cast *Marshaler to *Sizer for now * add casts to batch_processor_test
1 parent 9cafb5d commit aa450a0

File tree

9 files changed

+118
-68
lines changed

9 files changed

+118
-68
lines changed

model/otlp/pb_marshaler.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,32 @@ import (
1919
"go.opentelemetry.io/collector/model/pdata"
2020
)
2121

22-
// NewProtobufTracesMarshaler returns a model.TracesMarshaler. Marshals to OTLP binary protobuf bytes.
22+
// NewProtobufTracesMarshaler returns a pdata.TracesMarshaler. Marshals to OTLP binary protobuf bytes.
2323
func NewProtobufTracesMarshaler() pdata.TracesMarshaler {
2424
return newPbMarshaler()
2525
}
2626

27-
// NewProtobufMetricsMarshaler returns a model.MetricsMarshaler. Marshals to OTLP binary protobuf bytes.
27+
// NewProtobufMetricsMarshaler returns a pdata.MetricsMarshaler. Marshals to OTLP binary protobuf bytes.
2828
func NewProtobufMetricsMarshaler() pdata.MetricsMarshaler {
2929
return newPbMarshaler()
3030
}
3131

32-
// NewProtobufLogsMarshaler returns a model.LogsMarshaler. Marshals to OTLP binary protobuf bytes.
32+
// NewProtobufLogsMarshaler returns a pdata.LogsMarshaler. Marshals to OTLP binary protobuf bytes.
3333
func NewProtobufLogsMarshaler() pdata.LogsMarshaler {
3434
return newPbMarshaler()
3535
}
3636

37+
// TODO(#3842): Figure out how we want to represent/return *Sizers.
3738
type pbMarshaler struct{}
3839

3940
func newPbMarshaler() *pbMarshaler {
4041
return &pbMarshaler{}
4142
}
4243

44+
var _ pdata.TracesSizer = (*pbMarshaler)(nil)
45+
var _ pdata.MetricsSizer = (*pbMarshaler)(nil)
46+
var _ pdata.LogsSizer = (*pbMarshaler)(nil)
47+
4348
func (e *pbMarshaler) MarshalLogs(ld pdata.Logs) ([]byte, error) {
4449
return internal.LogsToOtlp(ld.InternalRep()).Marshal()
4550
}
@@ -51,3 +56,15 @@ func (e *pbMarshaler) MarshalMetrics(md pdata.Metrics) ([]byte, error) {
5156
func (e *pbMarshaler) MarshalTraces(td pdata.Traces) ([]byte, error) {
5257
return internal.TracesToOtlp(td.InternalRep()).Marshal()
5358
}
59+
60+
func (e *pbMarshaler) TracesSize(td pdata.Traces) int {
61+
return internal.TracesToOtlp(td.InternalRep()).Size()
62+
}
63+
64+
func (e *pbMarshaler) MetricsSize(md pdata.Metrics) int {
65+
return internal.MetricsToOtlp(md.InternalRep()).Size()
66+
}
67+
68+
func (e *pbMarshaler) LogsSize(ld pdata.Logs) int {
69+
return internal.LogsToOtlp(ld.InternalRep()).Size()
70+
}

model/otlp/pb_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,65 @@ func TestProtobufTracesUnmarshaler_error(t *testing.T) {
4242
assert.Error(t, err)
4343
}
4444

45+
func TestProtobufTracesSizer(t *testing.T) {
46+
sizer := NewProtobufTracesMarshaler().(pdata.TracesSizer)
47+
marshaler := NewProtobufTracesMarshaler()
48+
td := pdata.NewTraces()
49+
rms := td.ResourceSpans()
50+
rms.AppendEmpty().InstrumentationLibrarySpans().AppendEmpty().Spans().AppendEmpty().SetName("foo")
51+
52+
size := sizer.TracesSize(td)
53+
54+
bytes, err := marshaler.MarshalTraces(td)
55+
require.NoError(t, err)
56+
assert.Equal(t, len(bytes), size)
57+
}
58+
59+
func TestProtobufTracesSizer_withNil(t *testing.T) {
60+
sizer := NewProtobufTracesMarshaler().(pdata.TracesSizer)
61+
62+
assert.Equal(t, 0, sizer.TracesSize(pdata.NewTraces()))
63+
}
64+
65+
func TestProtobufMetricsSizer(t *testing.T) {
66+
sizer := NewProtobufMetricsMarshaler().(pdata.MetricsSizer)
67+
marshaler := NewProtobufMetricsMarshaler()
68+
md := pdata.NewMetrics()
69+
md.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("foo")
70+
71+
size := sizer.MetricsSize(md)
72+
73+
bytes, err := marshaler.MarshalMetrics(md)
74+
require.NoError(t, err)
75+
assert.Equal(t, len(bytes), size)
76+
}
77+
78+
func TestProtobufMetricsSizer_withNil(t *testing.T) {
79+
sizer := NewProtobufMetricsMarshaler().(pdata.MetricsSizer)
80+
81+
assert.Equal(t, 0, sizer.MetricsSize(pdata.NewMetrics()))
82+
}
83+
84+
func TestProtobufLogsSizer(t *testing.T) {
85+
sizer := NewProtobufLogsMarshaler().(pdata.LogsSizer)
86+
marshaler := NewProtobufLogsMarshaler()
87+
ld := pdata.NewLogs()
88+
ld.ResourceLogs().AppendEmpty().InstrumentationLibraryLogs().AppendEmpty().Logs().AppendEmpty().SetName("foo")
89+
90+
size := sizer.LogsSize(ld)
91+
92+
bytes, err := marshaler.MarshalLogs(ld)
93+
require.NoError(t, err)
94+
assert.Equal(t, len(bytes), size)
95+
96+
}
97+
98+
func TestProtobufLogsSizer_withNil(t *testing.T) {
99+
sizer := NewProtobufLogsMarshaler().(pdata.LogsSizer)
100+
101+
assert.Equal(t, 0, sizer.LogsSize(pdata.NewLogs()))
102+
}
103+
45104
func BenchmarkLogsToProtobuf(b *testing.B) {
46105
marshaler := NewProtobufLogsMarshaler()
47106
logs := generateBenchmarkLogs(128)

model/pdata/logs.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
type LogsMarshaler interface {
2525
// MarshalLogs the given pdata.Logs into bytes.
2626
// If the error is not nil, the returned bytes slice cannot be used.
27-
MarshalLogs(td Logs) ([]byte, error)
27+
MarshalLogs(ld Logs) ([]byte, error)
2828
}
2929

3030
// LogsUnmarshaler unmarshalls bytes into pdata.Logs.
@@ -34,6 +34,12 @@ type LogsUnmarshaler interface {
3434
UnmarshalLogs(buf []byte) (Logs, error)
3535
}
3636

37+
// LogsSizer returns the size of a Logs.
38+
type LogsSizer interface {
39+
// LogsSize returns the size in bytes of a Logs.
40+
LogsSize(ld Logs) int
41+
}
42+
3743
// Logs is the top-level struct that is propagated through the logs pipeline.
3844
//
3945
// This is a reference type (like builtin map).
@@ -85,12 +91,6 @@ func (ld Logs) LogRecordCount() int {
8591
return logCount
8692
}
8793

88-
// OtlpProtoSize returns the size in bytes of this Logs encoded as OTLP Collector
89-
// ExportLogsServiceRequest ProtoBuf bytes.
90-
func (ld Logs) OtlpProtoSize() int {
91-
return ld.orig.Size()
92-
}
93-
9494
// ResourceLogs returns the ResourceLogsSlice associated with this Logs.
9595
func (ld Logs) ResourceLogs() ResourceLogsSlice {
9696
return newResourceLogsSlice(&ld.orig.ResourceLogs)

model/pdata/metrics.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
type MetricsMarshaler interface {
2525
// MarshalMetrics the given pdata.Metrics into bytes.
2626
// If the error is not nil, the returned bytes slice cannot be used.
27-
MarshalMetrics(td Metrics) ([]byte, error)
27+
MarshalMetrics(md Metrics) ([]byte, error)
2828
}
2929

3030
// MetricsUnmarshaler unmarshalls bytes into pdata.Metrics.
@@ -34,6 +34,12 @@ type MetricsUnmarshaler interface {
3434
UnmarshalMetrics(buf []byte) (Metrics, error)
3535
}
3636

37+
// MetricsSizer returns the size of a Metrics.
38+
type MetricsSizer interface {
39+
// LogsSize returns the size in bytes of a Metrics.
40+
MetricsSize(md Metrics) int
41+
}
42+
3743
// Metrics is an opaque interface that allows transition to the new internal Metrics data, but also facilitates the
3844
// transition to the new components, especially for traces.
3945
//
@@ -87,12 +93,6 @@ func (md Metrics) MetricCount() int {
8793
return metricCount
8894
}
8995

90-
// OtlpProtoSize returns the size in bytes of this Metrics encoded as OTLP Collector
91-
// ExportMetricsServiceRequest ProtoBuf bytes.
92-
func (md Metrics) OtlpProtoSize() int {
93-
return md.orig.Size()
94-
}
95-
9696
// DataPointCount calculates the total number of data points.
9797
func (md Metrics) DataPointCount() (dataPointCount int) {
9898
rms := md.ResourceMetrics()

model/pdata/metrics_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
gogoproto "github.com/gogo/protobuf/proto"
2121
"github.com/stretchr/testify/assert"
22-
"github.com/stretchr/testify/require"
2322
goproto "google.golang.org/protobuf/proto"
2423
"google.golang.org/protobuf/types/known/emptypb"
2524

@@ -148,22 +147,6 @@ func TestMetricCount(t *testing.T) {
148147
assert.EqualValues(t, 6, md.MetricCount())
149148
}
150149

151-
func TestMetricsSize(t *testing.T) {
152-
assert.Equal(t, 0, NewMetrics().OtlpProtoSize())
153-
154-
md := generateMetricsEmptyDataPoints()
155-
orig := md.orig
156-
size := orig.Size()
157-
bytes, err := orig.Marshal()
158-
require.NoError(t, err)
159-
assert.Equal(t, size, md.OtlpProtoSize())
160-
assert.Equal(t, len(bytes), md.OtlpProtoSize())
161-
}
162-
163-
func TestMetricsSizeWithNil(t *testing.T) {
164-
assert.Equal(t, 0, NewMetrics().OtlpProtoSize())
165-
}
166-
167150
func TestMetricCountWithEmpty(t *testing.T) {
168151
assert.EqualValues(t, 0, generateMetricsEmptyResource().MetricCount())
169152
assert.EqualValues(t, 0, generateMetricsEmptyInstrumentation().MetricCount())

model/pdata/traces.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ type TracesUnmarshaler interface {
3434
UnmarshalTraces(buf []byte) (Traces, error)
3535
}
3636

37+
// TracesSizer returns the size of a Traces.
38+
type TracesSizer interface {
39+
// TracesSize returns the size in bytes of a Traces.
40+
TracesSize(td Traces) int
41+
}
42+
3743
// Traces is the top-level struct that is propagated through the traces pipeline.
3844
type Traces struct {
3945
orig *otlpcollectortrace.ExportTraceServiceRequest
@@ -77,12 +83,6 @@ func (td Traces) SpanCount() int {
7783
return spanCount
7884
}
7985

80-
// OtlpProtoSize returns the size in bytes of this Traces encoded as OTLP Collector
81-
// ExportTraceServiceRequest ProtoBuf bytes.
82-
func (td Traces) OtlpProtoSize() int {
83-
return td.orig.Size()
84-
}
85-
8686
// ResourceSpans returns the ResourceSpansSlice associated with this Metrics.
8787
func (td Traces) ResourceSpans() ResourceSpansSlice {
8888
return newResourceSpansSlice(&td.orig.ResourceSpans)

model/pdata/traces_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
gogoproto "github.com/gogo/protobuf/proto"
2121
"github.com/stretchr/testify/assert"
22-
"github.com/stretchr/testify/require"
2322
goproto "google.golang.org/protobuf/proto"
2423
"google.golang.org/protobuf/types/known/emptypb"
2524

@@ -52,23 +51,6 @@ func TestSpanCount(t *testing.T) {
5251
assert.EqualValues(t, 6, md.SpanCount())
5352
}
5453

55-
func TestTracesSize(t *testing.T) {
56-
assert.Equal(t, 0, NewTraces().OtlpProtoSize())
57-
td := NewTraces()
58-
rms := td.ResourceSpans()
59-
rms.AppendEmpty().InstrumentationLibrarySpans().AppendEmpty().Spans().AppendEmpty().SetName("foo")
60-
orig := td.orig
61-
size := orig.Size()
62-
bytes, err := orig.Marshal()
63-
require.NoError(t, err)
64-
assert.Equal(t, size, td.OtlpProtoSize())
65-
assert.Equal(t, len(bytes), td.OtlpProtoSize())
66-
}
67-
68-
func TestTracesSizeWithNil(t *testing.T) {
69-
assert.Equal(t, 0, NewTraces().OtlpProtoSize())
70-
}
71-
7254
func TestSpanCountWithEmpty(t *testing.T) {
7355
assert.EqualValues(t, 0, Traces{orig: &otlpcollectortrace.ExportTraceServiceRequest{
7456
ResourceSpans: []*otlptrace.ResourceSpans{{}},

processor/batchprocessor/batch_processor.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"go.opentelemetry.io/collector/component"
2828
"go.opentelemetry.io/collector/config/configtelemetry"
2929
"go.opentelemetry.io/collector/consumer"
30+
"go.opentelemetry.io/collector/model/otlp"
3031
"go.opentelemetry.io/collector/model/pdata"
3132
)
3233

@@ -223,10 +224,11 @@ type batchTraces struct {
223224
nextConsumer consumer.Traces
224225
traceData pdata.Traces
225226
spanCount int
227+
sizer pdata.TracesSizer
226228
}
227229

228230
func newBatchTraces(nextConsumer consumer.Traces) *batchTraces {
229-
return &batchTraces{nextConsumer: nextConsumer, traceData: pdata.NewTraces()}
231+
return &batchTraces{nextConsumer: nextConsumer, traceData: pdata.NewTraces(), sizer: otlp.NewProtobufTracesMarshaler().(pdata.TracesSizer)}
230232
}
231233

232234
// add updates current batchTraces by adding new TraceData object
@@ -259,17 +261,18 @@ func (bt *batchTraces) itemCount() int {
259261
}
260262

261263
func (bt *batchTraces) size() int {
262-
return bt.traceData.OtlpProtoSize()
264+
return bt.sizer.TracesSize(bt.traceData)
263265
}
264266

265267
type batchMetrics struct {
266268
nextConsumer consumer.Metrics
267269
metricData pdata.Metrics
268270
dataPointCount int
271+
sizer pdata.MetricsSizer
269272
}
270273

271274
func newBatchMetrics(nextConsumer consumer.Metrics) *batchMetrics {
272-
return &batchMetrics{nextConsumer: nextConsumer, metricData: pdata.NewMetrics()}
275+
return &batchMetrics{nextConsumer: nextConsumer, metricData: pdata.NewMetrics(), sizer: otlp.NewProtobufMetricsMarshaler().(pdata.MetricsSizer)}
273276
}
274277

275278
func (bm *batchMetrics) export(ctx context.Context, sendBatchMaxSize int) error {
@@ -290,7 +293,7 @@ func (bm *batchMetrics) itemCount() int {
290293
}
291294

292295
func (bm *batchMetrics) size() int {
293-
return bm.metricData.OtlpProtoSize()
296+
return bm.sizer.MetricsSize(bm.metricData)
294297
}
295298

296299
func (bm *batchMetrics) add(item interface{}) {
@@ -308,10 +311,11 @@ type batchLogs struct {
308311
nextConsumer consumer.Logs
309312
logData pdata.Logs
310313
logCount int
314+
sizer pdata.LogsSizer
311315
}
312316

313317
func newBatchLogs(nextConsumer consumer.Logs) *batchLogs {
314-
return &batchLogs{nextConsumer: nextConsumer, logData: pdata.NewLogs()}
318+
return &batchLogs{nextConsumer: nextConsumer, logData: pdata.NewLogs(), sizer: otlp.NewProtobufLogsMarshaler().(pdata.LogsSizer)}
315319
}
316320

317321
func (bl *batchLogs) export(ctx context.Context, sendBatchMaxSize int) error {
@@ -332,7 +336,7 @@ func (bl *batchLogs) itemCount() int {
332336
}
333337

334338
func (bl *batchLogs) size() int {
335-
return bl.logData.OtlpProtoSize()
339+
return bl.sizer.LogsSize(bl.logData)
336340
}
337341

338342
func (bl *batchLogs) add(item interface{}) {

0 commit comments

Comments
 (0)