Skip to content

Commit 0dff746

Browse files
[cmd/mdatagen] Add extended type aliases for configuration schema (#15514)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Adds first-class extended type aliases to cmd/mdatagen, so `metadata.yaml` authors can write a Go-idiomatic type name directly as the type field of a config property, instead of combining `type` + `format` or `type` + `x-customType` by hand. <!-- Issue number if applicable --> #### Link to tracking issue Fixes #15513 <!--Describe what testing was performed and which tests were added.--> #### Testing - Sample components extended with examples - Changes covered with unit tests <!--Describe the documentation added.--> #### Documentation Extended README of mdatagen with info about usage of extended types and how to add new. <!--Authorship attestation. See AGENTS.md for details. AI agents must not check this box on behalf of the user; the human author must check it themselves before the PR is ready for review.--> #### Authorship - [x] I, a human, wrote this pull request description myself. <!--Please delete paragraphs that you did not use before submitting.-->
1 parent 9ee240d commit 0dff746

15 files changed

Lines changed: 577 additions & 49 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp)
7+
component: cmd/mdatagen
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add first-class extended type aliases (int64, duration, opaque_string, id, opaque_map, etc.) to config schemas in metadata.yaml
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [15513]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
Authors can now write `type: int64`, `type: duration`, `type: opaque_string`, `type: id`, or
20+
`type: opaque_map` directly as a property type in the `config:` section of `metadata.yaml`.
21+
Each alias expands to the correct JSON Schema representation and Go type automatically.
22+
Existing uses of standard JSON Schema types, `format:`, and `x-customType:` remain supported
23+
without migration.
24+
25+
# Optional: The change log or logs in which this entry should be included.
26+
# e.g. '[user]' or '[user, api]'
27+
# Include 'user' if the change is relevant to end users.
28+
# Include 'api' if there is a change to a library API.
29+
# Default: '[user]'
30+
change_logs: [user, api]

cmd/mdatagen/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,50 @@ The `config` section is based on [JSON Schema standard](https://json-schema.org/
9797
- **References**: Internal (`$ref: definition_name`), external (`$ref: package.path.type`), or relative (`$ref: ./internal/config.type`)
9898
- **Reusable definitions**: Define common schemas in `$defs` and reference them with `$ref`
9999
- **Schema composition**: Use `allOf` for complex configurations
100+
- **Extended type aliases**: first-class aliases that expand to the correct JSON Schema shape and Go type automatically (see table below)
101+
102+
#### Extended type aliases
103+
104+
Instead of combining `type`+`format` or `type`+`x-customType` by hand, you can use an alias directly as the `type` value:
105+
106+
| Alias | Go type | JSON Schema representation |
107+
|---|---|-----------------------------------|
108+
| `rune` | `rune` | `integer` |
109+
| `byte` | `byte` | `integer` |
110+
| `uint` | `uint` | `integer` |
111+
| `int8` | `int8` | `integer` |
112+
| `uint8` | `uint8` | `integer` |
113+
| `int16` | `int16` | `integer` |
114+
| `uint16` | `uint16` | `integer` |
115+
| `int32` | `int32` | `integer` |
116+
| `uint32` | `uint32` | `integer` |
117+
| `int64` | `int64` | `integer` |
118+
| `uint64` | `uint64` | `integer` |
119+
| `float32` | `float32` | `number` |
120+
| `float64` | `float64` | `number` |
121+
| `duration` | `time.Duration` | `string` with Go duration pattern |
122+
| `time` | `time.Time` | `string` with `format: date-time` |
123+
| `opaque_string` | `configopaque.String` | `string` |
124+
| `opaque_map` | `configopaque.MapList` | `object` of name/value pairs |
125+
| `id` | `component.ID` | `string` |
126+
127+
Example:
128+
129+
```yaml
130+
config:
131+
type: object
132+
properties:
133+
max_results:
134+
type: int64
135+
default: 100
136+
timeout:
137+
type: duration
138+
default: 30s
139+
api_token:
140+
type: opaque_string
141+
```
142+
143+
Aliases are additive: existing uses of standard JSON Schema types, `format`, and `x-customType` remain supported without migration.
100144

101145
### Metrics Builder Configuration
102146

cmd/mdatagen/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/stretchr/testify v1.11.1
1010
go.opentelemetry.io/collector/component v1.63.0
1111
go.opentelemetry.io/collector/component/componenttest v0.157.0
12+
go.opentelemetry.io/collector/config/configopaque v1.63.0
1213
go.opentelemetry.io/collector/config/configoptional v1.63.0
1314
go.opentelemetry.io/collector/confmap v1.63.0
1415
go.opentelemetry.io/collector/confmap/provider/fileprovider v1.63.0

cmd/mdatagen/internal/loader_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,26 @@ func TestLoadMetadata(t *testing.T) {
9797
},
9898
"timeout": {
9999
Description: "Timeout for scraping metrics.",
100-
Type: "string",
101-
Format: "duration",
100+
Type: "duration",
102101
Default: "10s",
103102
},
103+
"max_results": {
104+
Description: "Maximum number of results to return per scrape.",
105+
Type: "int64",
106+
Default: 100,
107+
},
108+
"api_token": {
109+
Description: "API token used to authenticate with the endpoint.",
110+
Type: "opaque_string",
111+
},
112+
"component_id": {
113+
Description: "Component ID used to identify this receiver instance.",
114+
Type: "component_id",
115+
},
116+
"headers": {
117+
Description: "Extra HTTP headers to attach to each request.",
118+
Type: "opaque_map",
119+
},
104120
},
105121
Required: []string{"endpoint"},
106122
},

