You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[chore] Add implementation details to Component Configuration Management RFC (#14486)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
Added implementation details and examples to RFC document
Changes were announced on `#otel-collector-dev` slack channel.
<!-- Issue number if applicable -->
#### Link to tracking issue
Extends #14433
---------
Co-authored-by: Pablo Baeyens <pablo.baeyens@datadoghq.com>
`#/$defs/MetricsBuilderConfig` would be automatically generated by mdatagen with the same process used to generate the go structs and documentation today.
104
+
#### Schema specification details
95
105
96
-
`go.opentelemetry.io/collector/scraper/scraperhelper#/$defs/ControllerConfig`would be generated by the new tool from the schema definition in the scraperhelper component.
- **special types** like time.Duration or time.Time can be represented as strings with specific formats (e.g., "duration", "date-time").
108
+
- **dependencies** are specified with `$ref` attribute to reference other schema definitions, either from external packages or internal definitions.
109
+
To identify references we need full package paths (for external) or use local paths (for internal). Examples:
110
+
- `go.opentelemetry.io/collector/scraper/scraperhelper.controller_config`full package path with type name after the last dot if we want to import schema from external repository.
111
+
- `/config/confighttp.client_config`absolute path with repository root as a base for internal references to other schema defined in the same repository.
112
+
- `./internal/metadata.metrics_builder_config`relative path for internal reference to a schema defined in the same repository.
113
+
114
+
Dependencies from external packages not covered with schemas will be replaced by `any` type with actual type annotation.
115
+
- **default values** can be specified using the `default` attribute.
116
+
- **validation** is possible using standard JSON Schema attributes, see [docs](https://json-schema.org/draft/2020-12/json-schema-validation).
97
117
98
118
#### Extensibility
99
119
100
-
The YAML schema specification can be extended with custom fields (e.g., `x-customType`) to capture domain-specific types and validation rules that are not natively supported in JSON schema. Additionally, we may introduce custom fields that generate fields that will produce references to structs or validation functions that require more complex logic and manual implementation.
120
+
The YAML schema specification can be extended with custom annotations to capture domain-specific types and validation rules that are not natively supported in JSON schema. Additionally, we may introduce custom fields that generate fields that will produce references to structs or validation functions that require more complex logic and manual implementation.
121
+
122
+
One proposal is to have `go` annotation that could contain the following sub-attributes:
123
+
- type [string] - allows specifying a custom Go type for the field in a format `<package>.<type_name>`.
124
+
- pointer [bool] - if set to true, the generated field will be a pointer to the specified type.
125
+
- optional [bool] - if set to true, the generated field will be wrapped in Optional[...] generic type.
126
+
- custom [list of strings] - allows specifying custom code snippets to be injected into the generated code.
127
+
The possible values are: `type`, `default`, `validate`.
128
+
Example:
129
+
```yaml
130
+
some_optional_field:
131
+
type: string
132
+
go:
133
+
type: com.github/custom/custompkg.CustomType
134
+
pointer: true
135
+
optional: true
136
+
custom: ['validate']
137
+
```
138
+
139
+
### Generated Go code example
140
+
141
+
See below for an example of the generated Go code from the above schema:
- The generated `config.go` file will define Go structs that mirror the configuration schema. It's capable
201
+
of handling nested objects and arrays and creating subtypes with auto-generated names.
202
+
- The `Validate` method will include validation logic based on the schema (e.g., required fields).
203
+
- A `createDefaultConfig` function will be generated to provide default values for the configuration.
204
+
205
+
#### Customization
206
+
207
+
Because schema defined field with custom type, validation and default value called `custom_field`, generated code will refer to placeholders that must be implemented in component's Go package.
208
+
This creates an implementation hook for component developers to add the custom logic.
209
+
210
+
In the above example:
211
+
1.`CustomFieldType` - a Go type for a `custom_field`
212
+
2.`ValidateCustomField` - a function that provides validation of `custom_field` values
213
+
3.`DefaultCustomFieldValue` - a function that returns default value(s) for `custom_field`
214
+
215
+
### Generated JSON Schema example
216
+
217
+
This file will be a direct translation of the `config` section from `metadata.yaml` into JSON Schema format.
"description": "MetricsConfig provides config for icmpcheckreceiver metrics.",
249
+
"type": "object",
250
+
"properties": { ... }
251
+
},
252
+
"metadata.resource_attribute_config": {
253
+
"description": "ResourceAttributeConfig provides common config for a particular resource attribute.",
254
+
"type": "object",
255
+
"properties": { ... }
256
+
},
257
+
"metadata.resource_attributes_config": { ... },
258
+
"scraperhelper.controller_config": {
259
+
"description": "ControllerConfig defines common settings for a scraper controller configuration. Scraper controller receivers can embed this struct, instead of receiver.Settings, and extend it with more fields if needed.",
260
+
"type": "object",
261
+
"properties": {
262
+
"collection_interval": {
263
+
"description": "CollectionInterval sets how frequently the scraper should be called and used as the context timeout to ensure that scrapers don't exceed the interval.",
0 commit comments