Skip to content

Commit

Permalink
[cmd/mdatagen] Remove the old metadata generator (open-telemetry#13374)
Browse files Browse the repository at this point in the history
All receivers were migrated to the new metrics builder. We can remove the old mdatagen tool and declare the new metrics builder as stable.
  • Loading branch information
dmitryax authored Aug 15, 2022
1 parent 504e59f commit 9b6a513
Show file tree
Hide file tree
Showing 81 changed files with 326 additions and 499 deletions.
3 changes: 1 addition & 2 deletions cmd/mdatagen/documentation.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ These are the metrics available for this scraper.
{{- range $index, $attributeName := $metricInfo.Attributes }} <li>{{ $attributeName }}</li> {{- end }} </ul> |
{{- end }}

**Highlighted metrics** are emitted by default.{{ if .ExpGen }} Other metrics are optional and not emitted by default.
**Highlighted metrics** are emitted by default. Other metrics are optional and not emitted by default.
Any metric can be enabled or disabled with the following scraper configuration:

```yaml
metrics:
<metric_name>:
enabled: <true|false>
```
{{- end }}

{{- if .ResourceAttributes }}

Expand Down
3 changes: 0 additions & 3 deletions cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,6 @@ type templateContext struct {
metadata
// Package name for generated code.
Package string
// ExpGen identifies whether the experimental metrics generator is used.
// TODO: Remove once the old mdata generator is gone.
ExpGen bool
}

func loadMetadata(filePath string) (metadata, error) {
Expand Down
36 changes: 12 additions & 24 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,19 @@ import (
)

const (
tmplFileV1 = "metrics.tmpl"
outputFileV1 = "generated_metrics.go"
tmplFileV2 = "metrics_v2.tmpl"
outputFileV2 = "generated_metrics_v2.go"
tmplFile = "metrics.tmpl"
outputFile = "generated_metrics.go"
)

func main() {
useExpGen := flag.Bool("experimental-gen", false, "Use experimental generator")
flag.Parse()
yml := flag.Arg(0)
if err := run(yml, *useExpGen); err != nil {
if err := run(yml); err != nil {
log.Fatal(err)
}
}

func run(ymlPath string, useExpGen bool) error {
func run(ymlPath string) error {
if ymlPath == "" {
return errors.New("argument must be metadata.yaml file")
}
Expand All @@ -62,20 +59,13 @@ func run(ymlPath string, useExpGen bool) error {
}
thisDir := filepath.Dir(filename)

if err = generateMetrics(ymlDir, thisDir, md, useExpGen); err != nil {
if err = generateMetrics(ymlDir, thisDir, md); err != nil {
return err
}
return generateDocumentation(ymlDir, thisDir, md, useExpGen)
return generateDocumentation(ymlDir, thisDir, md)
}

func generateMetrics(ymlDir string, thisDir string, md metadata, useExpGen bool) error {
tmplFile := tmplFileV1
outputFile := outputFileV1
if useExpGen {
tmplFile = tmplFileV2
outputFile = outputFileV2
}

func generateMetrics(ymlDir string, thisDir string, md metadata) error {
tmpl := template.Must(
template.
New(tmplFile).
Expand Down Expand Up @@ -123,20 +113,18 @@ func generateMetrics(ymlDir string, thisDir string, md metadata, useExpGen bool)
if err := os.MkdirAll(outputDir, 0700); err != nil {
return fmt.Errorf("unable to create output directory %q: %w", outputDir, err)
}
for _, f := range []string{filepath.Join(outputDir, outputFileV1), filepath.Join(outputDir, outputFileV2)} {
if err := os.Remove(f); err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("unable to remove genererated file %q: %w", f, err)
}
}
outputFilepath := filepath.Join(outputDir, outputFile)
if err := os.Remove(outputFilepath); err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("unable to remove genererated file %q: %w", outputFilepath, err)
}
if err := os.WriteFile(outputFilepath, formatted, 0600); err != nil {
return fmt.Errorf("failed writing %q: %w", outputFilepath, err)
}

return nil
}

func generateDocumentation(ymlDir string, thisDir string, md metadata, useExpGen bool) error {
func generateDocumentation(ymlDir string, thisDir string, md metadata) error {
tmpl := template.Must(
template.
New("documentation.tmpl").
Expand All @@ -150,7 +138,7 @@ func generateDocumentation(ymlDir string, thisDir string, md metadata, useExpGen

buf := bytes.Buffer{}

tmplCtx := templateContext{metadata: md, ExpGen: useExpGen, Package: "metadata"}
tmplCtx := templateContext{metadata: md, Package: "metadata"}
if err := tmpl.Execute(&buf, tmplCtx); err != nil {
return fmt.Errorf("failed executing template: %w", err)
}
Expand Down
25 changes: 7 additions & 18 deletions cmd/mdatagen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ metrics:

func Test_runContents(t *testing.T) {
type args struct {
yml string
useExpGen bool
yml string
}
tests := []struct {
name string
Expand All @@ -62,19 +61,13 @@ func Test_runContents(t *testing.T) {
}{
{
name: "valid metadata",
args: args{validMetadata, false},
expectedDocumentation: "testdata/documentation_v1.md",
want: "",
},
{
name: "valid metadata v2",
args: args{validMetadata, true},
expectedDocumentation: "testdata/documentation_v2.md",
args: args{validMetadata},
expectedDocumentation: "testdata/documentation.md",
want: "",
},
{
name: "invalid yaml",
args: args{"invalid", false},
args: args{"invalid"},
want: "",
wantErr: true,
},
Expand All @@ -86,18 +79,14 @@ func Test_runContents(t *testing.T) {
metadataFile := filepath.Join(tmpdir, "metadata.yaml")
require.NoError(t, os.WriteFile(metadataFile, []byte(tt.args.yml), 0600))

err := run(metadataFile, tt.args.useExpGen)
err := run(metadataFile)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)

genFilePath := filepath.Join(tmpdir, "internal/metadata/generated_metrics.go")
if tt.args.useExpGen {
genFilePath = filepath.Join(tmpdir, "internal/metadata/generated_metrics_v2.go")
}
require.FileExists(t, genFilePath)
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_metrics.go"))

actualDocumentation := filepath.Join(tmpdir, "documentation.md")
require.FileExists(t, actualDocumentation)
Expand Down Expand Up @@ -136,7 +125,7 @@ func Test_run(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := run(tt.args.ymlPath, false); (err != nil) != tt.wantErr {
if err := run(tt.args.ymlPath); (err != nil) != tt.wantErr {
t.Errorf("run() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/mdatagen/metric-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ attributes:
# being described below.
metrics:
<metric.name>:
# Required: whether the metric is collected by default. Takes effect only with "--experimental-gen" mdatagen flag.
# Required: whether the metric is collected by default.
enabled: # true | false
# Required: metric description.
description:
Expand Down
Loading

0 comments on commit 9b6a513

Please sign in to comment.