diff --git a/cmd/mdatagen/README.md b/cmd/mdatagen/README.md index 63cc5260401..075a1fa9674 100644 --- a/cmd/mdatagen/README.md +++ b/cmd/mdatagen/README.md @@ -57,10 +57,10 @@ You can run `cd cmd/mdatagen && $(GOCMD) install .` to install the `mdatagen` to ## Contributing to the Metadata Generator -The code for generating the documentation can be found in [loader.go](./loader.go) and the templates for rendering the documentation can be found in [templates](./templates). +The code for generating the documentation can be found in [loader.go](./internal/loader.go) and the templates for rendering the documentation can be found in [templates](internal/templates). When making updates to the metadata generator or introducing support for new functionality: -1. Ensure the [metadata-schema.yaml](./metadata-schema.yaml) and [./metadata.yaml](metadata.yaml) files reflect the changes. +1. Ensure the [metadata-schema.yaml](./metadata-schema.yaml) and [metadata.yaml](./metadata.yaml) files reflect the changes. 2. Run `make mdatagen-test`. 3. Make sure all tests are passing including [generated tests](./internal/samplereceiver/internal/metadata/generated_metrics_test.go). 4. Run `make generate`. diff --git a/cmd/mdatagen/embeded_templates.go b/cmd/mdatagen/internal/embeded_templates.go similarity index 64% rename from cmd/mdatagen/embeded_templates.go rename to cmd/mdatagen/internal/embeded_templates.go index dace6c43f32..9aca0fe6a90 100644 --- a/cmd/mdatagen/embeded_templates.go +++ b/cmd/mdatagen/internal/embeded_templates.go @@ -1,13 +1,13 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal" import "embed" -// templateFS ensures that the files needed +// TemplateFS ensures that the files needed // to generate metadata as an embedded filesystem since // `go get` doesn't require these files to be downloaded. // //go:embed templates/*.tmpl templates/testdata/*.tmpl -var templateFS embed.FS +var TemplateFS embed.FS diff --git a/cmd/mdatagen/embeded_templates_test.go b/cmd/mdatagen/internal/embeded_templates_test.go similarity index 94% rename from cmd/mdatagen/embeded_templates_test.go rename to cmd/mdatagen/internal/embeded_templates_test.go index 40562297e30..ab4124f3d6b 100644 --- a/cmd/mdatagen/embeded_templates_test.go +++ b/cmd/mdatagen/internal/embeded_templates_test.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal import ( "io/fs" @@ -38,7 +38,7 @@ func TestEnsureTemplatesLoaded(t *testing.T) { } count = 0 ) - assert.NoError(t, fs.WalkDir(templateFS, ".", func(path string, d fs.DirEntry, _ error) error { + assert.NoError(t, fs.WalkDir(TemplateFS, ".", func(path string, d fs.DirEntry, _ error) error { if d != nil && d.IsDir() { return nil } diff --git a/cmd/mdatagen/lint.go b/cmd/mdatagen/internal/lint.go similarity index 87% rename from cmd/mdatagen/lint.go rename to cmd/mdatagen/internal/lint.go index f169ac4fdbb..5dfcfb4e0a2 100644 --- a/cmd/mdatagen/lint.go +++ b/cmd/mdatagen/internal/lint.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal" import ( "errors" @@ -11,8 +11,8 @@ import ( "go.opentelemetry.io/collector/cmd/mdatagen/third_party/golint" ) -// formatIdentifier variable in a go-safe way -func formatIdentifier(s string, exported bool) (string, error) { +// FormatIdentifier variable in a go-safe way +func FormatIdentifier(s string, exported bool) (string, error) { if s == "" { return "", errors.New("string cannot be empty") } diff --git a/cmd/mdatagen/lint_test.go b/cmd/mdatagen/internal/lint_test.go similarity index 94% rename from cmd/mdatagen/lint_test.go rename to cmd/mdatagen/internal/lint_test.go index f05d6615ea3..24789d1011b 100644 --- a/cmd/mdatagen/lint_test.go +++ b/cmd/mdatagen/internal/lint_test.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal import ( "testing" @@ -37,7 +37,7 @@ func TestFormatIdentifier(t *testing.T) { } for _, tt := range tests { t.Run(tt.input, func(t *testing.T) { - got, err := formatIdentifier(tt.input, tt.exported) + got, err := FormatIdentifier(tt.input, tt.exported) if tt.wantErr != "" { require.EqualError(t, err, tt.wantErr) diff --git a/cmd/mdatagen/loader.go b/cmd/mdatagen/internal/loader.go similarity index 86% rename from cmd/mdatagen/loader.go rename to cmd/mdatagen/internal/loader.go index 451bc0c5b4d..1df1a0d06bb 100644 --- a/cmd/mdatagen/loader.go +++ b/cmd/mdatagen/internal/loader.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal" import ( "context" @@ -19,24 +19,24 @@ import ( "go.opentelemetry.io/collector/pdata/pcommon" ) -type metricName string +type MetricName string -func (mn metricName) Render() (string, error) { - return formatIdentifier(string(mn), true) +func (mn MetricName) Render() (string, error) { + return FormatIdentifier(string(mn), true) } -func (mn metricName) RenderUnexported() (string, error) { - return formatIdentifier(string(mn), false) +func (mn MetricName) RenderUnexported() (string, error) { + return FormatIdentifier(string(mn), false) } -type attributeName string +type AttributeName string -func (mn attributeName) Render() (string, error) { - return formatIdentifier(string(mn), true) +func (mn AttributeName) Render() (string, error) { + return FormatIdentifier(string(mn), true) } -func (mn attributeName) RenderUnexported() (string, error) { - return formatIdentifier(string(mn), false) +func (mn AttributeName) RenderUnexported() (string, error) { + return FormatIdentifier(string(mn), false) } // ValueType defines an attribute value type. @@ -97,7 +97,7 @@ func (mvt ValueType) Primitive() string { } } -type metric struct { +type Metric struct { // Enabled defines whether the metric is enabled by default. Enabled bool `mapstructure:"enabled"` @@ -126,7 +126,7 @@ type metric struct { Histogram *histogram `mapstructure:"histogram,omitempty"` // Attributes is the list of attributes that the metric emits. - Attributes []attributeName `mapstructure:"attributes"` + Attributes []AttributeName `mapstructure:"attributes"` // Level specifies the minimum `configtelemetry.Level` for which // the metric will be emitted. This only applies to internal telemetry @@ -134,13 +134,13 @@ type metric struct { Level configtelemetry.Level `mapstructure:"level"` } -func (m *metric) Unmarshal(parser *confmap.Conf) error { +func (m *Metric) Unmarshal(parser *confmap.Conf) error { if !parser.IsSet("enabled") { return errors.New("missing required field: `enabled`") } return parser.Unmarshal(m) } -func (m metric) Data() MetricData { +func (m Metric) Data() MetricData { if m.Sum != nil { return m.Sum } @@ -162,7 +162,7 @@ type warnings struct { IfConfigured string `mapstructure:"if_configured"` } -type attribute struct { +type Attribute struct { // Description describes the purpose of the attribute. Description string `mapstructure:"description"` // NameOverride can be used to override the attribute name. @@ -178,20 +178,20 @@ type attribute struct { // Type is an attribute type. Type ValueType `mapstructure:"type"` // FullName is the attribute name populated from the map key. - FullName attributeName `mapstructure:"-"` + FullName AttributeName `mapstructure:"-"` // Warnings that will be shown to user under specified conditions. Warnings warnings `mapstructure:"warnings"` } // Name returns actual name of the attribute that is set on the metric after applying NameOverride. -func (a attribute) Name() attributeName { +func (a Attribute) Name() AttributeName { if a.NameOverride != "" { - return attributeName(a.NameOverride) + return AttributeName(a.NameOverride) } return a.FullName } -func (a attribute) TestValue() string { +func (a Attribute) TestValue() string { if a.Enum != nil { return fmt.Sprintf(`"%s"`, a.Enum[0]) } @@ -239,7 +239,7 @@ type tests struct { type telemetry struct { Level configtelemetry.Level `mapstructure:"level"` - Metrics map[metricName]metric `mapstructure:"metrics"` + Metrics map[MetricName]Metric `mapstructure:"metrics"` } func (t telemetry) Levels() map[string]interface{} { @@ -250,7 +250,7 @@ func (t telemetry) Levels() map[string]interface{} { return levels } -type metadata struct { +type Metadata struct { // Type of the component. Type string `mapstructure:"type"` // Type of the parent component (applicable to subcomponents). @@ -262,11 +262,11 @@ type metadata struct { // SemConvVersion is a version number of OpenTelemetry semantic conventions applied to the scraped metrics. SemConvVersion string `mapstructure:"sem_conv_version"` // ResourceAttributes that can be emitted by the component. - ResourceAttributes map[attributeName]attribute `mapstructure:"resource_attributes"` + ResourceAttributes map[AttributeName]Attribute `mapstructure:"resource_attributes"` // Attributes emitted by one or more metrics. - Attributes map[attributeName]attribute `mapstructure:"attributes"` + Attributes map[AttributeName]Attribute `mapstructure:"attributes"` // Metrics that can be emitted by the component. - Metrics map[metricName]metric `mapstructure:"metrics"` + Metrics map[MetricName]Metric `mapstructure:"metrics"` // GithubProject is the project where the component README lives in the format of org/repo, defaults to open-telemetry/opentelemetry-collector-contrib GithubProject string `mapstructure:"github_project"` // ScopeName of the metrics emitted by the component. @@ -277,31 +277,31 @@ type metadata struct { Tests tests `mapstructure:"tests"` } -func setAttributesFullName(attrs map[attributeName]attribute) { +func setAttributesFullName(attrs map[AttributeName]Attribute) { for k, v := range attrs { v.FullName = k attrs[k] = v } } -type templateContext struct { - metadata +type TemplateContext struct { + Metadata // Package name for generated code. Package string } -func loadMetadata(filePath string) (metadata, error) { +func LoadMetadata(filePath string) (Metadata, error) { cp, err := fileprovider.NewFactory().Create(confmaptest.NewNopProviderSettings()).Retrieve(context.Background(), "file:"+filePath, nil) if err != nil { - return metadata{}, err + return Metadata{}, err } conf, err := cp.AsConf() if err != nil { - return metadata{}, err + return Metadata{}, err } - md := metadata{ShortFolderName: shortFolderName(filePath), Tests: tests{Host: "componenttest.NewNopHost()"}} + md := Metadata{ShortFolderName: shortFolderName(filePath), Tests: tests{Host: "componenttest.NewNopHost()"}} if err = conf.Unmarshal(&md); err != nil { return md, err } diff --git a/cmd/mdatagen/loader_test.go b/cmd/mdatagen/internal/loader_test.go similarity index 94% rename from cmd/mdatagen/loader_test.go rename to cmd/mdatagen/internal/loader_test.go index 68dcfb84e0f..6bb7c1f7485 100644 --- a/cmd/mdatagen/loader_test.go +++ b/cmd/mdatagen/internal/loader_test.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal import ( "testing" @@ -16,12 +16,12 @@ import ( func TestLoadMetadata(t *testing.T) { tests := []struct { name string - want metadata + want Metadata wantErr string }{ { - name: "internal/samplereceiver/metadata.yaml", - want: metadata{ + name: "samplereceiver/metadata.yaml", + want: Metadata{ GithubProject: "open-telemetry/opentelemetry-collector", Type: "sample", SemConvVersion: "1.9.0", @@ -39,7 +39,7 @@ func TestLoadMetadata(t *testing.T) { Warnings: []string{"Any additional information that should be brought to the consumer's attention"}, UnsupportedPlatforms: []string{"freebsd", "illumos"}, }, - ResourceAttributes: map[attributeName]attribute{ + ResourceAttributes: map[AttributeName]Attribute{ "string.resource.attr": { Description: "Resource attribute with any string value.", Enabled: true, @@ -116,7 +116,7 @@ func TestLoadMetadata(t *testing.T) { }, }, - Attributes: map[attributeName]attribute{ + Attributes: map[AttributeName]Attribute{ "enum_attr": { Description: "Attribute with a known set of string values.", NameOverride: "", @@ -171,7 +171,7 @@ func TestLoadMetadata(t *testing.T) { FullName: "map_attr", }, }, - Metrics: map[metricName]metric{ + Metrics: map[MetricName]Metric{ "default.metric": { Enabled: true, Description: "Monotonic cumulative sum int metric enabled by default.", @@ -185,7 +185,7 @@ func TestLoadMetadata(t *testing.T) { AggregationTemporality: AggregationTemporality{Aggregation: pmetric.AggregationTemporalityCumulative}, Mono: Mono{Monotonic: true}, }, - Attributes: []attributeName{"string_attr", "overridden_int_attr", "enum_attr", "slice_attr", "map_attr"}, + Attributes: []AttributeName{"string_attr", "overridden_int_attr", "enum_attr", "slice_attr", "map_attr"}, }, "optional.metric": { Enabled: false, @@ -197,7 +197,7 @@ func TestLoadMetadata(t *testing.T) { Gauge: &gauge{ MetricValueType: MetricValueType{pmetric.NumberDataPointValueTypeDouble}, }, - Attributes: []attributeName{"string_attr", "boolean_attr", "boolean_attr2"}, + Attributes: []AttributeName{"string_attr", "boolean_attr", "boolean_attr2"}, }, "optional.metric.empty_unit": { Enabled: false, @@ -209,7 +209,7 @@ func TestLoadMetadata(t *testing.T) { Gauge: &gauge{ MetricValueType: MetricValueType{pmetric.NumberDataPointValueTypeDouble}, }, - Attributes: []attributeName{"string_attr", "boolean_attr"}, + Attributes: []AttributeName{"string_attr", "boolean_attr"}, }, "default.metric.to_be_removed": { @@ -236,11 +236,11 @@ func TestLoadMetadata(t *testing.T) { AggregationTemporality: AggregationTemporality{Aggregation: pmetric.AggregationTemporalityCumulative}, Mono: Mono{Monotonic: true}, }, - Attributes: []attributeName{"string_attr", "overridden_int_attr", "enum_attr", "slice_attr", "map_attr"}, + Attributes: []AttributeName{"string_attr", "overridden_int_attr", "enum_attr", "slice_attr", "map_attr"}, }, }, Telemetry: telemetry{ - Metrics: map[metricName]metric{ + Metrics: map[MetricName]Metric{ "batch_size_trigger_send": { Enabled: true, Description: "Number of times the batch was sent due to a size trigger", @@ -293,27 +293,27 @@ func TestLoadMetadata(t *testing.T) { }, { name: "testdata/parent.yaml", - want: metadata{ + want: Metadata{ Type: "subcomponent", Parent: "parentComponent", - ScopeName: "go.opentelemetry.io/collector/cmd/mdatagen", + ScopeName: "go.opentelemetry.io/collector/cmd/mdatagen/internal", ShortFolderName: "testdata", Tests: tests{Host: "componenttest.NewNopHost()"}, }, }, { name: "testdata/invalid_type_rattr.yaml", - want: metadata{}, + want: Metadata{}, wantErr: "decoding failed due to the following error(s):\n\nerror decoding 'resource_attributes[string.resource.attr].type': invalid type: \"invalidtype\"", }, { name: "testdata/no_enabled.yaml", - want: metadata{}, + want: Metadata{}, wantErr: "decoding failed due to the following error(s):\n\nerror decoding 'metrics[system.cpu.time]': missing required field: `enabled`", }, { name: "testdata/no_value_type.yaml", - want: metadata{}, + want: Metadata{}, wantErr: "decoding failed due to the following error(s):\n\nerror decoding 'metrics[system.cpu.time]': decoding failed due to the following error(s):\n\n" + "error decoding 'sum': missing required field: `value_type`", }, @@ -323,18 +323,18 @@ func TestLoadMetadata(t *testing.T) { }, { name: "testdata/invalid_aggregation.yaml", - want: metadata{}, + want: Metadata{}, wantErr: "decoding failed due to the following error(s):\n\nerror decoding 'metrics[default.metric]': decoding failed due to the following error(s):\n\nerror decoding 'sum': decoding failed due to the following error(s):\n\nerror decoding 'aggregation_temporality': invalid aggregation: \"invalidaggregation\"", }, { name: "testdata/invalid_type_attr.yaml", - want: metadata{}, + want: Metadata{}, wantErr: "decoding failed due to the following error(s):\n\nerror decoding 'attributes[used_attr].type': invalid type: \"invalidtype\"", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := loadMetadata(tt.name) + got, err := LoadMetadata(tt.name) if tt.wantErr != "" { require.Error(t, err) require.EqualError(t, err, tt.wantErr) diff --git a/cmd/mdatagen/metricdata.go b/cmd/mdatagen/internal/metricdata.go similarity index 97% rename from cmd/mdatagen/metricdata.go rename to cmd/mdatagen/internal/metricdata.go index 4749e03e608..ae69f6bc2ad 100644 --- a/cmd/mdatagen/metricdata.go +++ b/cmd/mdatagen/internal/metricdata.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal" import ( "errors" @@ -179,7 +179,7 @@ func (d *sum) Unmarshal(parser *confmap.Conf) error { } // TODO: Currently, this func will not be called because of https://github.com/open-telemetry/opentelemetry-collector/issues/6671. Uncomment function and -// add a test case to Test_loadMetadata for file no_monotonic.yaml once the issue is solved. +// add a test case to Test_LoadMetadata for file no_monotonic.yaml once the issue is solved. // // Unmarshal is a custom unmarshaler for Mono. // func (m *Mono) Unmarshal(parser *confmap.Conf) error { diff --git a/cmd/mdatagen/metricdata_test.go b/cmd/mdatagen/internal/metricdata_test.go similarity index 99% rename from cmd/mdatagen/metricdata_test.go rename to cmd/mdatagen/internal/metricdata_test.go index 1e56afff27c..4e889044e42 100644 --- a/cmd/mdatagen/metricdata_test.go +++ b/cmd/mdatagen/internal/metricdata_test.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal import ( "testing" diff --git a/cmd/mdatagen/statusdata.go b/cmd/mdatagen/internal/statusdata.go similarity index 96% rename from cmd/mdatagen/statusdata.go rename to cmd/mdatagen/internal/statusdata.go index f09c68f98e9..56938d79e83 100644 --- a/cmd/mdatagen/statusdata.go +++ b/cmd/mdatagen/internal/statusdata.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal" import ( "errors" @@ -16,7 +16,7 @@ import ( // The rules below apply to every distribution added to this list: // - The distribution is open source and maintained by the OpenTelemetry project. // - The link must point to a publicly accessible repository. -var distros = map[string]string{ +var Distros = map[string]string{ "core": "https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol", "contrib": "https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib", "k8s": "https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-k8s", diff --git a/cmd/mdatagen/statusdata_test.go b/cmd/mdatagen/internal/statusdata_test.go similarity index 98% rename from cmd/mdatagen/statusdata_test.go rename to cmd/mdatagen/internal/statusdata_test.go index 947065b1d6f..30813d8d851 100644 --- a/cmd/mdatagen/statusdata_test.go +++ b/cmd/mdatagen/internal/statusdata_test.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal import ( "testing" diff --git a/cmd/mdatagen/templates/component_telemetry_test.go.tmpl b/cmd/mdatagen/internal/templates/component_telemetry_test.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/component_telemetry_test.go.tmpl rename to cmd/mdatagen/internal/templates/component_telemetry_test.go.tmpl diff --git a/cmd/mdatagen/templates/component_test.go.tmpl b/cmd/mdatagen/internal/templates/component_test.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/component_test.go.tmpl rename to cmd/mdatagen/internal/templates/component_test.go.tmpl diff --git a/cmd/mdatagen/templates/config.go.tmpl b/cmd/mdatagen/internal/templates/config.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/config.go.tmpl rename to cmd/mdatagen/internal/templates/config.go.tmpl diff --git a/cmd/mdatagen/templates/config_test.go.tmpl b/cmd/mdatagen/internal/templates/config_test.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/config_test.go.tmpl rename to cmd/mdatagen/internal/templates/config_test.go.tmpl diff --git a/cmd/mdatagen/templates/documentation.md.tmpl b/cmd/mdatagen/internal/templates/documentation.md.tmpl similarity index 100% rename from cmd/mdatagen/templates/documentation.md.tmpl rename to cmd/mdatagen/internal/templates/documentation.md.tmpl diff --git a/cmd/mdatagen/templates/metrics.go.tmpl b/cmd/mdatagen/internal/templates/metrics.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/metrics.go.tmpl rename to cmd/mdatagen/internal/templates/metrics.go.tmpl diff --git a/cmd/mdatagen/templates/metrics_test.go.tmpl b/cmd/mdatagen/internal/templates/metrics_test.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/metrics_test.go.tmpl rename to cmd/mdatagen/internal/templates/metrics_test.go.tmpl diff --git a/cmd/mdatagen/templates/package_test.go.tmpl b/cmd/mdatagen/internal/templates/package_test.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/package_test.go.tmpl rename to cmd/mdatagen/internal/templates/package_test.go.tmpl diff --git a/cmd/mdatagen/templates/readme.md.tmpl b/cmd/mdatagen/internal/templates/readme.md.tmpl similarity index 100% rename from cmd/mdatagen/templates/readme.md.tmpl rename to cmd/mdatagen/internal/templates/readme.md.tmpl diff --git a/cmd/mdatagen/templates/resource.go.tmpl b/cmd/mdatagen/internal/templates/resource.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/resource.go.tmpl rename to cmd/mdatagen/internal/templates/resource.go.tmpl diff --git a/cmd/mdatagen/templates/resource_test.go.tmpl b/cmd/mdatagen/internal/templates/resource_test.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/resource_test.go.tmpl rename to cmd/mdatagen/internal/templates/resource_test.go.tmpl diff --git a/cmd/mdatagen/templates/status.go.tmpl b/cmd/mdatagen/internal/templates/status.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/status.go.tmpl rename to cmd/mdatagen/internal/templates/status.go.tmpl diff --git a/cmd/mdatagen/templates/telemetry.go.tmpl b/cmd/mdatagen/internal/templates/telemetry.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/telemetry.go.tmpl rename to cmd/mdatagen/internal/templates/telemetry.go.tmpl diff --git a/cmd/mdatagen/templates/telemetry_test.go.tmpl b/cmd/mdatagen/internal/templates/telemetry_test.go.tmpl similarity index 100% rename from cmd/mdatagen/templates/telemetry_test.go.tmpl rename to cmd/mdatagen/internal/templates/telemetry_test.go.tmpl diff --git a/cmd/mdatagen/templates/testdata/config.yaml.tmpl b/cmd/mdatagen/internal/templates/testdata/config.yaml.tmpl similarity index 100% rename from cmd/mdatagen/templates/testdata/config.yaml.tmpl rename to cmd/mdatagen/internal/templates/testdata/config.yaml.tmpl diff --git a/cmd/mdatagen/testdata/async_metric.yaml b/cmd/mdatagen/internal/testdata/async_metric.yaml similarity index 100% rename from cmd/mdatagen/testdata/async_metric.yaml rename to cmd/mdatagen/internal/testdata/async_metric.yaml diff --git a/cmd/mdatagen/testdata/invalid.yaml b/cmd/mdatagen/internal/testdata/invalid.yaml similarity index 100% rename from cmd/mdatagen/testdata/invalid.yaml rename to cmd/mdatagen/internal/testdata/invalid.yaml diff --git a/cmd/mdatagen/testdata/invalid_aggregation.yaml b/cmd/mdatagen/internal/testdata/invalid_aggregation.yaml similarity index 100% rename from cmd/mdatagen/testdata/invalid_aggregation.yaml rename to cmd/mdatagen/internal/testdata/invalid_aggregation.yaml diff --git a/cmd/mdatagen/testdata/invalid_class.yaml b/cmd/mdatagen/internal/testdata/invalid_class.yaml similarity index 100% rename from cmd/mdatagen/testdata/invalid_class.yaml rename to cmd/mdatagen/internal/testdata/invalid_class.yaml diff --git a/cmd/mdatagen/testdata/invalid_input_type.yaml b/cmd/mdatagen/internal/testdata/invalid_input_type.yaml similarity index 100% rename from cmd/mdatagen/testdata/invalid_input_type.yaml rename to cmd/mdatagen/internal/testdata/invalid_input_type.yaml diff --git a/cmd/mdatagen/testdata/invalid_stability.yaml b/cmd/mdatagen/internal/testdata/invalid_stability.yaml similarity index 100% rename from cmd/mdatagen/testdata/invalid_stability.yaml rename to cmd/mdatagen/internal/testdata/invalid_stability.yaml diff --git a/cmd/mdatagen/testdata/invalid_stability_component.yaml b/cmd/mdatagen/internal/testdata/invalid_stability_component.yaml similarity index 100% rename from cmd/mdatagen/testdata/invalid_stability_component.yaml rename to cmd/mdatagen/internal/testdata/invalid_stability_component.yaml diff --git a/cmd/mdatagen/testdata/invalid_telemetry_missing_value_type_for_histogram.yaml b/cmd/mdatagen/internal/testdata/invalid_telemetry_missing_value_type_for_histogram.yaml similarity index 100% rename from cmd/mdatagen/testdata/invalid_telemetry_missing_value_type_for_histogram.yaml rename to cmd/mdatagen/internal/testdata/invalid_telemetry_missing_value_type_for_histogram.yaml diff --git a/cmd/mdatagen/testdata/invalid_type_attr.yaml b/cmd/mdatagen/internal/testdata/invalid_type_attr.yaml similarity index 100% rename from cmd/mdatagen/testdata/invalid_type_attr.yaml rename to cmd/mdatagen/internal/testdata/invalid_type_attr.yaml diff --git a/cmd/mdatagen/testdata/invalid_type_rattr.yaml b/cmd/mdatagen/internal/testdata/invalid_type_rattr.yaml similarity index 100% rename from cmd/mdatagen/testdata/invalid_type_rattr.yaml rename to cmd/mdatagen/internal/testdata/invalid_type_rattr.yaml diff --git a/cmd/mdatagen/testdata/metrics_and_type.yaml b/cmd/mdatagen/internal/testdata/metrics_and_type.yaml similarity index 100% rename from cmd/mdatagen/testdata/metrics_and_type.yaml rename to cmd/mdatagen/internal/testdata/metrics_and_type.yaml diff --git a/cmd/mdatagen/testdata/no_aggregation.yaml b/cmd/mdatagen/internal/testdata/no_aggregation.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_aggregation.yaml rename to cmd/mdatagen/internal/testdata/no_aggregation.yaml diff --git a/cmd/mdatagen/testdata/no_class.yaml b/cmd/mdatagen/internal/testdata/no_class.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_class.yaml rename to cmd/mdatagen/internal/testdata/no_class.yaml diff --git a/cmd/mdatagen/testdata/no_description_attr.yaml b/cmd/mdatagen/internal/testdata/no_description_attr.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_description_attr.yaml rename to cmd/mdatagen/internal/testdata/no_description_attr.yaml diff --git a/cmd/mdatagen/testdata/no_description_rattr.yaml b/cmd/mdatagen/internal/testdata/no_description_rattr.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_description_rattr.yaml rename to cmd/mdatagen/internal/testdata/no_description_rattr.yaml diff --git a/cmd/mdatagen/testdata/no_enabled.yaml b/cmd/mdatagen/internal/testdata/no_enabled.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_enabled.yaml rename to cmd/mdatagen/internal/testdata/no_enabled.yaml diff --git a/cmd/mdatagen/testdata/no_metric_description.yaml b/cmd/mdatagen/internal/testdata/no_metric_description.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_metric_description.yaml rename to cmd/mdatagen/internal/testdata/no_metric_description.yaml diff --git a/cmd/mdatagen/testdata/no_metric_type.yaml b/cmd/mdatagen/internal/testdata/no_metric_type.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_metric_type.yaml rename to cmd/mdatagen/internal/testdata/no_metric_type.yaml diff --git a/cmd/mdatagen/testdata/no_metric_unit.yaml b/cmd/mdatagen/internal/testdata/no_metric_unit.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_metric_unit.yaml rename to cmd/mdatagen/internal/testdata/no_metric_unit.yaml diff --git a/cmd/mdatagen/testdata/no_monotonic.yaml b/cmd/mdatagen/internal/testdata/no_monotonic.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_monotonic.yaml rename to cmd/mdatagen/internal/testdata/no_monotonic.yaml diff --git a/cmd/mdatagen/testdata/no_stability.yaml b/cmd/mdatagen/internal/testdata/no_stability.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_stability.yaml rename to cmd/mdatagen/internal/testdata/no_stability.yaml diff --git a/cmd/mdatagen/testdata/no_stability_component.yaml b/cmd/mdatagen/internal/testdata/no_stability_component.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_stability_component.yaml rename to cmd/mdatagen/internal/testdata/no_stability_component.yaml diff --git a/cmd/mdatagen/testdata/no_status.yaml b/cmd/mdatagen/internal/testdata/no_status.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_status.yaml rename to cmd/mdatagen/internal/testdata/no_status.yaml diff --git a/cmd/mdatagen/testdata/no_type.yaml b/cmd/mdatagen/internal/testdata/no_type.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_type.yaml rename to cmd/mdatagen/internal/testdata/no_type.yaml diff --git a/cmd/mdatagen/testdata/no_type_attr.yaml b/cmd/mdatagen/internal/testdata/no_type_attr.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_type_attr.yaml rename to cmd/mdatagen/internal/testdata/no_type_attr.yaml diff --git a/cmd/mdatagen/testdata/no_type_rattr.yaml b/cmd/mdatagen/internal/testdata/no_type_rattr.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_type_rattr.yaml rename to cmd/mdatagen/internal/testdata/no_type_rattr.yaml diff --git a/cmd/mdatagen/testdata/no_value_type.yaml b/cmd/mdatagen/internal/testdata/no_value_type.yaml similarity index 100% rename from cmd/mdatagen/testdata/no_value_type.yaml rename to cmd/mdatagen/internal/testdata/no_value_type.yaml diff --git a/cmd/mdatagen/testdata/parent.yaml b/cmd/mdatagen/internal/testdata/parent.yaml similarity index 100% rename from cmd/mdatagen/testdata/parent.yaml rename to cmd/mdatagen/internal/testdata/parent.yaml diff --git a/cmd/mdatagen/testdata/readme_with_cmd_class.md b/cmd/mdatagen/internal/testdata/readme_with_cmd_class.md similarity index 100% rename from cmd/mdatagen/testdata/readme_with_cmd_class.md rename to cmd/mdatagen/internal/testdata/readme_with_cmd_class.md diff --git a/cmd/mdatagen/testdata/readme_with_multiple_signals.md b/cmd/mdatagen/internal/testdata/readme_with_multiple_signals.md similarity index 100% rename from cmd/mdatagen/testdata/readme_with_multiple_signals.md rename to cmd/mdatagen/internal/testdata/readme_with_multiple_signals.md diff --git a/cmd/mdatagen/testdata/readme_with_status.md b/cmd/mdatagen/internal/testdata/readme_with_status.md similarity index 100% rename from cmd/mdatagen/testdata/readme_with_status.md rename to cmd/mdatagen/internal/testdata/readme_with_status.md diff --git a/cmd/mdatagen/testdata/readme_with_status_codeowners.md b/cmd/mdatagen/internal/testdata/readme_with_status_codeowners.md similarity index 100% rename from cmd/mdatagen/testdata/readme_with_status_codeowners.md rename to cmd/mdatagen/internal/testdata/readme_with_status_codeowners.md diff --git a/cmd/mdatagen/testdata/readme_with_status_codeowners_and_emeritus.md b/cmd/mdatagen/internal/testdata/readme_with_status_codeowners_and_emeritus.md similarity index 100% rename from cmd/mdatagen/testdata/readme_with_status_codeowners_and_emeritus.md rename to cmd/mdatagen/internal/testdata/readme_with_status_codeowners_and_emeritus.md diff --git a/cmd/mdatagen/testdata/readme_with_status_codeowners_and_seeking_new.md b/cmd/mdatagen/internal/testdata/readme_with_status_codeowners_and_seeking_new.md similarity index 100% rename from cmd/mdatagen/testdata/readme_with_status_codeowners_and_seeking_new.md rename to cmd/mdatagen/internal/testdata/readme_with_status_codeowners_and_seeking_new.md diff --git a/cmd/mdatagen/testdata/readme_with_status_extension.md b/cmd/mdatagen/internal/testdata/readme_with_status_extension.md similarity index 100% rename from cmd/mdatagen/testdata/readme_with_status_extension.md rename to cmd/mdatagen/internal/testdata/readme_with_status_extension.md diff --git a/cmd/mdatagen/testdata/readme_with_warnings.md b/cmd/mdatagen/internal/testdata/readme_with_warnings.md similarity index 100% rename from cmd/mdatagen/testdata/readme_with_warnings.md rename to cmd/mdatagen/internal/testdata/readme_with_warnings.md diff --git a/cmd/mdatagen/testdata/readme_without_status.md b/cmd/mdatagen/internal/testdata/readme_without_status.md similarity index 100% rename from cmd/mdatagen/testdata/readme_without_status.md rename to cmd/mdatagen/internal/testdata/readme_without_status.md diff --git a/cmd/mdatagen/testdata/resource_attributes_only.yaml b/cmd/mdatagen/internal/testdata/resource_attributes_only.yaml similarity index 100% rename from cmd/mdatagen/testdata/resource_attributes_only.yaml rename to cmd/mdatagen/internal/testdata/resource_attributes_only.yaml diff --git a/cmd/mdatagen/testdata/status_only.yaml b/cmd/mdatagen/internal/testdata/status_only.yaml similarity index 100% rename from cmd/mdatagen/testdata/status_only.yaml rename to cmd/mdatagen/internal/testdata/status_only.yaml diff --git a/cmd/mdatagen/testdata/two_metric_types.yaml b/cmd/mdatagen/internal/testdata/two_metric_types.yaml similarity index 100% rename from cmd/mdatagen/testdata/two_metric_types.yaml rename to cmd/mdatagen/internal/testdata/two_metric_types.yaml diff --git a/cmd/mdatagen/testdata/unknown_metric_attribute.yaml b/cmd/mdatagen/internal/testdata/unknown_metric_attribute.yaml similarity index 100% rename from cmd/mdatagen/testdata/unknown_metric_attribute.yaml rename to cmd/mdatagen/internal/testdata/unknown_metric_attribute.yaml diff --git a/cmd/mdatagen/testdata/unknown_value_type.yaml b/cmd/mdatagen/internal/testdata/unknown_value_type.yaml similarity index 100% rename from cmd/mdatagen/testdata/unknown_value_type.yaml rename to cmd/mdatagen/internal/testdata/unknown_value_type.yaml diff --git a/cmd/mdatagen/testdata/unused_attribute.yaml b/cmd/mdatagen/internal/testdata/unused_attribute.yaml similarity index 100% rename from cmd/mdatagen/testdata/unused_attribute.yaml rename to cmd/mdatagen/internal/testdata/unused_attribute.yaml diff --git a/cmd/mdatagen/testdata/with_goleak_ignores.yaml b/cmd/mdatagen/internal/testdata/with_goleak_ignores.yaml similarity index 100% rename from cmd/mdatagen/testdata/with_goleak_ignores.yaml rename to cmd/mdatagen/internal/testdata/with_goleak_ignores.yaml diff --git a/cmd/mdatagen/testdata/with_goleak_setup.yaml b/cmd/mdatagen/internal/testdata/with_goleak_setup.yaml similarity index 100% rename from cmd/mdatagen/testdata/with_goleak_setup.yaml rename to cmd/mdatagen/internal/testdata/with_goleak_setup.yaml diff --git a/cmd/mdatagen/testdata/with_goleak_skip.yaml b/cmd/mdatagen/internal/testdata/with_goleak_skip.yaml similarity index 100% rename from cmd/mdatagen/testdata/with_goleak_skip.yaml rename to cmd/mdatagen/internal/testdata/with_goleak_skip.yaml diff --git a/cmd/mdatagen/testdata/with_goleak_teardown.yaml b/cmd/mdatagen/internal/testdata/with_goleak_teardown.yaml similarity index 100% rename from cmd/mdatagen/testdata/with_goleak_teardown.yaml rename to cmd/mdatagen/internal/testdata/with_goleak_teardown.yaml diff --git a/cmd/mdatagen/testdata/with_telemetry.yaml b/cmd/mdatagen/internal/testdata/with_telemetry.yaml similarity index 100% rename from cmd/mdatagen/testdata/with_telemetry.yaml rename to cmd/mdatagen/internal/testdata/with_telemetry.yaml diff --git a/cmd/mdatagen/testdata/with_tests_connector.yaml b/cmd/mdatagen/internal/testdata/with_tests_connector.yaml similarity index 100% rename from cmd/mdatagen/testdata/with_tests_connector.yaml rename to cmd/mdatagen/internal/testdata/with_tests_connector.yaml diff --git a/cmd/mdatagen/testdata/with_tests_exporter.yaml b/cmd/mdatagen/internal/testdata/with_tests_exporter.yaml similarity index 100% rename from cmd/mdatagen/testdata/with_tests_exporter.yaml rename to cmd/mdatagen/internal/testdata/with_tests_exporter.yaml diff --git a/cmd/mdatagen/testdata/with_tests_extension.yaml b/cmd/mdatagen/internal/testdata/with_tests_extension.yaml similarity index 100% rename from cmd/mdatagen/testdata/with_tests_extension.yaml rename to cmd/mdatagen/internal/testdata/with_tests_extension.yaml diff --git a/cmd/mdatagen/testdata/with_tests_processor.yaml b/cmd/mdatagen/internal/testdata/with_tests_processor.yaml similarity index 100% rename from cmd/mdatagen/testdata/with_tests_processor.yaml rename to cmd/mdatagen/internal/testdata/with_tests_processor.yaml diff --git a/cmd/mdatagen/testdata/with_tests_receiver.yaml b/cmd/mdatagen/internal/testdata/with_tests_receiver.yaml similarity index 100% rename from cmd/mdatagen/testdata/with_tests_receiver.yaml rename to cmd/mdatagen/internal/testdata/with_tests_receiver.yaml diff --git a/cmd/mdatagen/validate.go b/cmd/mdatagen/internal/validate.go similarity index 88% rename from cmd/mdatagen/validate.go rename to cmd/mdatagen/internal/validate.go index 4685b1e2b1c..e28f629b7f6 100644 --- a/cmd/mdatagen/validate.go +++ b/cmd/mdatagen/internal/validate.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal" import ( "errors" @@ -11,7 +11,7 @@ import ( "go.opentelemetry.io/collector/pdata/pcommon" ) -func (md *metadata) Validate() error { +func (md *Metadata) Validate() error { var errs error if err := md.validateType(); err != nil { errs = errors.Join(errs, err) @@ -35,7 +35,7 @@ func (md *metadata) Validate() error { // This must be kept in sync with the regex in component/config.go. var typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]{0,62}$`) -func (md *metadata) validateType() error { +func (md *Metadata) validateType() error { if md.Type == "" { return errors.New("missing type") } @@ -51,7 +51,7 @@ func (md *metadata) validateType() error { return nil } -func (md *metadata) validateStatus() error { +func (md *Metadata) validateStatus() error { if md.Parent != "" && md.Status == nil { // status is not required for subcomponents. return nil @@ -112,7 +112,7 @@ func (s *Status) validateStability() error { return errs } -func (md *metadata) validateResourceAttributes() error { +func (md *Metadata) validateResourceAttributes() error { var errs error for name, attr := range md.ResourceAttributes { if attr.Description == "" { @@ -126,16 +126,16 @@ func (md *metadata) validateResourceAttributes() error { return errs } -func (md *metadata) validateMetrics() error { +func (md *Metadata) validateMetrics() error { var errs error - usedAttrs := map[attributeName]bool{} + usedAttrs := map[AttributeName]bool{} errs = errors.Join(errs, validateMetrics(md.Metrics, md.Attributes, usedAttrs), validateMetrics(md.Telemetry.Metrics, md.Attributes, usedAttrs), md.validateAttributes(usedAttrs)) return errs } -func (m *metric) validate() error { +func (m *Metric) validate() error { var errs error if m.Description == "" { errs = errors.Join(errs, errors.New(`missing metric description`)) @@ -159,9 +159,9 @@ func (mit MetricInputType) Validate() error { return nil } -func (md *metadata) validateAttributes(usedAttrs map[attributeName]bool) error { +func (md *Metadata) validateAttributes(usedAttrs map[AttributeName]bool) error { var errs error - unusedAttrs := make([]attributeName, 0, len(md.Attributes)) + unusedAttrs := make([]AttributeName, 0, len(md.Attributes)) for attrName, attr := range md.Attributes { if attr.Description == "" { errs = errors.Join(errs, fmt.Errorf(`missing attribute description for: %v`, attrName)) @@ -180,7 +180,7 @@ func (md *metadata) validateAttributes(usedAttrs map[attributeName]bool) error { return errs } -func validateMetrics(metrics map[metricName]metric, attributes map[attributeName]attribute, usedAttrs map[attributeName]bool) error { +func validateMetrics(metrics map[MetricName]Metric, attributes map[AttributeName]Attribute, usedAttrs map[AttributeName]bool) error { var errs error for mn, m := range metrics { if m.Sum == nil && m.Gauge == nil && m.Histogram == nil { @@ -197,7 +197,7 @@ func validateMetrics(metrics map[metricName]metric, attributes map[attributeName errs = errors.Join(errs, fmt.Errorf(`metric "%v": %w`, mn, err)) continue } - unknownAttrs := make([]attributeName, 0, len(m.Attributes)) + unknownAttrs := make([]AttributeName, 0, len(m.Attributes)) for _, attr := range m.Attributes { if _, ok := attributes[attr]; ok { usedAttrs[attr] = true diff --git a/cmd/mdatagen/validate_test.go b/cmd/mdatagen/internal/validate_test.go similarity index 95% rename from cmd/mdatagen/validate_test.go rename to cmd/mdatagen/internal/validate_test.go index fcc62428648..4c71c272338 100644 --- a/cmd/mdatagen/validate_test.go +++ b/cmd/mdatagen/internal/validate_test.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package main +package internal import ( "fmt" @@ -99,7 +99,7 @@ func TestValidate(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := loadMetadata(tt.name) + _, err := LoadMetadata(tt.name) require.Error(t, err) require.EqualError(t, err, tt.wantErr) }) @@ -113,9 +113,9 @@ func TestValidateMetricDuplicates(t *testing.T) { "container.uptime": {"docker_stats", "kubeletstats"}, } allMetrics := map[string][]string{} - err := filepath.Walk("../../receiver", func(path string, info fs.FileInfo, _ error) error { + err := filepath.Walk("../../../receiver", func(path string, info fs.FileInfo, _ error) error { if info.Name() == "metadata.yaml" { - md, err := loadMetadata(path) + md, err := LoadMetadata(path) require.NoError(t, err) if len(md.Metrics) > 0 { for metricName := range md.Metrics { diff --git a/cmd/mdatagen/main.go b/cmd/mdatagen/main.go index 66f97ae54da..0f27b925fce 100644 --- a/cmd/mdatagen/main.go +++ b/cmd/mdatagen/main.go @@ -20,6 +20,8 @@ import ( "golang.org/x/text/cases" "golang.org/x/text/language" + + "go.opentelemetry.io/collector/cmd/mdatagen/internal" ) const ( @@ -51,7 +53,7 @@ func run(ymlPath string) error { ymlDir := filepath.Dir(ymlPath) packageName := filepath.Base(ymlDir) - md, err := loadMetadata(ymlPath) + md, err := internal.LoadMetadata(ymlPath) if err != nil { return fmt.Errorf("failed loading %v: %w", ymlPath, err) } @@ -143,25 +145,25 @@ func run(ymlPath string) error { return nil } -func templatize(tmplFile string, md metadata) *template.Template { +func templatize(tmplFile string, md internal.Metadata) *template.Template { return template.Must( template. New(filepath.Base(tmplFile)). Option("missingkey=error"). Funcs(map[string]any{ "publicVar": func(s string) (string, error) { - return formatIdentifier(s, true) + return internal.FormatIdentifier(s, true) }, - "attributeInfo": func(an attributeName) attribute { + "attributeInfo": func(an internal.AttributeName) internal.Attribute { return md.Attributes[an] }, - "metricInfo": func(mn metricName) metric { + "metricInfo": func(mn internal.MetricName) internal.Metric { return md.Metrics[mn] }, - "telemetryInfo": func(mn metricName) metric { + "telemetryInfo": func(mn internal.MetricName) internal.Metric { return md.Telemetry.Metrics[mn] }, - "parseImportsRequired": func(metrics map[metricName]metric) bool { + "parseImportsRequired": func(metrics map[internal.MetricName]internal.Metric) bool { for _, m := range metrics { if m.Data().HasMetricInputType() { return true @@ -195,7 +197,7 @@ func templatize(tmplFile string, md metadata) *template.Template { }, "inc": func(i int) int { return i + 1 }, "distroURL": func(name string) string { - return distros[name] + return internal.Distros[name] }, "isExporter": func() bool { return md.Status.Class == "exporter" @@ -342,10 +344,10 @@ func templatize(tmplFile string, md metadata) *template.Template { // which uses the `\` as a special character. // Meaning on windows based machines, the `\` needs to be replaced // with a `/` for it to find the file. - }).ParseFS(templateFS, strings.ReplaceAll(tmplFile, "\\", "/"))) + }).ParseFS(internal.TemplateFS, strings.ReplaceAll(tmplFile, "\\", "/"))) } -func inlineReplace(tmplFile string, outputFile string, md metadata, start string, end string) error { +func inlineReplace(tmplFile string, outputFile string, md internal.Metadata, start string, end string) error { var readmeContents []byte var err error if readmeContents, err = os.ReadFile(outputFile); err != nil { // nolint: gosec @@ -364,7 +366,7 @@ func inlineReplace(tmplFile string, outputFile string, md metadata, start string md.GithubProject = "open-telemetry/opentelemetry-collector-contrib" } - if err := tmpl.Execute(&buf, templateContext{metadata: md, Package: "metadata"}); err != nil { + if err := tmpl.Execute(&buf, internal.TemplateContext{Metadata: md, Package: "metadata"}); err != nil { return fmt.Errorf("failed executing template: %w", err) } @@ -378,11 +380,11 @@ func inlineReplace(tmplFile string, outputFile string, md metadata, start string return nil } -func generateFile(tmplFile string, outputFile string, md metadata, goPackage string) error { +func generateFile(tmplFile string, outputFile string, md internal.Metadata, goPackage string) error { tmpl := templatize(tmplFile, md) buf := bytes.Buffer{} - if err := tmpl.Execute(&buf, templateContext{metadata: md, Package: goPackage}); err != nil { + if err := tmpl.Execute(&buf, internal.TemplateContext{Metadata: md, Package: goPackage}); err != nil { return fmt.Errorf("failed executing template: %w", err) } diff --git a/cmd/mdatagen/main_test.go b/cmd/mdatagen/main_test.go index aec9ec132dc..dfdcabab7fe 100644 --- a/cmd/mdatagen/main_test.go +++ b/cmd/mdatagen/main_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/cmd/mdatagen/internal" "go.opentelemetry.io/collector/component" ) @@ -111,7 +112,7 @@ func TestRunContents(t *testing.T) { tmpdir := filepath.Join(t.TempDir(), "shortname") err := os.MkdirAll(tmpdir, 0750) require.NoError(t, err) - ymlContent, err := os.ReadFile(filepath.Join("testdata", tt.yml)) + ymlContent, err := os.ReadFile(filepath.Join("internal/testdata", tt.yml)) require.NoError(t, err) metadataFile := filepath.Join(tmpdir, "metadata.yaml") require.NoError(t, os.WriteFile(metadataFile, ymlContent, 0600)) @@ -265,7 +266,7 @@ func TestInlineReplace(t *testing.T) { warnings []string stability map[component.StabilityLevel][]string distros []string - codeowners *Codeowners + codeowners *internal.Codeowners githubProject string }{ { @@ -307,7 +308,7 @@ Some info about a component outputFile: "readme_with_status_codeowners_and_seeking_new.md", componentClass: "receiver", distros: []string{"contrib"}, - codeowners: &Codeowners{ + codeowners: &internal.Codeowners{ Active: []string{"foo"}, SeekingNew: true, }, @@ -324,7 +325,7 @@ Some info about a component outputFile: "readme_with_status_codeowners_and_emeritus.md", componentClass: "receiver", distros: []string{"contrib"}, - codeowners: &Codeowners{ + codeowners: &internal.Codeowners{ Active: []string{"foo"}, Emeritus: []string{"bar"}, }, @@ -341,7 +342,7 @@ Some info about a component outputFile: "readme_with_status_codeowners.md", componentClass: "receiver", distros: []string{"contrib"}, - codeowners: &Codeowners{ + codeowners: &internal.Codeowners{ Active: []string{"foo"}, }, }, @@ -425,11 +426,11 @@ Some info about a component if len(tt.stability) > 0 { stability = tt.stability } - md := metadata{ + md := internal.Metadata{ GithubProject: tt.githubProject, Type: "foo", ShortFolderName: "foo", - Status: &Status{ + Status: &internal.Status{ Stability: stability, Distributions: tt.distros, Class: tt.componentClass, @@ -449,7 +450,7 @@ Some info about a component got, err := os.ReadFile(filepath.Join(tmpdir, "README.md")) // nolint: gosec require.NoError(t, err) got = bytes.ReplaceAll(got, []byte("\r\n"), []byte("\n")) - expected, err := os.ReadFile(filepath.Join("testdata", tt.outputFile)) + expected, err := os.ReadFile(filepath.Join("internal/testdata", tt.outputFile)) require.NoError(t, err) expected = bytes.ReplaceAll(expected, []byte("\r\n"), []byte("\n")) fmt.Println(string(got)) @@ -463,14 +464,14 @@ func TestGenerateStatusMetadata(t *testing.T) { tests := []struct { name string output string - md metadata + md internal.Metadata expected string }{ { name: "foo component with beta status", - md: metadata{ + md: internal.Metadata{ Type: "foo", - Status: &Status{ + Status: &internal.Status{ Stability: map[component.StabilityLevel][]string{ component.StabilityLevelBeta: {"metrics"}, }, @@ -498,9 +499,9 @@ const ( }, { name: "foo component with alpha status", - md: metadata{ + md: internal.Metadata{ Type: "foo", - Status: &Status{ + Status: &internal.Status{ Stability: map[component.StabilityLevel][]string{ component.StabilityLevelAlpha: {"metrics"}, }, @@ -545,14 +546,14 @@ func TestGenerateTelemetryMetadata(t *testing.T) { tests := []struct { name string output string - md metadata + md internal.Metadata expected string }{ { name: "foo component with beta status", - md: metadata{ + md: internal.Metadata{ Type: "foo", - Status: &Status{ + Status: &internal.Status{ Stability: map[component.StabilityLevel][]string{ component.StabilityLevelBeta: {"metrics"}, }, @@ -589,9 +590,9 @@ func Tracer(settings component.TelemetrySettings) trace.Tracer { }, { name: "foo component with alpha status", - md: metadata{ + md: internal.Metadata{ Type: "foo", - Status: &Status{ + Status: &internal.Status{ Stability: map[component.StabilityLevel][]string{ component.StabilityLevelAlpha: {"metrics"}, },