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

Change runtime log level #293

Merged
merged 1 commit into from
Oct 14, 2021
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
1 change: 1 addition & 0 deletions cfg/envconfig/envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const (
AWS_CSM_ENABLED = "AWS_CSM_ENABLED"
AWS_CA_BUNDLE = "AWS_CA_BUNDLE"
CWAGENT_USER_AGENT = "CWAGENT_USER_AGENT"
CWAGENT_LOG_LEVEL = "CWAGENT_LOG_LEVEL"
)
90 changes: 75 additions & 15 deletions cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"errors"
"flag"
"fmt"
"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
"github.com/influxdata/wlog"
"io/ioutil"
"log"
"net/http"
Expand All @@ -28,17 +30,15 @@ import (
"github.com/aws/amazon-cloudwatch-agent/profiler"

lumberjack "github.com/aws/amazon-cloudwatch-agent/logger"
_ "github.com/aws/amazon-cloudwatch-agent/plugins"
"github.com/influxdata/telegraf/agent"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/logger"

//_ "github.com/influxdata/telegraf/plugins/aggregators/all"
"github.com/influxdata/telegraf/plugins/inputs"
//_ "github.com/influxdata/telegraf/plugins/inputs/all"
"github.com/influxdata/telegraf/plugins/outputs"
//_ "github.com/influxdata/telegraf/plugins/outputs/all"
//_ "github.com/influxdata/telegraf/plugins/processors/all"
_ "github.com/aws/amazon-cloudwatch-agent/plugins"

"github.com/kardianos/service"
)

