diff --git a/.chloggen/builder-confmap-converters.yaml b/.chloggen/builder-confmap-converters.yaml new file mode 100644 index 00000000000..aed5da166ea --- /dev/null +++ b/.chloggen/builder-confmap-converters.yaml @@ -0,0 +1,36 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: cmd/builder + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Allow configuring `confmap.Converter` components in ocb. + +# One or more tracking issues or pull requests related to the change +issues: [11582] + +# (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: | + If no converters are specified, there will be no converters added. + Currently, the only published converter is `expandconverter` which is + deprecated as of v0.107.0, but can still be added for testing purposes. + + To configure a custom converter, make sure your converter implements the converter + interface and is published as a go module (or replaced locally if not published). + You may then use the `converters` key in your OCB build manifest with a list of + Go modules (and replaces as necessary) to include your converter. + + Please note that converters are order-dependent. The confmap will apply converters + in order of which they are listed in your manifest if there is more than one. + +# 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: [user] diff --git a/cmd/builder/internal/builder/config.go b/cmd/builder/internal/builder/config.go index c6de745c371..427920c84e8 100644 --- a/cmd/builder/internal/builder/config.go +++ b/cmd/builder/internal/builder/config.go @@ -43,6 +43,7 @@ type Config struct { Processors []Module `mapstructure:"processors"` Connectors []Module `mapstructure:"connectors"` Providers []Module `mapstructure:"providers"` + Converters []Module `mapstructure:"converters"` Replaces []string `mapstructure:"replaces"` Excludes []string `mapstructure:"excludes"` @@ -142,6 +143,7 @@ func (c *Config) Validate() error { validateModules("processor", c.Processors), validateModules("connector", c.Connectors), validateModules("provider", c.Providers), + validateModules("converter", c.Converters), ) } @@ -194,7 +196,10 @@ func (c *Config) ParseModules() error { if err != nil { return err } - + c.Converters, err = parseModules(c.Converters) + if err != nil { + return err + } return nil } @@ -226,6 +231,10 @@ func parseModules(mods []Module) ([]Module, error) { if err != nil { return mods, fmt.Errorf("module has a relative \"path\" element, but we couldn't resolve the current working dir: %w", err) } + // Check if the path exists + if _, err := os.Stat(mod.Path); os.IsNotExist(err) { + return mods, fmt.Errorf("filepath does not exist: %s", mod.Path) + } } parsedModules = append(parsedModules, mod) diff --git a/cmd/builder/internal/builder/config_test.go b/cmd/builder/internal/builder/config_test.go index 62123dc2afe..f27da4df5dc 100644 --- a/cmd/builder/internal/builder/config_test.go +++ b/cmd/builder/internal/builder/config_test.go @@ -36,12 +36,27 @@ func TestParseModules(t *testing.T) { assert.Equal(t, "repo", cfg.Extensions[0].Name) } +func TestInvalidConverter(t *testing.T) { + // Create a Config instance with invalid Converters + config := &Config{ + Converters: []Module{ + { + Path: "./invalid/module/path", // Invalid module path to trigger an error + }, + }, + } + + // Call the method and expect an error + err := config.ParseModules() + require.Error(t, err, "expected an error when parsing invalid modules") +} + func TestRelativePath(t *testing.T) { // prepare cfg := Config{ Extensions: []Module{{ GoMod: "some-module", - Path: "./some-module", + Path: "./templates", }}, } @@ -116,7 +131,7 @@ func TestMissingModule(t *testing.T) { cfg: Config{ Logger: zap.NewNop(), Exporters: []Module{{ - Import: "invali", + Import: "invalid", }}, }, err: errMissingGoMod, @@ -139,6 +154,15 @@ func TestMissingModule(t *testing.T) { }, err: errMissingGoMod, }, + { + cfg: Config{ + Logger: zap.NewNop(), + Converters: []Module{{ + Import: "invalid", + }}, + }, + err: errMissingGoMod, + }, } for _, test := range configurations { @@ -235,6 +259,7 @@ func TestSkipsNilFieldValidation(t *testing.T) { cfg, err := NewDefaultConfig() require.NoError(t, err) cfg.Providers = nil + cfg.Converters = nil assert.NoError(t, cfg.Validate()) } diff --git a/cmd/builder/internal/builder/main.go b/cmd/builder/internal/builder/main.go index d811381f74f..3bd8285cb44 100644 --- a/cmd/builder/internal/builder/main.go +++ b/cmd/builder/internal/builder/main.go @@ -213,7 +213,7 @@ func processAndWrite(cfg *Config, tmpl *template.Template, outFile string, tmplP } func (c *Config) allComponents() []Module { - return slices.Concat[[]Module](c.Exporters, c.Receivers, c.Processors, c.Extensions, c.Connectors, c.Providers) + return slices.Concat[[]Module](c.Exporters, c.Receivers, c.Processors, c.Extensions, c.Connectors, c.Providers, c.Converters) } func (c *Config) readGoModFile() (string, map[string]string, error) { diff --git a/cmd/builder/internal/builder/templates/go.mod.tmpl b/cmd/builder/internal/builder/templates/go.mod.tmpl index 7f0bb56725d..cec15fe06a4 100644 --- a/cmd/builder/internal/builder/templates/go.mod.tmpl +++ b/cmd/builder/internal/builder/templates/go.mod.tmpl @@ -8,6 +8,9 @@ require ( {{- range .Providers}} {{if .GoMod}}{{.GoMod}}{{end}} {{- end}} + {{- range .Converters}} + {{if .GoMod}}{{.GoMod}}{{end}} + {{- end}} {{- range .Connectors}} {{if .GoMod}}{{.GoMod}}{{end}} {{- end}} diff --git a/cmd/builder/internal/builder/templates/main.go.tmpl b/cmd/builder/internal/builder/templates/main.go.tmpl index bff3d5785de..61bf580ddf8 100644 --- a/cmd/builder/internal/builder/templates/main.go.tmpl +++ b/cmd/builder/internal/builder/templates/main.go.tmpl @@ -8,6 +8,9 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/confmap" + {{- range .Converters}} + {{.Name}} "{{.Import}}" + {{- end}} {{- range .Providers}} {{.Name}} "{{.Import}}" {{- end}} @@ -31,6 +34,13 @@ func main() { {{.Name}}.NewFactory(), {{- end}} }, + {{- if .Converters }} + ConverterFactories: []confmap.ConverterFactory{ + {{- range .Converters}} + {{.Name}}.NewFactory(), + {{- end}} + }, + {{- end }} {{- if .ConfResolver.DefaultURIScheme }} DefaultScheme: "{{ .ConfResolver.DefaultURIScheme }}", {{- end }}