Skip to content

Commit

Permalink
sdk/log: Add options to NewSimpleProcessor (#5185)
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared authored Apr 10, 2024
1 parent 727f03e commit 054a63f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
16 changes: 8 additions & 8 deletions sdk/log/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type BatchingProcessor struct {
// so that the log records are batched before exporting.
//
// All of the exporter's methods are called synchronously.
func NewBatchingProcessor(exporter Exporter, opts ...BatchingOption) *BatchingProcessor {
func NewBatchingProcessor(exporter Exporter, opts ...BatchProcessorOption) *BatchingProcessor {
if exporter == nil {
// Do not panic on nil export.
exporter = defaultNoopExporter
Expand Down Expand Up @@ -170,7 +170,7 @@ type batchingConfig struct {
expMaxBatchSize setting[int]
}

func newBatchingConfig(options []BatchingOption) batchingConfig {
func newBatchingConfig(options []BatchProcessorOption) batchingConfig {
var c batchingConfig
for _, o := range options {
c = o.apply(c)
Expand Down Expand Up @@ -205,8 +205,8 @@ func newBatchingConfig(options []BatchingOption) batchingConfig {
return c
}

// BatchingOption applies a configuration to a [BatchingProcessor].
type BatchingOption interface {
// BatchProcessorOption applies a configuration to a [BatchingProcessor].
type BatchProcessorOption interface {
apply(batchingConfig) batchingConfig
}

Expand All @@ -225,7 +225,7 @@ func (fn batchingOptionFunc) apply(c batchingConfig) batchingConfig {
// By default, if an environment variable is not set, and this option is not
// passed, 2048 will be used.
// The default value is also used when the provided value is less than one.
func WithMaxQueueSize(size int) BatchingOption {
func WithMaxQueueSize(size int) BatchProcessorOption {
return batchingOptionFunc(func(cfg batchingConfig) batchingConfig {
cfg.maxQSize = newSetting(size)
return cfg
Expand All @@ -240,7 +240,7 @@ func WithMaxQueueSize(size int) BatchingOption {
// By default, if an environment variable is not set, and this option is not
// passed, 1s will be used.
// The default value is also used when the provided value is less than one.
func WithExportInterval(d time.Duration) BatchingOption {
func WithExportInterval(d time.Duration) BatchProcessorOption {
return batchingOptionFunc(func(cfg batchingConfig) batchingConfig {
cfg.expInterval = newSetting(d)
return cfg
Expand All @@ -255,7 +255,7 @@ func WithExportInterval(d time.Duration) BatchingOption {
// By default, if an environment variable is not set, and this option is not
// passed, 30s will be used.
// The default value is also used when the provided value is less than one.
func WithExportTimeout(d time.Duration) BatchingOption {
func WithExportTimeout(d time.Duration) BatchProcessorOption {
return batchingOptionFunc(func(cfg batchingConfig) batchingConfig {
cfg.expTimeout = newSetting(d)
return cfg
Expand All @@ -271,7 +271,7 @@ func WithExportTimeout(d time.Duration) BatchingOption {
// By default, if an environment variable is not set, and this option is not
// passed, 512 will be used.
// The default value is also used when the provided value is less than one.
func WithExportMaxBatchSize(size int) BatchingOption {
func WithExportMaxBatchSize(size int) BatchProcessorOption {
return batchingOptionFunc(func(cfg batchingConfig) batchingConfig {
cfg.expMaxBatchSize = newSetting(size)
return cfg
Expand Down
10 changes: 5 additions & 5 deletions sdk/log/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewBatchingConfig(t *testing.T) {
testcases := []struct {
name string
envars map[string]string
options []BatchingOption
options []BatchProcessorOption
want batchingConfig
}{
{
Expand All @@ -39,7 +39,7 @@ func TestNewBatchingConfig(t *testing.T) {
},
{
name: "Options",
options: []BatchingOption{
options: []BatchProcessorOption{
WithMaxQueueSize(10),
WithExportInterval(time.Microsecond),
WithExportTimeout(time.Hour),
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestNewBatchingConfig(t *testing.T) {
},
{
name: "InvalidOptions",
options: []BatchingOption{
options: []BatchProcessorOption{
WithMaxQueueSize(-11),
WithExportInterval(-1 * time.Microsecond),
WithExportTimeout(-1 * time.Hour),
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestNewBatchingConfig(t *testing.T) {
envarExpTimeout: strconv.Itoa(1000),
envarExpMaxBatchSize: strconv.Itoa(10),
},
options: []BatchingOption{
options: []BatchProcessorOption{
// These override the environment variables.
WithMaxQueueSize(3),
WithExportInterval(time.Microsecond),
Expand All @@ -121,7 +121,7 @@ func TestNewBatchingConfig(t *testing.T) {
},
{
name: "BatchLessThanOrEqualToQSize",
options: []BatchingOption{
options: []BatchProcessorOption{
WithMaxQueueSize(1),
WithExportMaxBatchSize(10),
},
Expand Down
7 changes: 6 additions & 1 deletion sdk/log/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type SimpleProcessor struct {
// showing examples of other features, but it can be slow and have a high
// computation resource usage overhead. [NewBatchingProcessor] is recommended
// for production use instead.
func NewSimpleProcessor(exporter Exporter) *SimpleProcessor {
func NewSimpleProcessor(exporter Exporter, _ ...SimpleProcessorOption) *SimpleProcessor {
if exporter == nil {
// Do not panic on nil exporter.
exporter = defaultNoopExporter
Expand All @@ -49,3 +49,8 @@ func (s *SimpleProcessor) Shutdown(ctx context.Context) error {
func (s *SimpleProcessor) ForceFlush(ctx context.Context) error {
return s.exporter.ForceFlush(ctx)
}

// SimpleProcessorOption applies a configuration to a [SimpleProcessor].
type SimpleProcessorOption interface {
apply()
}

0 comments on commit 054a63f

Please sign in to comment.