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

Use default value when -otelconfig option not given #894

Merged
merged 5 commits into from
Oct 9, 2023
Merged
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
17 changes: 9 additions & 8 deletions cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/aws/amazon-cloudwatch-agent/service/configprovider"
"github.com/aws/amazon-cloudwatch-agent/service/defaultcomponents"
"github.com/aws/amazon-cloudwatch-agent/service/registry"
"github.com/aws/amazon-cloudwatch-agent/tool/paths"
)

const (
Expand All @@ -57,8 +58,8 @@ var fQuiet = flag.Bool("quiet", false,
var fTest = flag.Bool("test", false, "enable test mode: gather metrics, print them out, and exit")
var fTestWait = flag.Int("test-wait", 0, "wait up to this many seconds for service inputs to complete in test mode")
var fSchemaTest = flag.Bool("schematest", false, "validate the toml file schema")
var fConfig = flag.String("config", "", "configuration file to load")
var fOtelConfig = flag.String("otelconfig", "", "YAML configuration file to run OTel pipeline")
var fTomlConfig = flag.String("config", "", "configuration file to load")
var fOtelConfig = flag.String("otelconfig", paths.YamlConfigPath, "YAML configuration file to run OTel pipeline")
adam-mateen marked this conversation as resolved.
Show resolved Hide resolved
var fEnvConfig = flag.String("envconfig", "", "env configuration file to load")
var fConfigDirectory = flag.String("config-directory", "",
"directory containing additional *.conf files")
Expand Down Expand Up @@ -135,7 +136,7 @@ func reloadLoop(
}
}(ctx)

if envConfigPath, err := getEnvConfigPath(*fConfig, *fEnvConfig); err == nil {
if envConfigPath, err := getEnvConfigPath(*fTomlConfig, *fEnvConfig); err == nil {
// Reloads environment variables when file is changed
go func(ctx context.Context, envConfigPath string) {
var previousModTime time.Time
Expand Down Expand Up @@ -215,7 +216,7 @@ func runAgent(ctx context.Context,
inputFilters []string,
outputFilters []string,
) error {
envConfigPath, err := getEnvConfigPath(*fConfig, *fEnvConfig)
envConfigPath, err := getEnvConfigPath(*fTomlConfig, *fEnvConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -554,8 +555,8 @@ func main() {
// Handle the --service flag here to prevent any issues with tooling that
// may not have an interactive session, e.g. installing from Ansible.
if *fService != "" {
if *fConfig != "" {
svcConfig.Arguments = []string{"--config", *fConfig}
if *fTomlConfig != "" {
svcConfig.Arguments = []string{"--config", *fTomlConfig}
}
if *fConfigDirectory != "" {
svcConfig.Arguments = append(svcConfig.Arguments, "--config-directory", *fConfigDirectory)
Expand Down Expand Up @@ -606,7 +607,7 @@ func windowsRunAsService() bool {
}

func loadTomlConfigIntoAgent(c *config.Config) error {
err := c.LoadConfig(*fConfig)
err := c.LoadConfig(*fTomlConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -637,7 +638,7 @@ func validateAgentFinalConfigAndPlugins(c *config.Config) error {
if *fSchemaTest {
//up to this point, the given config file must be valid
fmt.Println(agentinfo.FullVersion())
fmt.Printf("The given config: %v is valid\n", *fConfig)
fmt.Printf("The given config: %v is valid\n", *fTomlConfig)
os.Exit(0)
}

Expand Down
38 changes: 12 additions & 26 deletions cmd/start-amazon-cloudwatch-agent/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func startAgent(writer io.WriteCloser) error {
if os.Getenv(config.RUN_IN_CONTAINER) == config.RUN_IN_CONTAINER_TRUE {
// Use exec so PID 1 changes to agent from start-agent.
execArgs := []string{
agentBinaryPath, // when using syscall.Exec, must pass binary name as args[0]
"-config", tomlConfigPath,
"-envconfig", envConfigPath,
"-otelconfig", yamlConfigPath,
paths.AgentBinaryPath, // when using syscall.Exec, must pass binary name as args[0]
"-config", paths.TomlConfigPath,
"-envconfig", paths.EnvConfigPath,
adam-mateen marked this conversation as resolved.
Show resolved Hide resolved
"-otelconfig", paths.YamlConfigPath,
"-pidfile", paths.AgentDir + "/var/amazon-cloudwatch-agent.pid",
}
if err := syscall.Exec(agentBinaryPath, execArgs, os.Environ()); err != nil {
if err := syscall.Exec(paths.AgentBinaryPath, execArgs, os.Environ()); err != nil {
return fmt.Errorf("error exec as agent binary: %w", err)
}
// We should never reach this line but the compiler doesn't know...
Expand All @@ -49,7 +49,7 @@ func startAgent(writer io.WriteCloser) error {
return err
}

name, err := exec.LookPath(agentBinaryPath)
name, err := exec.LookPath(paths.AgentBinaryPath)
if err != nil {
log.Printf("E! Failed to lookpath: %v ", err)
return err
Expand All @@ -62,10 +62,10 @@ func startAgent(writer io.WriteCloser) error {

// linux command has pid passed while windows does not
agentCmd := []string{
agentBinaryPath,
"-config", tomlConfigPath,
"-envconfig", envConfigPath,
"-otelconfig", yamlConfigPath,
paths.AgentBinaryPath,
"-config", paths.TomlConfigPath,
"-envconfig", paths.EnvConfigPath,
"-otelconfig", paths.YamlConfigPath,
"-pidfile", paths.AgentDir + "/var/amazon-cloudwatch-agent.pid",
}
if err = syscall.Exec(name, agentCmd, os.Environ()); err != nil {
Expand All @@ -80,22 +80,8 @@ func startAgent(writer io.WriteCloser) error {
func generateMergedJsonConfigMap() (map[string]interface{}, error) {
ctx := context.CurrentContext()
setCTXOS(ctx)
ctx.SetInputJsonFilePath(jsonConfigPath)
ctx.SetInputJsonDirPath(jsonDirPath)
ctx.SetInputJsonFilePath(paths.JsonConfigPath)
ctx.SetInputJsonDirPath(paths.JsonDirPath)
ctx.SetMultiConfig("remove")
return cmdutil.GenerateMergedJsonConfigMap(ctx)
}

func init() {
jsonConfigPath = paths.AgentDir + "/etc/" + JSON
jsonDirPath = paths.AgentDir + "/etc/" + paths.JsonDir
envConfigPath = paths.AgentDir + "/etc/" + ENV
tomlConfigPath = paths.AgentDir + "/etc/" + TOML
commonConfigPath = paths.AgentDir + "/etc/" + COMMON_CONFIG
yamlConfigPath = paths.AgentDir + "/etc/" + YAML

agentLogFilePath = paths.AgentDir + "/logs/" + AGENT_LOG_FILE

translatorBinaryPath = paths.AgentDir + "/bin/" + paths.TranslatorBinaryName
agentBinaryPath = paths.AgentDir + "/bin/" + paths.AgentBinaryName
}
adam-mateen marked this conversation as resolved.
Show resolved Hide resolved
36 changes: 4 additions & 32 deletions cmd/start-amazon-cloudwatch-agent/path_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"
"log"
"os"
"os/exec"

"github.com/aws/amazon-cloudwatch-agent/tool/paths"
Expand All @@ -23,40 +22,13 @@ func startAgent(writer io.WriteCloser) error {
}

cmd := exec.Command(
agentBinaryPath,
"-config", tomlConfigPath,
"-envconfig", envConfigPath,
"-otelconfig", yamlConfigPath,
paths.AgentBinaryPath,
"-config", paths.TomlConfigPath,
"-envconfig", paths.EnvConfigPath,
"-otelconfig", paths.YamlConfigPath,
)
stdoutStderr, err := cmd.CombinedOutput()
// log file is closed, so use fmt here
fmt.Printf("%s \n", stdoutStderr)
return err
}

func init() {
programFiles := os.Getenv("ProgramFiles")
var programData string
if _, ok := os.LookupEnv("ProgramData"); ok {
programData = os.Getenv("ProgramData")
} else {
// Windows 2003
programData = os.Getenv("ALLUSERSPROFILE") + "\\Application Data"
}

agentRootDir := programFiles + paths.AgentDir
agentConfigDir := programData + paths.AgentDir

jsonConfigPath = agentConfigDir + "\\" + JSON
jsonDirPath = agentConfigDir + paths.JsonDir
envConfigPath = agentConfigDir + "\\" + ENV
tomlConfigPath = agentConfigDir + "\\" + TOML
yamlConfigPath = agentConfigDir + "\\" + YAML

commonConfigPath = agentConfigDir + "\\" + COMMON_CONFIG

agentLogFilePath = agentConfigDir + "\\Logs\\" + AGENT_LOG_FILE

translatorBinaryPath = agentRootDir + "\\" + paths.TranslatorBinaryName
agentBinaryPath = agentRootDir + "\\" + paths.AgentBinaryName
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,21 @@ import (

"gopkg.in/natefinch/lumberjack.v2"

"github.com/aws/amazon-cloudwatch-agent/tool/paths"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
)

const (
COMMON_CONFIG = "common-config.toml"
JSON = "amazon-cloudwatch-agent.json"
TOML = "amazon-cloudwatch-agent.toml"
YAML = "amazon-cloudwatch-agent.yaml"
ENV = "env-config.json"

AGENT_LOG_FILE = "amazon-cloudwatch-agent.log"

//TODO this CONFIG_DIR_IN_CONTAINER should change to something indicate dir, keep it for now to avoid break testing
CONFIG_DIR_IN_CONTAINER = "/etc/cwagentconfig"
)

var (
jsonConfigPath string
jsonDirPath string
envConfigPath string
tomlConfigPath string
commonConfigPath string
yamlConfigPath string

agentLogFilePath string

translatorBinaryPath string
agentBinaryPath string
)

// We use an environment variable here because we need this condition before the translator reads agent config json file.
var runInContainer = os.Getenv(config.RUN_IN_CONTAINER)

func translateConfig() error {
args := []string{"--output", tomlConfigPath, "--mode", "auto"}
args := []string{"--output", paths.TomlConfigPath, "--mode", "auto"}
if runInContainer == config.RUN_IN_CONTAINER_TRUE {
args = append(args, "--input-dir", CONFIG_DIR_IN_CONTAINER)
args = append(args, "--input-dir", paths.CONFIG_DIR_IN_CONTAINER)
} else {
args = append(args, "--input", jsonConfigPath, "--input-dir", jsonDirPath, "--config", commonConfigPath)
args = append(args, "--input", paths.JsonConfigPath, "--input-dir", paths.JsonDirPath, "--config", paths.CommonConfigPath)
}
cmd := exec.Command(translatorBinaryPath, args...)
cmd := exec.Command(paths.TranslatorBinaryPath, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
err := cmd.Run()
Expand Down Expand Up @@ -83,7 +57,7 @@ func main() {

if runInContainer != config.RUN_IN_CONTAINER_TRUE {
writer = &lumberjack.Logger{
Filename: agentLogFilePath,
Filename: paths.AgentLogFilePath,
MaxSize: 100, //MB
MaxBackups: 5, //backup files
MaxAge: 7, //days
Expand All @@ -96,10 +70,10 @@ func main() {
if err := translateConfig(); err != nil {
log.Fatalf("E! Cannot translate JSON, ERROR is %v \n", err)
}
log.Printf("I! Config has been translated into TOML %s \n", tomlConfigPath)
printFileContents(tomlConfigPath)
log.Printf("I! Config has been translated into YAML %s \n", yamlConfigPath)
printFileContents(yamlConfigPath)
log.Printf("I! Config has been translated into TOML %s \n", paths.TomlConfigPath)
printFileContents(paths.TomlConfigPath)
log.Printf("I! Config has been translated into YAML %s \n", paths.YamlConfigPath)
printFileContents(paths.YamlConfigPath)

if err := startAgent(writer); err != nil {
log.Printf("E! Error when starting Agent, Error is %v \n", err)
Expand Down
27 changes: 27 additions & 0 deletions tool/paths/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT

package paths

const (
COMMON_CONFIG = "common-config.toml"
JSON = "amazon-cloudwatch-agent.json"
TOML = "amazon-cloudwatch-agent.toml"
YAML = "amazon-cloudwatch-agent.yaml"
ENV = "env-config.json"
AGENT_LOG_FILE = "amazon-cloudwatch-agent.log"
//TODO this CONFIG_DIR_IN_CONTAINER should change to something indicate dir, keep it for now to avoid break testing
CONFIG_DIR_IN_CONTAINER = "/etc/cwagentconfig"
)

var (
JsonConfigPath string
JsonDirPath string
EnvConfigPath string
TomlConfigPath string
CommonConfigPath string
YamlConfigPath string
AgentLogFilePath string
TranslatorBinaryPath string
AgentBinaryPath string
)
14 changes: 14 additions & 0 deletions tool/paths/paths_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package paths

import "path/filepath"

const (
AgentDir = "/opt/aws/amazon-cloudwatch-agent"
BinaryDir = "bin"
Expand All @@ -15,3 +17,15 @@ const (
WizardBinaryName = "amazon-cloudwatch-agent-config-wizard"
AgentStartName = "amazon-cloudwatch-agent-ctl"
)

func init() {
JsonConfigPath = filepath.Join(AgentDir, "etc", JSON)
JsonDirPath = filepath.Join(AgentDir, "etc", JsonDir)
EnvConfigPath = filepath.Join(AgentDir, "etc", ENV)
TomlConfigPath = filepath.Join(AgentDir, "etc", TOML)
CommonConfigPath = filepath.Join(AgentDir, "etc", COMMON_CONFIG)
YamlConfigPath = filepath.Join(AgentDir, "etc", YAML)
AgentLogFilePath = filepath.Join(AgentDir, "logs", AGENT_LOG_FILE)
TranslatorBinaryPath = filepath.Join(AgentDir, "bin", TranslatorBinaryName)
AgentBinaryPath = filepath.Join(AgentDir, "bin", AgentBinaryName)
}
28 changes: 28 additions & 0 deletions tool/paths/paths_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

package paths

import (
"os"
"path/filepath"
)

const (
AgentDir = "\\Amazon\\AmazonCloudWatchAgent\\"
JsonDir = "\\Configs"
Expand All @@ -15,3 +20,26 @@ const (
WizardBinaryName = "amazon-cloudwatch-agent-config-wizard.exe"
AgentStartName = "amazon-cloudwatch-agent-ctl.ps1"
)

func init() {
programFiles := os.Getenv("ProgramFiles")
var programData string
if _, ok := os.LookupEnv("ProgramData"); ok {
programData = os.Getenv("ProgramData")
} else {
// Windows 2003
programData = filepath.Join(os.Getenv("ALLUSERSPROFILE"), "Application Data")
}

AgentRootDir := filepath.Join(programFiles, AgentDir)
AgentConfigDir := filepath.Join(programData, AgentDir)
JsonConfigPath = filepath.Join(AgentConfigDir, JSON)
JsonDirPath = filepath.Join(AgentConfigDir, JsonDir)
EnvConfigPath = filepath.Join(AgentConfigDir, ENV)
TomlConfigPath = filepath.Join(AgentConfigDir, TOML)
YamlConfigPath = filepath.Join(AgentConfigDir, YAML)
CommonConfigPath = filepath.Join(AgentConfigDir, COMMON_CONFIG)
AgentLogFilePath = filepath.Join(AgentConfigDir, AGENT_LOG_FILE)
TranslatorBinaryPath = filepath.Join(AgentRootDir, TranslatorBinaryName)
AgentBinaryPath = filepath.Join(AgentRootDir, AgentBinaryName)
}