Skip to content

Commit

Permalink
Automated sync with upstream - last commit d31f1fb - run #81.1 (#542)
Browse files Browse the repository at this point in the history
Co-authored-by: Seth L <81644108+sethAmazon@users.noreply.github.com>
Co-authored-by: Chad Patel <chadpatel@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Github Action <action@github.com>
Co-authored-by: Jeffrey Chien <chienjef@amazon.com>
Co-authored-by: Adam <90734270+adam-mateen@users.noreply.github.com>
  • Loading branch information
6 people authored Oct 9, 2023
1 parent 7fc3c14 commit d1a0ae1
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 200 deletions.
35 changes: 20 additions & 15 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 @@ -51,14 +52,14 @@ const (
var fDebug = flag.Bool("debug", false,
"turn on debug logging")
var pprofAddr = flag.String("pprof-addr", "",
"pprof address to listen on, not activate pprof if empty")
"pprof address to listen on, disabled by default, examples: 'localhost:1234', ':4567' (restricted to localhost)")
var fQuiet = flag.Bool("quiet", false,
"run in quiet mode")
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")
var fEnvConfig = flag.String("envconfig", "", "env configuration file to load")
var fConfigDirectory = flag.String("config-directory", "",
"directory containing additional *.conf files")
Expand All @@ -80,8 +81,6 @@ var fAggregatorFilters = flag.String("aggregator-filter", "",
"filter the aggregators to enable, separator is :")
var fProcessorFilters = flag.String("processor-filter", "",
"filter the processors to enable, separator is :")
var fUsage = flag.String("usage", "",
"print usage for a plugin, ie, 'telegraf --usage mysql'")
var fService = flag.String("service", "",
"operate on the service (windows only)")
var fServiceName = flag.String("service-name", "telegraf", "service name (windows only)")
Expand Down Expand Up @@ -137,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 @@ -181,17 +180,17 @@ func reloadLoop(
// The "config-translator" program populates that file.
func loadEnvironmentVariables(path string) error {
if path == "" {
return fmt.Errorf("No env config file specified")
return fmt.Errorf("no env config file specified")
}

bytes, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("Can't read env config file %s due to: %s", path, err.Error())
return fmt.Errorf("cannot read env config file %s due to: %s", path, err.Error())
}
envVars := map[string]string{}
err = json.Unmarshal(bytes, &envVars)
if err != nil {
return fmt.Errorf("Can't create env config due to: %s", err.Error())
return fmt.Errorf("cannot create env config due to: %s", err.Error())
}

for key, val := range envVars {
Expand All @@ -203,7 +202,7 @@ func loadEnvironmentVariables(path string) error {

func getEnvConfigPath(configPath, envConfigPath string) (string, error) {
if configPath == "" {
return "", fmt.Errorf("No config file specified")
return "", fmt.Errorf("no config file specified")
}
//load the environment variables that's saved in json env config file
if envConfigPath == "" {
Expand All @@ -217,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 @@ -437,6 +436,9 @@ func main() {
parts := strings.Split(pprofHostPort, ":")
if len(parts) == 2 && parts[0] == "" {
pprofHostPort = fmt.Sprintf("localhost:%s", parts[1])
} else if parts[0] != "localhost" {
log.Printf("W! Not starting pprof, it is restricted to localhost:nnnn")
return
}
pprofHostPort = "http://" + pprofHostPort + "/debug/pprof"

Expand Down Expand Up @@ -516,6 +518,9 @@ func main() {
}
envVars[parts[0]] = parts[1]
bytes, err = json.MarshalIndent(envVars, "", "\t")
if err != nil {
log.Fatalf("E! Failed to marshal env config: %v", err)
}
if err = os.WriteFile(*fEnvConfig, bytes, 0644); err != nil {
log.Fatalf("E! Failed to update env config: %v", err)
}
Expand Down Expand Up @@ -550,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 @@ -602,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 @@ -633,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,
"-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
}
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
}
46 changes: 10 additions & 36 deletions cmd/start-amazon-cloudwatch-agent/start-amazon-cloudwatch-agent.go
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
28 changes: 15 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@ replace github.com/influxdata/telegraf => github.com/aws/telegraf v0.10.2-0.2022

// Replace with https://github.com/amazon-contributing/opentelemetry-collector-contrib, there are no requirements for all receivers/processors/exporters
// to be all replaced since there are some changes that will always be from upstream
replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter => github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awsemfexporter v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter => github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awsemfexporter v0.0.0-20231005180140-4b74f352a689

replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter => github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awsxrayexporter v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter => github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awsxrayexporter v0.0.0-20231005180140-4b74f352a689

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray => github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/xray v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray => github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/xray v0.0.0-20231005180140-4b74f352a689

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver => github.com/amazon-contributing/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver => github.com/amazon-contributing/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver v0.0.0-20231005180140-4b74f352a689

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/k8s => github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/k8s v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/k8s => github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/k8s v0.0.0-20231005180140-4b74f352a689

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/containerinsight => github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/containerinsight v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/containerinsight => github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/containerinsight v0.0.0-20231005180140-4b74f352a689

// Replace with contrib to revert upstream change https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/20519
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus => github.com/amazon-contributing/opentelemetry-collector-contrib/pkg/translator/prometheus v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus => github.com/amazon-contributing/opentelemetry-collector-contrib/pkg/translator/prometheus v0.0.0-20231005180140-4b74f352a689

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza => github.com/amazon-contributing/opentelemetry-collector-contrib/pkg/stanza v0.0.0-20230928170322-0df38c533713

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver => github.com/amazon-contributing/opentelemetry-collector-contrib/receiver/prometheusreceiver v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver => github.com/amazon-contributing/opentelemetry-collector-contrib/receiver/prometheusreceiver v0.0.0-20231005180140-4b74f352a689

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil => github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/awsutil v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil => github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/awsutil v0.0.0-20231005180140-4b74f352a689

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/cwlogs => github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/cwlogs v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/cwlogs => github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/cwlogs v0.0.0-20231005180140-4b74f352a689

replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter => github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter => github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter v0.0.0-20231005180140-4b74f352a689

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsxrayreceiver => github.com/amazon-contributing/opentelemetry-collector-contrib/receiver/awsxrayreceiver v0.0.0-20230928170322-0df38c533713
replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsxrayreceiver => github.com/amazon-contributing/opentelemetry-collector-contrib/receiver/awsxrayreceiver v0.0.0-20231005180140-4b74f352a689

replace github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws => github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230928170322-0df38c533713
replace github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws => github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20231005180140-4b74f352a689

// Temporary fix, pending PR https://github.com/shirou/gopsutil/pull/957
replace github.com/shirou/gopsutil/v3 => github.com/aws/telegraf/patches/gopsutil/v3 v3.0.0-20230915153624-7629361f8380 // indirect
Expand Down Expand Up @@ -422,3 +422,5 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/amazon-contributing/opentelemetry-collector-contrib/pkg/stanza => github.com/amazon-contributing/opentelemetry-collector-contrib/pkg/stanza v0.0.0-20231005180140-4b74f352a689
Loading

0 comments on commit d1a0ae1

Please sign in to comment.