Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mdatagen: get package name from metadata #11468

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/mdatagen_fix_package_name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix the custom package name not being fully used for all templating.

# One or more tracking issues or pull requests related to the change
issues: [11469]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,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 @@ -398,7 +398,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
Loading