cmd/mdatagen/internal/samplereceiver/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ This is where warnings are described.
3434
## Configuration
3535
| Setting | Type | Default | Required | Description |
3636
| ------- | ---- | ------- | -------- | ----------- |
37+
| `api_token` | string | | no | API token used to authenticate with the endpoint. |
38+
| `component_id` | string | | no | Component ID used to identify this receiver instance. |
3739
| `endpoint` | string | localhost:12345 | **yes** | The endpoint to scrape metrics from. |
40+
| `headers` | map[string]string | | no | Extra HTTP headers to attach to each request. |
41+
| `max_results` | int | 100 | no | Maximum number of results to return per scrape. |
3842
| `metrics` | object (see [metrics](#metrics)) | | no | MetricsConfig provides config for sample metrics. |
3943
| `resource_attributes` | object (see [resource_attributes](#resource_attributes)) | | no | ResourceAttributesConfig provides config for sample resource attributes. |
4044
| `sample_pkg` | object (see [sample_pkg](#sample_pkg)) | | no | |

cmd/mdatagen/internal/samplereceiver/config.schema.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
{
22
"type": "object",
33
"properties": {
4+
"api_token": {
5+
"type": "string",
6+
"description": "API token used to authenticate with the endpoint."
7+
},
8+
"component_id": {
9+
"type": "string",
10+
"description": "Component ID used to identify this receiver instance."
11+
},
412
"endpoint": {
513
"type": "string",
614
"description": "The endpoint to scrape metrics from.",
715
"default": "localhost:12345"
816
},
17+
"headers": {
18+
"type": "object",
19+
"description": "Extra HTTP headers to attach to each request.",
20+
"additionalProperties": {
21+
"type": "string"
22+
}
23+
},
24+
"max_results": {
25+
"type": "integer",
26+
"description": "Maximum number of results to return per scrape.",
27+
"default": 100
28+
},
929
"sample_pkg": {
1030
"type": "object",
1131
"properties": {

cmd/mdatagen/internal/samplereceiver/generated_config.go

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/metadata.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,21 @@ config:
5050
default: "localhost:12345"
5151
timeout:
5252
description: Timeout for scraping metrics.
53-
type: string
54-
format: duration
53+
type: duration
5554
default: 10s
55+
max_results:
56+
description: Maximum number of results to return per scrape.
57+
type: int64
58+
default: 100
59+
api_token:
60+
description: API token used to authenticate with the endpoint.
61+
type: opaque_string
62+
component_id:
63+
description: Component ID used to identify this receiver instance.
64+
type: component_id
65+
headers:
66+
description: Extra HTTP headers to attach to each request.
67+
type: opaque_map
5668
sample_pkg:
5769
$ref: ../samplepkg.sample_config
5870
required: [endpoint]

cmd/mdatagen/internal/samplescraper/metadata.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ config:
4040
go_struct:
4141
anonymous: true
4242
component:
43-
type: string
43+
type: component_id
4444
description: Identifies the scraper, used for telemetry and logging.
45-
x-customType: go.opentelemetry.io/collector/component.ID
4645
go_struct:
4746
field_name: ComponentID
4847
job_name:
@@ -62,8 +61,7 @@ config:
6261
type: object
6362
properties:
6463
interval:
65-
type: string
66-
format: duration
64+
type: duration
6765
x-optional: true
6866
default: 10s
6967
labels:

config/configmiddleware/metadata.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ exported_configs:
1313
properties:
1414
id:
1515
description: Specifies the name of the extension to use.
16-
type: string
17-
x-customType: go.opentelemetry.io/collector/component.ID
16+
type: component_id

0 commit comments

Comments
 (0)