diff --git a/.chloggen/msg_fix-embed-metadatagen-templates.yaml b/.chloggen/msg_fix-embed-metadatagen-templates.yaml new file mode 100755 index 000000000000..d1aad32e97b5 --- /dev/null +++ b/.chloggen/msg_fix-embed-metadatagen-templates.yaml @@ -0,0 +1,16 @@ +# 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. filelogreceiver) +component: cmd/metadata + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Ensure template files are downloaded as part of the `go get` and embeded into the application + +# One or more tracking issues related to the change +issues: [17442] + +# (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: diff --git a/cmd/mdatagen/embeded_templates.go b/cmd/mdatagen/embeded_templates.go new file mode 100644 index 000000000000..3c65d094143f --- /dev/null +++ b/cmd/mdatagen/embeded_templates.go @@ -0,0 +1,24 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import "embed" + +// templateFS ensures that the files needed +// to generate metadata as an embedded filesystem since +// `go get` doesn't require these files to be downloaded. +// +//go:embed templates/*.tmpl templates/testdata/*.tmpl +var templateFS embed.FS diff --git a/cmd/mdatagen/embeded_templates_test.go b/cmd/mdatagen/embeded_templates_test.go new file mode 100644 index 000000000000..1e948d622da6 --- /dev/null +++ b/cmd/mdatagen/embeded_templates_test.go @@ -0,0 +1,51 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "io/fs" + "path" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEnsureTemplatesLoaded(t *testing.T) { + t.Parallel() + + const ( + rootDir = "templates" + ) + + var ( + templateFiles = map[string]struct{}{ + path.Join(rootDir, "documentation.md.tmpl"): {}, + path.Join(rootDir, "metrics_test.go.tmpl"): {}, + path.Join(rootDir, "metrics.go.tmpl"): {}, + path.Join(rootDir, "testdata", "config.yaml.tmpl"): {}, + } + count = 0 + ) + assert.NoError(t, fs.WalkDir(templateFS, ".", func(path string, d fs.DirEntry, err error) error { + if d != nil && d.IsDir() { + return nil + } + count++ + assert.Contains(t, templateFiles, path) + return nil + })) + assert.Equal(t, len(templateFiles), count, "Must match the expected number of calls") + +} diff --git a/cmd/mdatagen/main.go b/cmd/mdatagen/main.go index 3c621cc8dc1f..112716f97cdb 100644 --- a/cmd/mdatagen/main.go +++ b/cmd/mdatagen/main.go @@ -23,7 +23,6 @@ import ( "log" "os" "path/filepath" - "runtime" "strings" "text/template" ) @@ -48,11 +47,7 @@ func run(ymlPath string) error { return fmt.Errorf("failed loading %v: %w", ymlPath, err) } - _, filename, _, ok := runtime.Caller(0) - if !ok { - return errors.New("unable to determine filename") - } - tmplDir := filepath.Join(filepath.Dir(filename), "templates") + tmplDir := "templates" codeDir := filepath.Join(ymlDir, "internal", "metadata") if err = os.MkdirAll(filepath.Join(codeDir, "testdata"), 0700); err != nil { @@ -104,7 +99,7 @@ func generateFile(tmplFile string, outputFile string, md metadata) error { }, "stringsJoin": strings.Join, "inc": func(i int) int { return i + 1 }, - }).ParseFiles(tmplFile)) + }).ParseFS(templateFS, tmplFile)) buf := bytes.Buffer{}