Expand Down Expand Up @@ -84,6 +84,7 @@ var fService = flag.String("service", "",
var fServiceName = flag.String("service-name", "telegraf", "service name (windows only)")
var fServiceDisplayName = flag.String("service-display-name", "Telegraf Data Collector Service", "service display name (windows only)")
var fRunAsConsole = flag.Bool("console", false, "run as console application (windows only)")
var fSetEnv = flag.String("setenv", "", "set an env in the configuration file in the format of KEY=VALUE")

var (
version string
Expand Down Expand Up @@ -139,6 +140,36 @@ func reloadLoop(
}
}(ctx)

if envConfigPath, err := getEnvConfigPath(*fConfig, *fEnvConfig); err == nil {
// Reloads environment variables when file is changed
go func(ctx context.Context, envConfigPath string) {
var previousModTime time.Time
ticker := time.NewTicker(30 * time.Second)
jefchien marked this conversation as resolved.
Show resolved Hide resolved
defer ticker.Stop()
for {
select {
case <-ticker.C:
if info, err := os.Stat(envConfigPath); err == nil && info.ModTime().After(previousModTime) {
if err := loadEnvironmentVariables(envConfigPath); err != nil {
log.Printf("E! Unable to load env variables: %v", err)
SaxyPandaBear marked this conversation as resolved.
Show resolved Hide resolved
}
// Sets the log level based on environment variable
logLevel := os.Getenv(envconfig.CWAGENT_LOG_LEVEL)
if logLevel == "" {
logLevel = "INFO"
}
if err := wlog.SetLevelFromName(logLevel); err != nil {
log.Printf("E! Unable to set log level: %v", err)
}
previousModTime = info.ModTime()
}
case <-ctx.Done():
return
}
}
}(ctx, envConfigPath)
}

err := runAgent(ctx, inputFilters, outputFilters)
if err != nil && err != context.Canceled {
log.Fatalf("E! [telegraf] Error running agent: %v", err)
Expand Down Expand Up @@ -168,19 +199,27 @@ func loadEnvironmentVariables(path string) error {
return nil
}

func getEnvConfigPath(configPath, envConfigPath string) (string, error) {
if configPath == "" {
return "", fmt.Errorf("No config file specified")
}
//load the environment variables that's saved in json env config file
if envConfigPath == "" {
dir, _ := filepath.Split(configPath)
envConfigPath = filepath.Join(dir, defaultEnvCfgFileName)
}
return envConfigPath, nil
}

func runAgent(ctx context.Context,
inputFilters []string,
outputFilters []string,
) error {
if *fConfig == "" {
return fmt.Errorf("No config file specified")
}
//load the environment variables that's saved in json env config file
if *fEnvConfig == "" {
dir, _ := filepath.Split(*fConfig)
*fEnvConfig = filepath.Join(dir, defaultEnvCfgFileName)
envConfigPath, err := getEnvConfigPath(*fConfig, *fEnvConfig)
if err != nil {
return err
}
err := loadEnvironmentVariables(*fEnvConfig)
err = loadEnvironmentVariables(envConfigPath)
if err != nil && !*fSchemaTest {
log.Printf("W! Failed to load environment variables due to %s", err.Error())
}
Expand Down Expand Up @@ -310,7 +349,7 @@ type program struct {
processorFilters []string
}

func (p *program) Start(s service.Service) error {
func (p *program) Start(_ service.Service) error {
go p.run()
return nil
}
Expand All @@ -324,7 +363,7 @@ func (p *program) run() {
p.processorFilters,
)
}
func (p *program) Stop(s service.Service) error {
func (p *program) Stop(_ service.Service) error {
close(stop)
return nil
}
Expand Down Expand Up @@ -432,6 +471,27 @@ func main() {
log.Fatalf("E! %s and %s", err, err2)
}
return
case *fSetEnv != "":
if *fEnvConfig != "" {
parts := strings.SplitN(*fSetEnv, "=", 2)
if len(parts) == 2 {
bytes, err := ioutil.ReadFile(*fEnvConfig)
if err != nil {
log.Fatalf("E! Failed to read env config: %v", err)
}
envVars := map[string]string{}
err = json.Unmarshal(bytes, &envVars)
if err != nil {
log.Fatalf("E! Failed to unmarshal env config: %v", err)
}
envVars[parts[0]] = parts[1]
bytes, err = json.MarshalIndent(envVars, "", "\t")
if err = ioutil.WriteFile(*fEnvConfig, bytes, 0644); err != nil {
log.Fatalf("E! Failed to update env config: %v", err)
}
}
}
return
}

if runtime.GOOS == "windows" && windowsRunAsService() {
Expand Down Expand Up @@ -477,7 +537,7 @@ func main() {
} else {
winlogger, err := s.Logger(nil)
if err == nil {
//When in service mode, register eventlog target andd setup default logging to eventlog
//When in service mode, register eventlog target and setup default logging to eventlog
logger.RegisterEventLogger(winlogger)
logger.SetupLogging(logger.LogConfig{LogTarget: lumberjack.LogTargetLumberjack})
}
Expand Down
16 changes: 7 additions & 9 deletions cmd/config-translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os/user"
"path/filepath"

commonconfig "github.com/aws/amazon-cloudwatch-agent/cfg/commonconfig"
"github.com/aws/amazon-cloudwatch-agent/cfg/commonconfig"
"github.com/aws/amazon-cloudwatch-agent/translator"
"github.com/aws/amazon-cloudwatch-agent/translator/cmdutil"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
Expand Down Expand Up @@ -47,23 +47,21 @@ func initFlags() {
if err != nil {
log.Fatalf("E! Failed to open common-config file %s with error: %v", *inputConfig, err)
}
config, err := commonconfig.Parse(f)
conf, err := commonconfig.Parse(f)
if err != nil {
log.Fatalf("E! Failed to parse common-config file %s with error: %v", *inputConfig, err)
}
ctx.SetCredentials(config.CredentialsMap())
ctx.SetProxy(config.ProxyMap())
ctx.SetSSL(config.SSLMap())

ctx.SetCredentials(conf.CredentialsMap())
ctx.SetProxy(conf.ProxyMap())
ctx.SetSSL(conf.SSLMap())
}
translatorUtil.SetProxyEnv(ctx.Proxy())
translatorUtil.SetSSLEnv(ctx.SSL())
ctx.SetMode(translatorUtil.DetectAgentMode(*inputMode))

}

/**
* config-translator --input ${JSON} --input-dir ${JSON_DIR} --output ${TOML} --mode ${param_mode} --config ${COMMON_CONIG}
* config-translator --input ${JSON} --input-dir ${JSON_DIR} --output ${TOML} --mode ${param_mode} --config ${COMMON_CONFIG}
* --multi-config [default|append|remove]
*
* multi-config:
Expand All @@ -75,7 +73,7 @@ func main() {
initFlags()
defer func() {
if r := recover(); r != nil {
// Only emit error message if panic content is string(pre checked)
// Only emit error message if panic content is string(pre-checked)
// Not emitting the non-handled error message for now, we don't want to show non-user-friendly error message to customer
if val, ok := r.(string); ok {
log.Println(val)
Expand Down
47 changes: 41 additions & 6 deletions packaging/dependencies/amazon-cloudwatch-agent-ctl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ readonly YAML_DIR="${CWOC_CONFDIR}/cwagent-otel-collector.d"
readonly PREDEFINED_CONFIG_DATA="${AGENTDIR}/cwagent-otel-collector/var/.predefined-config-data"
readonly CV_LOG_FILE="${AGENTDIR}/logs/configuration-validation.log"
readonly COMMON_CONIG="${CONFDIR}/common-config.toml"
readonly ENV_CONFIG="${CONFDIR}/env-config.json"

readonly CWA_NAME='amazon-cloudwatch-agent'
readonly CWOC_NAME='cwagent-otel-collector'
Expand All @@ -33,9 +34,13 @@ SYSTEMD='false'
UsageString="


usage: amazon-cloudwatch-agent-ctl -a
stop|start|status|fetch-config|append-config|remove-config [-m
ec2|onPremise|auto] [-c default|all|ssm:<parameter-store-name>|file:<file-path>] [-o default|all|ssm:<parameter-store-name>|file:<file-path>] [-s]
usage: amazon-cloudwatch-agent-ctl -a
stop|start|status|fetch-config|append-config|remove-config|set-log-level
[-m ec2|onPremise|auto]
[-c default|all|ssm:<parameter-store-name>|file:<file-path>]
[-o default|all|ssm:<parameter-store-name>|file:<file-path>]
[-s]
[-l INFO|DEBUG|WARN|ERROR|OFF]

e.g.
1. apply a SSM parameter store config on EC2 instance and restart the agent afterwards:
Expand All @@ -52,6 +57,7 @@ UsageString="
fetch-config: apply config for agent, followed by -c or -o or both. Target config can be based on location (ssm parameter store name, file name), or 'default'.
append-config: append json config with the existing json configs if any, followed by -c. Target config can be based on the location (ssm parameter store name, file name), or 'default'.
remove-config: remove config for agent, followed by -c or -o or both. Target config can be based on the location (ssm parameter store name, file name), or 'all'.
set-log-level: sets the log level, followed by -l to provide the level in all caps.

-m: mode
ec2: indicate this is on ec2 host.
Expand All @@ -73,6 +79,9 @@ UsageString="
-s: optionally restart after configuring the agent configuration
this parameter is used for 'fetch-config', 'append-config', 'remove-config' action only.

-l: log level to set the agent to INFO, DEBUG, WARN, ERROR, or OFF
this parameter is used for 'set-log-level' only.

"

start_all() {
Expand Down Expand Up @@ -456,6 +465,30 @@ cwoc_config() {
fi
}

set_log_level_all() {
log_level="${1:-}"
case "${log_level}" in
INFO)
;;
DEBUG)
;;
ERROR)
;;
WARN)
;;
OFF)
;;
*) echo "Invalid log level: ${log_level} ${UsageString}" >&2
exit 1
;;
esac

runEnvConfigCommand="${CMDDIR}/amazon-cloudwatch-agent -setenv CWAGENT_LOG_LEVEL=${log_level} -envconfig ${ENV_CONFIG}"
echo "${runEnvConfigCommand}"
${runEnvConfigCommand} || return
echo "Set CWAGENT_LOG_LEVEL to ${log_level}"
}

main() {
action=''
cwa_config_location=''
Expand All @@ -465,9 +498,9 @@ main() {

# detect which init system is in use
if [ "$(/sbin/init --version 2>/dev/null | grep -c upstart)" = 1 ]; then
SYSTEMD='false'
SYSTEMD='false'
elif [ "$(systemctl | grep -c -E '\-\.mount\s')" = 1 ]; then
SYSTEMD='true'
SYSTEMD='true'
elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
echo "sysv-init is not supported" >&2
exit 1
Expand All @@ -477,7 +510,7 @@ main() {
fi

OPTIND=1
while getopts ":hsa:c:o:m:" opt; do
while getopts ":hsa:c:o:m:l:" opt; do
case "${opt}" in
h) echo "${UsageString}"
exit 0
Expand All @@ -487,6 +520,7 @@ main() {
c) cwa_config_location="${OPTARG}" ;;
o) cwoc_config_location="${OPTARG}" ;;
m) mode="${OPTARG}" ;;
l) log_level="${OPTARG}" ;;
\?) echo "Invalid option: -${OPTARG} ${UsageString}" >&2
;;
:) echo "Option -${OPTARG} requires an argument ${UsageString}" >&2
Expand Down Expand Up @@ -521,6 +555,7 @@ main() {
cond-restart) cond_restart_all ;;
# helper for rpm+deb uninstallation hooks, not expected to be called manually
preun) preun_all ;;
set-log-level) set_log_level_all "${log_level}" ;;
*) echo "Invalid action: ${action} ${UsageString}" >&2
exit 1
;;
Expand Down
Loading