Debian Codegraph @

exporter/exporterhelper/internal/queuebatch/config.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package queuebatch // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/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 // Config defines configuration for queueing and batching incoming requests.
18 type Config struct {
19 // Enabled indicates whether to not enqueue and batch before exporting.
20 Enabled bool `mapstructure:"enabled"`
21
22 // WaitForResult determines if incoming requests are blocked until the request is processed or not.
23 // Currently, this option is not available when persistent queue is configured using the storage configuration.
24 WaitForResult bool `mapstructure:"wait_for_result"`
25
26 // Sizer determines the type of size measurement used by this component.
27 // It accepts "requests", "items", or "bytes".
28 Sizer request.SizerType `mapstructure:"sizer"`
29
30 // QueueSize represents the maximum data size allowed for concurrent storage and processing.
31 QueueSize int64 `mapstructure:"queue_size"`
32
33 // BlockOnOverflow determines the behavior when the component's TotalSize limit is reached.
34 // If true, the component will wait for space; otherwise, operations will immediately return a retryable error.
35 BlockOnOverflow bool `mapstructure:"block_on_overflow"`
36
37 // StorageID if not empty, enables the persistent storage and uses the component specified
38 // as a storage extension for the persistent queue.
39 // TODO: This will be changed to Optional when available.
40 // See https://github.com/open-telemetry/opentelemetry-collector/issues/13822
41 StorageID *component.ID `mapstructure:"storage"`
42
43 // NumConsumers is the maximum number of concurrent consumers from the queue.
44 // This applies across all different optional configurations from above (e.g. wait_for_result, block_on_overflow, storage, etc.).
45 NumConsumers int `mapstructure:"num_consumers"`
46
47 // BatchConfig it configures how the requests are consumed from the queue and batch together during consumption.
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 // If all of the following hold:
57 // 1. the sizer is set,
58 // 2. the batch sizer is not set and
59 // 3. the batch section is nonempty,
60 // then use the same value as the queue sizer.
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 // Validate checks if the Config is valid
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 // Only support request sizer for persistent queue at this moment.
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 // Avoid situations where the queue is not able to hold any data.
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 // BatchConfig defines a configuration for batching requests based on a timeout and a minimum number of items.
97 type BatchConfig struct {
98 // FlushTimeout sets the time after which a batch will be sent regardless of its size.
99 FlushTimeout time.Duration `mapstructure:"flush_timeout"`
100
101 // Sizer determines the type of size measurement used by the batch.
102 // If not configured, use the same configuration as the queue.
103 // It accepts "requests", "items", or "bytes".
104 Sizer request.SizerType `mapstructure:"sizer"`
105
106 // MinSize defines the configuration for the minimum size of a batch.
107 MinSize int64 `mapstructure:"min_size"`
108
109 // MaxSize defines the configuration for the maximum size of a batch.
110 MaxSize int64 `mapstructure:"max_size"`
111 }
112
113 func (cfg *BatchConfig) Validate() error {
114 if cfg == nil {
115 return nil
116 }
117
118 // Only support items or bytes sizer for batch at this moment.
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 }