Skip to content

Commit 1692a18

Browse files
committed
[chore][cmd/mdatagen] Require all fields to have a non-empty description
1 parent 52e6bf4 commit 1692a18

15 files changed

Lines changed: 204 additions & 8 deletions

File tree

cmd/mdatagen/internal/command.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,11 @@ func generateConfigFiles(md Metadata, mdDir, importRootPath string) error {
692692
return fmt.Errorf("failed to resolve config schema: %w", err)
693693
}
694694

695+
if missing := resolvedSchema.CollectMissingDescriptions(); len(missing) > 0 {
696+
return fmt.Errorf("one or more config field(s) missing a description:\n\t%s",
697+
strings.Join(missing, "\n\t"))
698+
}
699+
695700
// do a shallow copy of Metadata and replace Config with resolved schema
696701
mdWithConfig := md
697702
mdWithConfig.ConfigsMetadata = resolvedSchema

cmd/mdatagen/internal/command_test.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ func TestGenerateConfigFiles_ExportedConfigsWithoutConfig(t *testing.T) {
600600
"sample_config": {
601601
Type: "object",
602602
Properties: map[string]*cfggen.ConfigMetadata{
603-
"endpoint": {Type: "string"},
603+
"endpoint": {Type: "string", Description: "The endpoint to connect to."},
604604
},
605605
},
606606
},
@@ -635,14 +635,14 @@ func TestGenerateConfigFiles_ExportedConfigsWithConfig(t *testing.T) {
635635
Config: &cfggen.ConfigMetadata{
636636
Type: "object",
637637
Properties: map[string]*cfggen.ConfigMetadata{
638-
"endpoint": {Type: "string"},
638+
"endpoint": {Type: "string", Description: "The endpoint to connect to."},
639639
},
640640
},
641641
ExportedConfigs: map[string]*cfggen.ConfigMetadata{
642642
"sample_config": {
643643
Type: "object",
644644
Properties: map[string]*cfggen.ConfigMetadata{
645-
"host_name": {Type: "string"},
645+
"host_name": {Type: "string", Description: "Host name to connect to."},
646646
},
647647
},
648648
},
@@ -664,6 +664,36 @@ func TestGenerateConfigFiles_ExportedConfigsWithConfig(t *testing.T) {
664664
require.Contains(t, string(generatedConfig), "type SampleConfig struct")
665665
}
666666

667+
func TestGenerateConfigFiles_MissingDescriptionFails(t *testing.T) {
668+
root := t.TempDir()
669+
tmpdir := filepath.Join(root, "shortname")
670+
require.NoError(t, os.MkdirAll(tmpdir, 0o700))
671+
require.NoError(t, os.WriteFile(filepath.Join(root, "go.mod"), []byte("module testmodule\n"), 0o600))
672+
673+
md := Metadata{
674+
Type: "test",
675+
PackageName: "testmodule/shortname",
676+
Status: &Status{Class: "receiver"},
677+
ConfigsMetadata: &cfggen.ConfigsMetadata{
678+
Config: &cfggen.ConfigMetadata{
679+
Type: "object",
680+
Properties: map[string]*cfggen.ConfigMetadata{
681+
"endpoint": {Type: "string", Description: "The endpoint to connect to."},
682+
"timeout": {Type: "duration"},
683+
},
684+
},
685+
},
686+
}
687+
688+
err := generateConfigFiles(md, tmpdir, "testmodule")
689+
require.Error(t, err)
690+
require.Contains(t, err.Error(), "config.timeout")
691+
require.NotContains(t, err.Error(), "config.endpoint")
692+
693+
// No files should have been generated when validation fails.
694+
require.NoFileExists(t, filepath.Join(tmpdir, "generated_config.go"))
695+
}
696+
667697
func TestInjectInternalMetadataDefs(t *testing.T) {
668698
t.Run("skips when metadata has no internal definitions", func(t *testing.T) {
669699
src := &cfggen.ConfigsMetadata{}

cmd/mdatagen/internal/loader_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func TestLoadMetadata(t *testing.T) {
9393
Default: "localhost:12345",
9494
},
9595
"sample_pkg": {
96-
Ref: "../samplepkg.sample_config",
96+
Description: "Configuration imported from the sample shared package.",
97+
Ref: "../samplepkg.sample_config",
9798
},
9899
"timeout": {
99100
Description: "Timeout for scraping metrics.",

cmd/mdatagen/internal/samplereceiver/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ This is where warnings are described.
4141
| `max_results` | int | 100 | no | Maximum number of results to return per scrape. |
4242
| `metrics` | object (see [metrics](#metrics)) | | no | MetricsConfig provides config for sample metrics. |
4343
| `resource_attributes` | object (see [resource_attributes](#resource_attributes)) | | no | ResourceAttributesConfig provides config for sample resource attributes. |
44-
| `sample_pkg` | object (see [sample_pkg](#sample_pkg)) | | no | |
44+
| `sample_pkg` | object (see [sample_pkg](#sample_pkg)) | | no | Configuration imported from the sample shared package. |
4545
| `timeout` | duration | 10s | no | Timeout for scraping metrics. |
4646

4747
### <a id="metrics"></a>metrics

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"maximum": 10000
4444
}
4545
},
46+
"description": "Configuration imported from the sample shared package.",
4647
"required": [
4748
"host_name"
4849
]

cmd/mdatagen/internal/samplereceiver/generated_config.go

Lines changed: 3 additions & 2 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ config:
6666
description: Extra HTTP headers to attach to each request.
6767
type: opaque_map
6868
sample_pkg:
69+
description: Configuration imported from the sample shared package.
6970
$ref: ../samplepkg.sample_config
7071
required: [endpoint]
7172

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"properties": {
3232
"interval": {
3333
"type": "string",
34+
"description": "Scrape interval for this target.",
3435
"default": "10s",
3536
"pattern": "^([0-9]+(\\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$"
3637
},

cmd/mdatagen/internal/samplescraper/generated_config.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplescraper/metadata.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ config:
6161
type: object
6262
properties:
6363
interval:
64+
description: Scrape interval for this target.
6465
type: duration
6566
x-optional: true
6667
default: 10s

0 commit comments

Comments
 (0)