| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package queuebatch |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"time" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/component" |
| 12 |
"go.opentelemetry.io/collector/config/configoptional" |
| 13 |
"go.opentelemetry.io/collector/confmap" |
| 14 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/request" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
type Config struct { |
| 19 |
|
| 20 |
Enabled bool `mapstructure:"enabled"` |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
WaitForResult bool `mapstructure:"wait_for_result"` |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
Sizer request.SizerType `mapstructure:"sizer"` |
| 29 |
|
| 30 |
|
| 31 |
QueueSize int64 `mapstructure:"queue_size"` |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
BlockOnOverflow bool `mapstructure:"block_on_overflow"` |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
StorageID *component.ID `mapstructure:"storage"` |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
NumConsumers int `mapstructure:"num_consumers"` |
| 46 |
|
| 47 |
|
| 48 |
Batch configoptional.Optional[BatchConfig] `mapstructure:"batch"` |
| 49 |
} |
| 50 |
|
| 51 |
func (cfg *Config) Unmarshal(conf *confmap.Conf) error { |
| 52 |
if err := conf.Unmarshal(cfg); err != nil { |
| 53 |
return err |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
if conf.IsSet("sizer") && !conf.IsSet("batch::sizer") && conf.IsSet("batch") && conf.Get("batch") != nil { |
| 62 |
cfg.Batch.Get().Sizer = cfg.Sizer |
| 63 |
} |
| 64 |
return nil |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func (cfg *Config) Validate() error { |
| 69 |
if !cfg.Enabled { |
| 70 |
return nil |
| 71 |
} |
| 72 |
|
| 73 |
if cfg.NumConsumers <= 0 { |
| 74 |
return errors.New("`num_consumers` must be positive") |
| 75 |
} |
| 76 |
|
| 77 |
if cfg.QueueSize <= 0 { |
| 78 |
return errors.New("`queue_size` must be positive") |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
if cfg.StorageID != nil && cfg.WaitForResult { |
| 83 |
return errors.New("`wait_for_result` is not supported with a persistent queue configured with `storage`") |
| 84 |
} |
| 85 |
|
| 86 |
if cfg.Batch.HasValue() && cfg.Batch.Get().Sizer == cfg.Sizer { |
| 87 |
|
| 88 |
if cfg.Batch.Get().MinSize > cfg.QueueSize { |
| 89 |
return errors.New("`min_size` must be less than or equal to `queue_size`") |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return nil |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
type BatchConfig struct { |
| 98 |
|
| 99 |
FlushTimeout time.Duration `mapstructure:"flush_timeout"` |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
Sizer request.SizerType `mapstructure:"sizer"` |
| 105 |
|
| 106 |
|
| 107 |
MinSize int64 `mapstructure:"min_size"` |
| 108 |
|
| 109 |
|
| 110 |
MaxSize int64 `mapstructure:"max_size"` |
| 111 |
} |
| 112 |
|
| 113 |
func (cfg *BatchConfig) Validate() error { |
| 114 |
if cfg == nil { |
| 115 |
return nil |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
if cfg.Sizer != request.SizerTypeItems && cfg.Sizer != request.SizerTypeBytes { |
| 120 |
return fmt.Errorf("`batch` supports only `items` or `bytes` sizer, found %q", cfg.Sizer.String()) |
| 121 |
} |
| 122 |
|
| 123 |
if cfg.FlushTimeout <= 0 { |
| 124 |
return fmt.Errorf("`flush_timeout` must be positive, found %d", cfg.FlushTimeout) |
| 125 |
} |
| 126 |
|
| 127 |
if cfg.MinSize < 0 { |
| 128 |
return fmt.Errorf("`min_size` must be non-negative, found %d", cfg.MinSize) |
| 129 |
} |
| 130 |
|
| 131 |
if cfg.MaxSize < 0 { |
| 132 |
return fmt.Errorf("`max_size` must be non-negative, found %d", cfg.MaxSize) |
| 133 |
} |
| 134 |
|
| 135 |
if cfg.MaxSize > 0 && cfg.MaxSize < cfg.MinSize { |
| 136 |
return fmt.Errorf("`max_size` (%d) must be greater or equal to `min_size` (%d)", cfg.MaxSize, cfg.MinSize) |
| 137 |
} |
| 138 |
|
| 139 |
return nil |
| 140 |
} |