-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[exporter][batching] Serialized bytes based batching for logs (#12299)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR implements serialized bytes based batching. <!-- Issue number if applicable --> #### Link to tracking issue #3262 <!--Describe what testing was performed and which tests were added.--> #### Testing ``` BenchmarkSplittingBasedOnItemCountManySmallLogs-10 409 2822478 ns/op 3148718 B/op 71082 allocs/op BenchmarkSplittingBasedOnByteSizeManySmallLogs-10 412 2890594 ns/op 3156103 B/op 71242 allocs/op BenchmarkSplittingBasedOnItemCountManyLogsSlightlyAboveLimit-10 38 27579702 ns/op 43531582 B/op 700471 allocs/op BenchmarkSplittingBasedOnByteSizeManyLogsSlightlyAboveLimit-10 24 43067219 ns/op 43639889 B/op 702927 allocs/op BenchmarkSplittingBasedOnItemCountHugeLogs-10 40 29722710 ns/op 41922925 B/op 690300 allocs/op BenchmarkSplittingBasedOnByteSizeHugeLogs-10 16 64321198 ns/op 42107144 B/op 694144 allocs/op ``` #### Caveat We book keep both item count and byte size regardless of batching option, which adds an overhead in both cases. Co-authored-by: Dmitrii Anoshin <[email protected]>
- Loading branch information
1 parent
b3b28ed
commit 66d83f0
Showing
7 changed files
with
366 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sizer // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/sizer" | ||
|
||
import ( | ||
math_bits "math/bits" | ||
|
||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
type LogsSizer interface { | ||
LogsSize(ld plog.Logs) int | ||
ResourceLogsSize(rl plog.ResourceLogs) int | ||
ScopeLogsSize(sl plog.ScopeLogs) int | ||
LogRecordSize(lr plog.LogRecord) int | ||
|
||
// DeltaSize() returns the delta size when a ResourceLog, ScopeLog or LogRecord is added. | ||
DeltaSize(newItemSize int) int | ||
} | ||
|
||
// LogsByteSizer returns the byte size of serialized protos. | ||
type LogsBytesSizer struct { | ||
plog.ProtoMarshaler | ||
} | ||
|
||
// DeltaSize() returns the delta size of a proto slice when a new item is added. | ||
// Example: | ||
// | ||
// prevSize := proto1.Size() | ||
// proto1.RepeatedField().AppendEmpty() = proto2 | ||
// | ||
// Then currSize of proto1 can be calculated as | ||
// | ||
// currSize := (prevSize + sizer.DeltaSize(proto2.Size())) | ||
// | ||
// This is derived from opentelemetry-collector/pdata/internal/data/protogen/logs/v1/logs.pb.go | ||
// which is generated with gogo/protobuf. | ||
func (s *LogsBytesSizer) DeltaSize(newItemSize int) int { | ||
return 1 + newItemSize + math_bits.Len64(uint64(newItemSize|1)+6)/7 //nolint:gosec // disable G115 | ||
} | ||
|
||
// LogsCountSizer returns the nunmber of logs entries. | ||
type LogsCountSizer struct{} | ||
|
||
func (s *LogsCountSizer) LogsSize(ld plog.Logs) int { | ||
return ld.LogRecordCount() | ||
} | ||
|
||
func (s *LogsCountSizer) ResourceLogsSize(rl plog.ResourceLogs) int { | ||
count := 0 | ||
for k := 0; k < rl.ScopeLogs().Len(); k++ { | ||
count += rl.ScopeLogs().At(k).LogRecords().Len() | ||
} | ||
return count | ||
} | ||
|
||
func (s *LogsCountSizer) ScopeLogsSize(sl plog.ScopeLogs) int { | ||
return sl.LogRecords().Len() | ||
} | ||
|
||
func (s *LogsCountSizer) LogRecordSize(_ plog.LogRecord) int { | ||
return 1 | ||
} | ||
|
||
func (s *LogsCountSizer) DeltaSize(newItemSize int) int { | ||
return newItemSize | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package sizer // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/sizer" | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/pdata/testdata" | ||
) | ||
|
||
func TestLogsCountSizer(t *testing.T) { | ||
ld := testdata.GenerateLogs(5) | ||
sizer := LogsCountSizer{} | ||
require.Equal(t, 5, sizer.LogsSize(ld)) | ||
|
||
rl := ld.ResourceLogs().At(0) | ||
require.Equal(t, 5, sizer.ResourceLogsSize(rl)) | ||
|
||
sl := rl.ScopeLogs().At(0) | ||
require.Equal(t, 5, sizer.ScopeLogsSize(sl)) | ||
|
||
require.Equal(t, 1, sizer.LogRecordSize(sl.LogRecords().At(0))) | ||
require.Equal(t, 1, sizer.LogRecordSize(sl.LogRecords().At(1))) | ||
require.Equal(t, 1, sizer.LogRecordSize(sl.LogRecords().At(2))) | ||
require.Equal(t, 1, sizer.LogRecordSize(sl.LogRecords().At(3))) | ||
require.Equal(t, 1, sizer.LogRecordSize(sl.LogRecords().At(4))) | ||
|
||
prevSize := sizer.ScopeLogsSize(sl) | ||
lr := sl.LogRecords().At(2) | ||
lr.CopyTo(sl.LogRecords().AppendEmpty()) | ||
require.Equal(t, sizer.ScopeLogsSize(sl), prevSize+sizer.DeltaSize(sizer.LogRecordSize(lr))) | ||
} | ||
|
||
func TestLogsBytesSizer(t *testing.T) { | ||
ld := testdata.GenerateLogs(5) | ||
sizer := LogsBytesSizer{} | ||
require.Equal(t, 545, sizer.LogsSize(ld)) | ||
|
||
rl := ld.ResourceLogs().At(0) | ||
require.Equal(t, 542, sizer.ResourceLogsSize(rl)) | ||
|
||
sl := rl.ScopeLogs().At(0) | ||
require.Equal(t, 497, sizer.ScopeLogsSize(sl)) | ||
|
||
require.Equal(t, 109, sizer.LogRecordSize(sl.LogRecords().At(0))) | ||
require.Equal(t, 79, sizer.LogRecordSize(sl.LogRecords().At(1))) | ||
require.Equal(t, 109, sizer.LogRecordSize(sl.LogRecords().At(2))) | ||
require.Equal(t, 79, sizer.LogRecordSize(sl.LogRecords().At(3))) | ||
require.Equal(t, 109, sizer.LogRecordSize(sl.LogRecords().At(4))) | ||
|
||
prevSize := sizer.ScopeLogsSize(sl) | ||
lr := sl.LogRecords().At(2) | ||
lr.CopyTo(sl.LogRecords().AppendEmpty()) | ||
require.Equal(t, sizer.ScopeLogsSize(sl), prevSize+sizer.DeltaSize(sizer.LogRecordSize(lr))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.