Debian Codegraph @

docs/rfcs/configuration-merging-strategy.md

raw
1 # Configuration merging
2
3 ## Background
4
5 As part of issue [#8754](https://github.com/open-telemetry/opentelemetry-collector/issues/8754), a new feature gate has been introduced to support merging component lists instead of replacing them ([first PR](https://github.com/open-telemetry/opentelemetry-collector/pull/12097)). This enhancement enables configurations from multiple sources to be combined, preserving all defined components in the final configuration.
6
7 More information about this feature can be found in the [confmap README](https://github.com/open-telemetry/opentelemetry-collector/blob/d4539dd6b4e554e15066226fa975b156af7b1510/confmap/README.md#experimental-append-merging-strategy-for-lists).
8
9 The main motivation for this change was to allow users to define configuration fragments in different sources, and have them merged in such a way that all specified components are included under `service::pipeline` in the final configuration.
10
11 Previously, we relied on Koanf’s default merging strategy, which overrides static values and slices (such as strings, numbers, and lists). This behavior often resulted in configurations being unintentionally overwritten when merged from multiple sources.
12
13 This issue has been highlighted in several discussions and feature requests:
14 - https://github.com/open-telemetry/opentelemetry-collector/issues/8394
15 - https://github.com/open-telemetry/opentelemetry-collector/issues/8754
16 - https://github.com/open-telemetry/opentelemetry-collector/issues/10370
17
18 ## Motivation and Scope
19
20 We’ve already implemented a feature gate and foundational logic that supports merging lists across configuration files. Currently, this logic is hardcoded to merge lists only for specific keys: receivers, exporters, and extensions. The relevant implementation can be found [here](https://github.com/open-telemetry/opentelemetry-collector/blob/main/confmap/internal/merge.go).
21
22 The current implementation lacks flexibility. Ideally, users should be able to specify which configuration paths should be merged, rather than relying on hardcoded defaults.
23 This RFC proposes extending the existing functionality by introducing a user-configurable mechanism to define merge behavior.
24
25 This RFC builds on top of feedback gathered from the [original PR](https://github.com/open-telemetry/opentelemetry-collector/pull/12097).
26 More specifically, this RFC aims to:
27 1. Add an option to specify which configuration paths should be merged.
28 2. Introduce support for prepend and append operations when merging list values:
29 - This is good to have for lists that rely on certain ordering, such as:
30 - `processors`
31 - `transformprocessor` statements
32
33 ## Proposed approaches:
34
35 ### Approach 1 (Recommended): Use yaml tags
36
37 The first approach relies on the concept of [yaml tags](https://tutorialreference.com/yaml/yaml-tags). We can specify a custom tag in our configuration files to indicate the lists we want to merge.
38
39 Consider following two configurations:
40
41 ```yaml
42 #main.yaml
43 receivers:
44 ...
45 exporters:
46 ...
47 extensions:
48 extension1:
49
50 service:
51 extensions: [extension1]
52 pipelines:
53 logs:
54 receivers: [...]
55 exporters: [...]
56 ```
57
58 ```yaml
59 #extra.yaml
60 extensions:
61 extension2:
62
63 service:
64 extensions: !mode=append [extension2]
65 ```
66
67 After running the collector with above configurations, the `service::extensions` path will get merged and final configuration will look like:
68
69 ```yaml
70 #final.yaml
71 receivers:
72 ...
73 exporters:
74 ...
75 extensions:
76 extension1:
77 extension2:
78
79 service:
80 extensions: [extension1, extension2]
81 pipelines:
82 logs:
83 receivers: [...]
84 exporters: [...]
85 ```
86
87 This approach completely relies on the configuration files and doesn't introduce any command line option.
88 Internally, we will loop through the yaml tree and fetch the paths we want to merged based on user-defined tags, then merge the lists found at those paths.
89
90 Our "custom" yaml tag will be in the format of URI query parameters. For starters, we can support following options:
91 1. `mode`:
92 - This setting will control the ordering of merged list.
93 - The value will be either of `append` or `prepend`.
94 - Default: `append`
95 2. `duplicates`:
96 - This setting controls the duplication of elements in the final list.
97 - If the user wants to allow duplicates, they can simply turn this flag on.
98 - Default: `false`
99 3. `recursive`:
100 - This setting controls the merging of child elements of the given yaml path.
101 - This is useful if user wants to merge all the lists under a give sub-tree.
102 - For eg. merging all the lists under the `service` section.
103
104 #### Examples of first approach
105
106 1. _Merge the `service::extensions` list_:
107
108 ```yaml
109 #main.yaml
110 receivers:
111 ...
112 exporters:
113 ...
114 extensions:
115 extension1:
116
117 service:
118 extensions: [extension1]
119 pipelines:
120 logs:
121 receivers: [...]
122 exporters: [...]
123
124 ---
125
126 #extra.yaml
127 extensions:
128 extension2:
129
130 service:
131 extensions: !mode=append [extension2]
132 ```
133
134 ```yaml
135 #final.yaml
136 receivers:
137 ...
138 exporters:
139 ...
140 extensions:
141 extension1:
142 extension2:
143
144 service:
145 extensions: [extension1, extension2]
146 pipelines:
147 logs:
148 receivers: [...]
149 exporters: [...]
150 ```
151
152 2. _Merge all the lists under the `service::*` section_:
153
154
155 ```yaml
156 #main.yaml
157 receivers:
158 ...
159 exporters:
160 ...
161 extensions:
162 extension1:
163
164 service:
165 extensions: [extension1]
166 pipelines:
167 logs:
168 receivers: [...]
169 exporters: [...]
170
171 ---
172
173 #extra.yaml
174 extensions:
175 extension2:
176 receiver:
177 receiver2:
178
179 service: !mode=append&recursive=true
180 extensions: [extension2]
181 pipelines:
182 logs:
183 receivers: [receiver2]
184 ```
185
186 ```yaml
187 #final.yaml
188 receivers:
189 ...
190 receiver2:
191 exporters:
192 ...
193 extensions:
194 extension1:
195 extension2:
196
197 service:
198 extensions: [extension1, extension2]
199 pipelines:
200 logs:
201 receivers: [..., receiver2]
202 exporters: [...]
203 ```
204
205 ### Approach 2: URI params
206
207 The proposed approach will rely on concept of URI query parameters([_RFC 3986_](https://datatracker.ietf.org/doc/html/rfc3986#page-23)). Our configuration URIs already adhere to this syntax and we can extend it to support query params instead adding new CLI flags.
208
209 For now, the new merging strategy is only enabled under `confmap.enableMergeAppendOption` gate. If user specifies the options and tries to run the collector without gate, we will merge as per default behaviour.
210
211 We will support new parameters to config URIs as follows:
212 1. `merge_paths`: A comma-separated list of glob patterns which will be used while config merging
213 - This setting will control the paths user wants to merge from the given config.
214 - Example:
215 - `otelcol --config main.yaml --config extra.yaml?merge_paths=service::extensions,service::**::receivers`
216 - In this example, we will merge the list of extensions and receivers from pipeline, excluding lists in the rest of the config.
217 - `otelcol --config main.yaml --config ext.yaml?merge_paths=service::extensions --config rec.yaml?merge_paths=service::**::receivers`
218 - In this example, we will merge all list of extensions from `ext.yml` and list of receivers from `rec.yaml`, excluding lists in the rest of the config.
219 2. `merge_mode`: One of `prepend` or `append`.
220 - This setting will control the ordering of merged list.
221
222 #### Examples of second approach
223
224 Here are some examples:
225
226 1. _Append to default mergeable components_:
227 ```bash
228 otelcol --config=main.yaml --config=extra_components.yaml?merge_mode=append --feature-gates=confmap.enableMergeAppendOption
229 ```
230
231 - After running above command, the final configuration will include:
232 - Merged component(s) (`receivers`, `exporters` and `extensions`) from `extra_components.yaml`
233
234 2. _Specify exact paths for merging_:
235 ```bash
236 otelcol \
237 --config=main.yaml \
238 --config=extra_extension.yaml?merge_mode=append&merge_paths=service::extensions \
239 --config=extra_receiver.yaml?merge_mode=append&merge_paths=service::**::receivers \
240 --feature-gates=confmap.enableMergeAppendOption
241 ```
242
243 - After running above command, the final configuration will include:
244 - Merged extension(s) from `extra_extension.yaml`
245 - Merged receiver(s) from `extra_receiver.yaml`
246
247
248 3. _Prepend processors_:
249 ```bash
250 otelcol --config=main.yaml --config=extra_processor.yaml?merge_mode=prepend&merge_paths=service::**::processors --feature-gates=confmap.enableMergeAppendOption
251 ```
252
253 - After running above command, the final configuration will include:
254 - Merged processor(s) from `extra_processor.yaml`, but prepend the existing list.
255
256 4. _Exclude a config file from lists merging process_:
257 ```bash
258 otelcol --config=main.yaml --config=extra_components.yaml?merge_mode=append --config override_components.yaml --feature-gates=confmap.enableMergeAppendOption
259 ```
260
261 - In the above command, we have no specified any options for `override_components.yaml`. Hence, it will override all the conflicting lists from previous configuration, which is the default behaviour.
262
263 ## Open questions
264
265 - What to do if an invalid option is provided for `merge_mode` or `merge_paths`?
266 - I can think of two possibilities:
267 1. Error out.
268 2. Log an error and merge the default way
269 - What to do if an invalid query param is provided in config URI?
270 - In this case, I strongly feel that we should error out.
271
272 ## Extensibility
273
274 This URI-based approach is highly extensible. In the future, it can enable advanced operations such as map overriding. Currently, it's impossible to do so.