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

Auto Generation of Component Configuration from metadata.yaml #27003

Closed
wants to merge 5 commits into from
Closed
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
121 changes: 121 additions & 0 deletions cmd/mdatagen/configgen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package main

import (
"bytes"
"encoding/json"
"fmt"
"os"
"text/template"

"github.com/atombender/go-jsonschema/pkg/schemas"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/mdatagen/third_party/gojsonschemagenerator"
)

const (
CONFIG_NAME = "config"
)

func GenerateConfig(conf any) error {
// load config
jsonBytes, err := json.Marshal(conf)
if err != nil {
return fmt.Errorf("failed loading config %w", err)
}
var schema schemas.Schema
if err := json.Unmarshal(jsonBytes, &schema); err != nil {
return fmt.Errorf("failed to unmarshal JSON: %w", err)
}

// todo: don't hardcode
pkgName := "fileexporter"

// init generator
cfg := gojsonschemagenerator.Config{
Warner: func(message string) {
logf("Warning: %s", message)
},
DefaultPackageName: pkgName,
DefaultOutputName: "config",
StructNameFromTitle: true,
Tags: []string{"json", "yaml", "mapstructure"},
SchemaMappings: []gojsonschemagenerator.SchemaMapping{},
YAMLPackage: "gopkg.in/yaml.v3",
YAMLExtensions: []string{".yaml", ".yml"},
}

generator, err := gojsonschemagenerator.New(cfg)
if err != nil {
return fmt.Errorf("failed to create generator: %w", err)
}
if err = generator.AddFile(CONFIG_NAME, &schema); err != nil {
return fmt.Errorf("failed to add config: %w", err)
}

hasUnsupportedValidations := len(generator.NotSupportedValidations) > 0

tplVars := struct {
ValidatorFuncName string
}{
ValidatorFuncName: "Validate",
}
if hasUnsupportedValidations {
tplVars.ValidatorFuncName = "ValidateHelper"
}

tpl := `
func (cfg *Config){{.ValidatorFuncName}}() error {
b, err := json.Marshal(cfg)
if err != nil {
return err
}
var config Config
if err := json.Unmarshal(b, &config); err != nil {
return err
}
return nil
}`
tmpl, err := template.New("validator").Parse(tpl)
if err != nil {
return fmt.Errorf("failed to parse template: %w", err)
}

for _, source := range generator.Sources() {
file, err := os.Create("/tmp/config.go")
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()

buf := bytes.NewBufferString("")
if err = tmpl.Execute(buf, tplVars); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
// only write custom validation if there are no unsupported validations
// source = append(source, []byte(tpl)...)
source = append(source, buf.Bytes()...)
_, err = file.Write(source)

if err != nil {
return fmt.Errorf("failed writing file: %w", err)
}
}
fmt.Println("done")
return nil
}

func logf(format string, args ...interface{}) {
fmt.Fprint(os.Stderr, "go-jsonschema: ")
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprint(os.Stderr, "\n")
}

func dump(v any) {
jsonBytes, err := json.Marshal(v)
if err != nil {
panic(err)
}
fmt.Println(string(jsonBytes))
}
10 changes: 10 additions & 0 deletions cmd/mdatagen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/cmd/mdatagen
go 1.20

require (
github.com/atombender/go-jsonschema v0.12.1
github.com/google/go-cmp v0.5.9
github.com/pkg/errors v0.9.1
github.com/sanity-io/litter v1.5.5
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/component v0.85.0
go.opentelemetry.io/collector/confmap v0.85.0
Expand All @@ -17,12 +20,17 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/goccy/go-yaml v1.11.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/knadh/koanf/v2 v2.0.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -34,8 +42,10 @@ require (
go.opentelemetry.io/otel v1.18.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.opentelemetry.io/otel/trace v1.18.0 // indirect
golang.org/x/exp v0.0.0-20230711023510-fffb14384f22 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
29 changes: 29 additions & 0 deletions cmd/mdatagen/go.sum

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

2 changes: 2 additions & 0 deletions cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ type metadata struct {
ScopeName string `mapstructure:"-"`
// ShortFolderName is the shortened folder name of the component, removing class if present
ShortFolderName string `mapstructure:"-"`
// Config is the component configuration.
Config any `mapstructure:"config"`
}

func setAttributesFullName(attrs map[attributeName]attribute) {
Expand Down
18 changes: 16 additions & 2 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ const (
)

func main() {
genConfig := flag.Bool("genConfig", false, "generate config files")
flag.Parse()
yml := flag.Arg(0)
if err := run(yml); err != nil {
if err := run(yml, *genConfig); err != nil {
log.Fatal(err)
}
}

func run(ymlPath string) error {
func run(ymlPath string, genConfig bool) error {

if ymlPath == "" {
return errors.New("argument must be metadata.yaml file")
}
Expand All @@ -49,6 +51,18 @@ func run(ymlPath string) error {
return fmt.Errorf("failed loading %v: %w", ymlPath, err)
}

// generate configuration
if genConfig {
if md.Config == nil {
return fmt.Errorf("no config found in %v", ymlPath)
}
err := GenerateConfig(md.Config)
if err != nil {
return fmt.Errorf("error generating config %w", err)
}
os.Exit(0)
}

tmplDir := "templates"

codeDir := filepath.Join(ymlDir, "internal", "metadata")
Expand Down
Loading
Loading