Skip to content

Commit a7d5c1a

Browse files
FrapschenMrAliasdmathieu
authored
Add an option to configure the exporter buffer of the BatchProcessor (#5877)
resolve: #5238 --------- Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Damien Mathieu <[email protected]>
1 parent eb9279b commit a7d5c1a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1111
### Added
1212

1313
- Add `go.opentelemetry.io/otel/sdk/metric/exemplar` package which includes `Exemplar`, `Filter`, `TraceBasedFilter`, `AlwaysOnFilter`, `HistogramReservoir`, `FixedSizeReservoir`, `Reservoir`, `Value` and `ValueType` types. These will be used for configuring the exemplar reservoir for the metrics sdk. (#5747, #5862)
14+
- Add `WithExportBufferSize` option to log batch processor.(#5877)
1415

1516
### Changed
1617

sdk/log/batch.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
dfltExpInterval = time.Second
2020
dfltExpTimeout = 30 * time.Second
2121
dfltExpMaxBatchSize = 512
22+
dfltExpBufferSize = 1
2223

2324
envarMaxQSize = "OTEL_BLRP_MAX_QUEUE_SIZE"
2425
envarExpInterval = "OTEL_BLRP_SCHEDULE_DELAY"
@@ -119,8 +120,7 @@ func NewBatchProcessor(exporter Exporter, opts ...BatchProcessorOption) *BatchPr
119120
exporter = newChunkExporter(exporter, cfg.expMaxBatchSize.Value)
120121

121122
b := &BatchProcessor{
122-
// TODO: explore making the size of this configurable.
123-
exporter: newBufferExporter(exporter, 1),
123+
exporter: newBufferExporter(exporter, cfg.expBufferSize.Value),
124124

125125
q: newQueue(cfg.maxQSize.Value),
126126
batchSize: cfg.expMaxBatchSize.Value,
@@ -349,6 +349,7 @@ type batchConfig struct {
349349
expInterval setting[time.Duration]
350350
expTimeout setting[time.Duration]
351351
expMaxBatchSize setting[int]
352+
expBufferSize setting[int]
352353
}
353354

354355
func newBatchConfig(options []BatchProcessorOption) batchConfig {
@@ -382,6 +383,10 @@ func newBatchConfig(options []BatchProcessorOption) batchConfig {
382383
clampMax[int](c.maxQSize.Value),
383384
fallback[int](dfltExpMaxBatchSize),
384385
)
386+
c.expBufferSize = c.expBufferSize.Resolve(
387+
clearLessThanOne[int](),
388+
fallback[int](dfltExpBufferSize),
389+
)
385390

386391
return c
387392
}
@@ -458,3 +463,15 @@ func WithExportMaxBatchSize(size int) BatchProcessorOption {
458463
return cfg
459464
})
460465
}
466+
467+
// WithExportBufferSize sets the batch buffer size.
468+
// Batches will be temporarily kept in a memory buffer until they are exported.
469+
//
470+
// By default, a value of 1 will be used.
471+
// The default value is also used when the provided value is less than one.
472+
func WithExportBufferSize(size int) BatchProcessorOption {
473+
return batchOptionFunc(func(cfg batchConfig) batchConfig {
474+
cfg.expBufferSize = newSetting(size)
475+
return cfg
476+
})
477+
}

sdk/log/batch_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func TestNewBatchConfig(t *testing.T) {
7070
expInterval: newSetting(dfltExpInterval),
7171
expTimeout: newSetting(dfltExpTimeout),
7272
expMaxBatchSize: newSetting(dfltExpMaxBatchSize),
73+
expBufferSize: newSetting(dfltExpBufferSize),
7374
},
7475
},
7576
{
@@ -79,12 +80,14 @@ func TestNewBatchConfig(t *testing.T) {
7980
WithExportInterval(time.Microsecond),
8081
WithExportTimeout(time.Hour),
8182
WithExportMaxBatchSize(2),
83+
WithExportBufferSize(3),
8284
},
8385
want: batchConfig{
8486
maxQSize: newSetting(10),
8587
expInterval: newSetting(time.Microsecond),
8688
expTimeout: newSetting(time.Hour),
8789
expMaxBatchSize: newSetting(2),
90+
expBufferSize: newSetting(3),
8891
},
8992
},
9093
{
@@ -100,6 +103,7 @@ func TestNewBatchConfig(t *testing.T) {
100103
expInterval: newSetting(100 * time.Millisecond),
101104
expTimeout: newSetting(1000 * time.Millisecond),
102105
expMaxBatchSize: newSetting(1),
106+
expBufferSize: newSetting(dfltExpBufferSize),
103107
},
104108
},
105109
{
@@ -109,12 +113,14 @@ func TestNewBatchConfig(t *testing.T) {
109113
WithExportInterval(-1 * time.Microsecond),
110114
WithExportTimeout(-1 * time.Hour),
111115
WithExportMaxBatchSize(-2),
116+
WithExportBufferSize(-2),
112117
},
113118
want: batchConfig{
114119
maxQSize: newSetting(dfltMaxQSize),
115120
expInterval: newSetting(dfltExpInterval),
116121
expTimeout: newSetting(dfltExpTimeout),
117122
expMaxBatchSize: newSetting(dfltExpMaxBatchSize),
123+
expBufferSize: newSetting(dfltExpBufferSize),
118124
},
119125
},
120126
{
@@ -130,6 +136,7 @@ func TestNewBatchConfig(t *testing.T) {
130136
expInterval: newSetting(dfltExpInterval),
131137
expTimeout: newSetting(dfltExpTimeout),
132138
expMaxBatchSize: newSetting(dfltExpMaxBatchSize),
139+
expBufferSize: newSetting(dfltExpBufferSize),
133140
},
134141
},
135142
{
@@ -146,25 +153,29 @@ func TestNewBatchConfig(t *testing.T) {
146153
WithExportInterval(time.Microsecond),
147154
WithExportTimeout(time.Hour),
148155
WithExportMaxBatchSize(2),
156+
WithExportBufferSize(2),
149157
},
150158
want: batchConfig{
151159
maxQSize: newSetting(3),
152160
expInterval: newSetting(time.Microsecond),
153161
expTimeout: newSetting(time.Hour),
154162
expMaxBatchSize: newSetting(2),
163+
expBufferSize: newSetting(2),
155164
},
156165
},
157166
{
158167
name: "BatchLessThanOrEqualToQSize",
159168
options: []BatchProcessorOption{
160169
WithMaxQueueSize(1),
161170
WithExportMaxBatchSize(10),
171+
WithExportBufferSize(3),
162172
},
163173
want: batchConfig{
164174
maxQSize: newSetting(1),
165175
expInterval: newSetting(dfltExpInterval),
166176
expTimeout: newSetting(dfltExpTimeout),
167177
expMaxBatchSize: newSetting(1),
178+
expBufferSize: newSetting(3),
168179
},
169180
},
170181
}

0 commit comments

Comments
 (0)