Skip to content

Commit

Permalink
mdatagen: get package name from metadata
Browse files Browse the repository at this point in the history
I noticed a mistake in my previous PR #11232; some function calls did
not pass the correct package name in (passing in "metadata" instead of
the intended generated package name). This PR attempts to address the
potential for this mistake to even occur by providing a wrapper
`generateFile` function that automatically uses the generated package
name from the metadata. The original version of the function that
accepts a package name is intact for the templates that are going in the
base package instead of the generated one.
  • Loading branch information
braydonk committed Oct 16, 2024
1 parent 388bae7 commit a7da76a
Show file tree
Hide file tree
Showing 23 changed files with 2,301 additions and 13 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,4 @@ mdatagen-test:
cd cmd/mdatagen && $(GOCMD) install .
cd cmd/mdatagen && $(GOCMD) generate ./...
cd cmd/mdatagen && $(GOCMD) test ./...
$(MAKE) -C cmd/mdatagen fmt
18 changes: 11 additions & 7 deletions cmd/mdatagen/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ func run(ymlPath string) error {
if md.Status != nil {
if md.Status.Class != "cmd" && md.Status.Class != "pkg" && !md.Status.NotComponent {
if err = generateFile(filepath.Join(tmplDir, "status.go.tmpl"),
filepath.Join(codeDir, "generated_status.go"), md, "metadata"); err != nil {
filepath.Join(codeDir, "generated_status.go"), md); err != nil {
return err
}
if err = generateFile(filepath.Join(tmplDir, "component_test.go.tmpl"),
if err = generateFileWithPackageName(filepath.Join(tmplDir, "component_test.go.tmpl"),
filepath.Join(ymlDir, "generated_component_test.go"), md, packageName); err != nil {
return err
}
}

if err = generateFile(filepath.Join(tmplDir, "package_test.go.tmpl"),
if err = generateFileWithPackageName(filepath.Join(tmplDir, "package_test.go.tmpl"),
filepath.Join(ymlDir, "generated_package_test.go"), md, packageName); err != nil {
return err
}
Expand All @@ -108,7 +108,7 @@ func run(ymlPath string) error {
toGenerate := map[string]string{}

if len(md.Telemetry.Metrics) != 0 { // if there are telemetry metrics, generate telemetry specific files
if err = generateFile(filepath.Join(tmplDir, "component_telemetry_test.go.tmpl"),
if err = generateFileWithPackageName(filepath.Join(tmplDir, "component_telemetry_test.go.tmpl"),
filepath.Join(ymlDir, "generated_component_telemetry_test.go"), md, packageName); err != nil {
return err
}
Expand All @@ -121,7 +121,7 @@ func run(ymlPath string) error {
}

for tmpl, dst := range toGenerate {
if err = generateFile(tmpl, dst, md, "metadata"); err != nil {
if err = generateFile(tmpl, dst, md); err != nil {
return err
}
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func run(ymlPath string) error {
}

for tmpl, dst := range toGenerate {
if err = generateFile(tmpl, dst, md, md.GeneratedPackageName); err != nil {
if err = generateFile(tmpl, dst, md); err != nil {
return err
}
}
Expand Down Expand Up @@ -400,7 +400,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 Metadata) error {
return generateFileWithPackageName(tmplFile, outputFile, md, md.GeneratedPackageName)
}

func generateFileWithPackageName(tmplFile string, outputFile string, md Metadata, goPackage string) error {
if err := os.Remove(outputFile); err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("unable to remove generated file %q: %w", outputFile, err)
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/mdatagen/internal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ func TestGenerateStatusMetadata(t *testing.T) {
{
name: "foo component with beta status",
md: Metadata{
Type: "foo",
Type: "foo",
GeneratedPackageName: "metadata",
Status: &Status{
Stability: map[component.StabilityLevel][]string{
component.StabilityLevelBeta: {"metrics"},
Expand Down Expand Up @@ -531,7 +532,8 @@ const (
{
name: "foo component with alpha status",
md: Metadata{
Type: "foo",
Type: "foo",
GeneratedPackageName: "metadata",
Status: &Status{
Stability: map[component.StabilityLevel][]string{
component.StabilityLevelAlpha: {"metrics"},
Expand Down Expand Up @@ -564,7 +566,7 @@ const (
t.Run(tt.name, func(t *testing.T) {
tmpdir := t.TempDir()
err := generateFile("templates/status.go.tmpl",
filepath.Join(tmpdir, "generated_status.go"), tt.md, "metadata")
filepath.Join(tmpdir, "generated_status.go"), tt.md)
require.NoError(t, err)
actual, err := os.ReadFile(filepath.Join(tmpdir, "generated_status.go")) // nolint: gosec
require.NoError(t, err)
Expand All @@ -583,7 +585,8 @@ func TestGenerateTelemetryMetadata(t *testing.T) {
{
name: "foo component with beta status",
md: Metadata{
Type: "foo",
Type: "foo",
GeneratedPackageName: "metadata",
Status: &Status{
Stability: map[component.StabilityLevel][]string{
component.StabilityLevelBeta: {"metrics"},
Expand Down Expand Up @@ -621,7 +624,8 @@ func Tracer(settings component.TelemetrySettings) trace.Tracer {
{
name: "foo component with alpha status",
md: Metadata{
Type: "foo",
Type: "foo",
GeneratedPackageName: "metadata",
Status: &Status{
Stability: map[component.StabilityLevel][]string{
component.StabilityLevelAlpha: {"metrics"},
Expand Down Expand Up @@ -662,7 +666,7 @@ func Tracer(settings component.TelemetrySettings) trace.Tracer {
t.Run(tt.name, func(t *testing.T) {
tmpdir := t.TempDir()
err := generateFile("templates/telemetry.go.tmpl",
filepath.Join(tmpdir, "generated_telemetry.go"), tt.md, "metadata")
filepath.Join(tmpdir, "generated_telemetry.go"), tt.md)
require.NoError(t, err)
actual, err := os.ReadFile(filepath.Join(tmpdir, "generated_telemetry.go")) // nolint: gosec
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions cmd/mdatagen/internal/sampleprocessor/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// Generate a test metrics builder from a sample metrics set covering all configuration options.
//go:generate mdatagen metadata.yaml
//go:generate mdatagen metadata_custom.yaml

// Deprecated: This package is moving to https://github.com/open-telemetry/opentelemetry-collector and will eventually be removed.
// Please see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/30497
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a7da76a

Please sign in to comment.