Skip to content

Commit 6df1e5a

Browse files
authored
Update fallback for OTEL config flag (#1697)
1 parent c244241 commit 6df1e5a

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ func main() {
494494
flag.Var(&fOtelConfigs, configprovider.OtelConfigFlagName, "YAML configuration files to run OTel pipeline")
495495
flag.Parse()
496496
if len(fOtelConfigs) == 0 {
497-
_ = fOtelConfigs.Set(paths.YamlConfigPath)
497+
_ = fOtelConfigs.Set(getFallbackOtelConfig(*fTomlConfig, paths.YamlConfigPath))
498498
}
499499
args := flag.Args()
500500
sectionFilters, inputFilters, outputFilters := []string{}, []string{}, []string{}
@@ -743,3 +743,28 @@ func checkRightForBinariesFileWithInputPlugins(inputPlugins []string) (string, e
743743

744744
return "", nil
745745
}
746+
747+
// getFallbackOtelConfig returns the first fallback YAML file that exists. It checks files in the following order:
748+
// 1. Default YAML path
749+
// 2. Default YAML in the provided TOML directory
750+
// 3. YAML with the same name as the provided TOML
751+
func getFallbackOtelConfig(tomlPath, defaultYamlPath string) string {
752+
candidatePaths := []string{defaultYamlPath}
753+
if tomlPath != "" {
754+
tomlDir := filepath.Dir(tomlPath)
755+
samePathYAML := strings.TrimSuffix(tomlPath, filepath.Ext(tomlPath)) + ".yaml"
756+
candidatePaths = append(candidatePaths,
757+
filepath.Join(tomlDir, paths.YAML),
758+
samePathYAML,
759+
)
760+
}
761+
fallbackPath := defaultYamlPath
762+
for _, candidatePath := range candidatePaths {
763+
_, err := os.Stat(candidatePath)
764+
if err == nil {
765+
fallbackPath = candidatePath
766+
break
767+
}
768+
}
769+
return fallbackPath
770+
}

cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
2121
"github.com/aws/amazon-cloudwatch-agent/internal/merge/confmap"
2222
"github.com/aws/amazon-cloudwatch-agent/logger"
23+
"github.com/aws/amazon-cloudwatch-agent/tool/paths"
2324
)
2425

2526
func Test_getCollectorParams(t *testing.T) {
@@ -154,6 +155,52 @@ service:
154155
}
155156
}
156157

158+
func TestFallbackOtelConfig(t *testing.T) {
159+
defaultYamlRelativePath := filepath.Join("default", paths.YAML)
160+
testCases := map[string]struct {
161+
tomlRelativePath string
162+
filesToCreate []string
163+
want string
164+
}{
165+
"WithoutAnyFiles": {
166+
tomlRelativePath: filepath.Join("config", "config.toml"),
167+
want: defaultYamlRelativePath,
168+
},
169+
"WithDefaultYamlPath": {
170+
tomlRelativePath: filepath.Join("config", "config.toml"),
171+
filesToCreate: []string{defaultYamlRelativePath, filepath.Join("config", paths.YAML)},
172+
want: defaultYamlRelativePath,
173+
},
174+
"WithDefaultYamlInTomlDir": {
175+
tomlRelativePath: filepath.Join("config", "config.toml"),
176+
filesToCreate: []string{filepath.Join("config", paths.YAML), filepath.Join("config", "config.yaml")},
177+
want: filepath.Join("config", paths.YAML),
178+
},
179+
"WithSameNameAsToml": {
180+
tomlRelativePath: filepath.Join("config", "config.toml"),
181+
filesToCreate: []string{filepath.Join("config", "config.yaml")},
182+
want: filepath.Join("config", "config.yaml"),
183+
},
184+
"WithoutTomlPath": {
185+
tomlRelativePath: "",
186+
filesToCreate: []string{filepath.Join("config", "config.yaml")},
187+
want: defaultYamlRelativePath,
188+
},
189+
}
190+
for name, testCase := range testCases {
191+
t.Run(name, func(t *testing.T) {
192+
tmpDir := t.TempDir()
193+
for _, fileToCreate := range testCase.filesToCreate {
194+
path := filepath.Join(tmpDir, fileToCreate)
195+
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0755))
196+
require.NoError(t, os.WriteFile(path, nil, 0600))
197+
}
198+
got := getFallbackOtelConfig(filepath.Join(tmpDir, testCase.tomlRelativePath), filepath.Join(tmpDir, defaultYamlRelativePath))
199+
assert.Equal(t, filepath.Join(tmpDir, testCase.want), got)
200+
})
201+
}
202+
}
203+
157204
func mustLoadFromFile(t *testing.T, path string) *confmap.Conf {
158205
conf, err := confmap.NewFileLoader(path).Load()
159206
require.NoError(t, err)

service/configprovider/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package configprovider
55

66
import (
7+
"flag"
78
"fmt"
89
)
910

@@ -13,6 +14,8 @@ const (
1314

1415
type OtelConfigFlags []string
1516

17+
var _ flag.Value = (*OtelConfigFlags)(nil)
18+
1619
func (o *OtelConfigFlags) String() string {
1720
return fmt.Sprint(*o)
1821
}

0 commit comments

Comments
 (0)