Skip to content

Commit

Permalink
Fix debug logging and revert Windows ctl script PS1 changes (#516)
Browse files Browse the repository at this point in the history
* change to use fmt

* Revert "Using cmd/c to avoid PowerShell ISE does not accept stderr (#473)"

This reverts commit 89d1912.

* remove log entirely

* update release notes
  • Loading branch information
SaxyPandaBear authored Jul 21, 2022
1 parent a809746 commit 74ba948
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Amazon CloudWatch Agent 1.247354.0 (2022-07-14)

Enhancements:
* Increase PutMetricData request size allowed in agent
* Fix Windows ctl issues on Windows 2012R2

========================================================================
Amazon CloudWatch Agent 1.247353.0 (2022-06-13)
Expand Down
2 changes: 1 addition & 1 deletion cmd/config-downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func main() {
region = util.DetectRegion(mode, cc.CredentialsMap())

if region == "" && downloadLocation != locationDefault {
log.Println("E! Unable to determine aws-region.")
fmt.Println("Unable to determine aws-region.")
if mode == config.ModeEC2 {
errorMessage = fmt.Sprintf("E! Please check if you can access the metadata service. For example, on linux, run 'wget -q -O - http://169.254.169.254/latest/meta-data/instance-id && echo' ")
} else {
Expand Down
21 changes: 9 additions & 12 deletions packaging/windows/amazon-cloudwatch-agent-ctl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Function AgentStart() {
if (!$svc) {
$startCommand = "`"${CWAProgramFiles}\start-amazon-cloudwatch-agent.exe`""
if (${service_name} -eq $CWOCServiceName) {
$startCommand = "`"${CWAProgramFiles}\cwagent-otel-collector.exe`" --config=`"${YAML}`""
$startCommand = "`"${CWAProgramFiles}\cwagent-otel-collector.exe`" --config=${YAML}"
}
New-Service -Name "${service_name}" -DisplayName "${service_display_name}" -Description "${service_display_name}" -DependsOn LanmanServer -BinaryPathName "${startCommand}" | Out-Null
# object returned by New-Service gives errors so retrieve it again
Expand Down Expand Up @@ -363,8 +363,8 @@ Function CWAConfig() {
if ($ConfigLocation -eq $AllConfig) {
Remove-Item -Path "${JSON_DIR}\*" -Force -ErrorAction SilentlyContinue
} else {
& cmd /c "`"$CWAProgramFiles\config-downloader.exe`" --output-dir `"${JSON_DIR}`" --download-source `"${ConfigLocation}`" --mode ${param_mode} --config `"${COMMON_CONIG}`" --multi-config ${multi_config} 2>&1"
CheckCMDResult # Exit immediately if config-downloader outputs any error
& $CWAProgramFiles\config-downloader.exe --output-dir "${JSON_DIR}" --download-source "${ConfigLocation}" --mode "${param_mode}" --config "${COMMON_CONIG}" --multi-config "${multi_config}"
CheckCMDResult
}

$jsonDirContent = Get-ChildItem "${JSON_DIR}" | Measure-Object
Expand All @@ -374,13 +374,11 @@ Function CWAConfig() {
Remove-Item "${TOML}" -Force -ErrorAction SilentlyContinue
} else {
Write-Output "Start configuration validation..."
& cmd /c "`"$CWAProgramFiles\config-translator.exe`" --input `"${JSON}`" --input-dir `"${JSON_DIR}`" --output `"${TOML}`" --mode ${param_mode} --config `"${COMMON_CONIG}`" --multi-config ${multi_config} 2>&1"
CheckCMDResult # Exit immediately if config-translator outputs any error

# Set ErrorActionPreference as Continue to continue on error when schema-test on toml file fails and
# return a UX-friendly message
& cmd /c "`"$CWAProgramFiles\config-translator.exe`" --input ${JSON} --input-dir ${JSON_DIR} --output ${TOML} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config} 2>&1"
CheckCMDResult
# Let command pass so we can check return code and give user-friendly error-message
$ErrorActionPreference = "Continue"
& cmd /c "`"${CWAProgramFiles}\amazon-cloudwatch-agent.exe`" --schematest --config `"${TOML}`" 2>&1" | Out-File $CVLogFile
& cmd /c "`"${CWAProgramFiles}\amazon-cloudwatch-agent.exe`" --schematest --config ${TOML} 2>&1" | Out-File $CVLogFile
if ($LASTEXITCODE -ne 0) {
Write-Output "Configuration validation second phase failed"
Write-Output "======== Error Log ========"
Expand Down Expand Up @@ -451,10 +449,9 @@ Function CWOCConfig() {
Copy-Item "${PREDEFINED_CONFIG_DATA}" -Destination "${YAML_DIR}/default.tmp"
Write-Output "Successfully fetched the config and saved in ${YAML_DIR}\default.tmp"
} else {
& cmd /c "`"$CWAProgramFiles\config-downloader.exe`" --output-dir `"${YAML_DIR}`" --download-source `"${OtelConfigLocation}`" --mode ${param_mode} --config `"${COMMON_CONIG}`" --multi-config ${multi_config} 2>&1"
# Use return instead of exit since CWAgent and ADOT Collector should be two independent agents as much as possible
& $CWAProgramFiles\config-downloader.exe --output-dir "${YAML_DIR}" --download-source "${OtelConfigLocation}" --mode "${param_mode}" --config "${COMMON_CONIG}" --multi-config "${multi_config}"
if ($LASTEXITCODE -ne 0) {
return
return
}
}

Expand Down
18 changes: 9 additions & 9 deletions translator/util/ec2util/ec2util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package ec2util

import (
"errors"
"log"
"fmt"
"net"
"sync"
"time"

"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
Expand Down Expand Up @@ -54,7 +54,7 @@ func initEC2UtilSingleton() (newInstance *ec2Util) {
ifs, err := net.Interfaces()

if err != nil {
log.Println("E! [EC2] An error occurred while fetching network interfaces: ", err)
fmt.Println("E! [EC2] An error occurred while fetching network interfaces: ", err)
}

for _, in := range ifs {
Expand All @@ -64,22 +64,22 @@ func initEC2UtilSingleton() (newInstance *ec2Util) {
}
}
if networkUp {
log.Println("D! [EC2] Found active network interface")
fmt.Println("D! [EC2] Found active network interface")
break
}

log.Println("W! [EC2] Sleep until network is up")
fmt.Println("W! [EC2] Sleep until network is up")
time.Sleep(1 * time.Second)
}

if !networkUp {
log.Println("E! [EC2] No available network interface")
fmt.Println("E! [EC2] No available network interface")
}

err := newInstance.deriveEC2MetadataFromIMDS()

if err != nil {
log.Println("E! [EC2] Cannot get EC2 Metadata from IMDS:", err)
fmt.Println("E! [EC2] Cannot get EC2 Metadata from IMDS:", err)
}

return
Expand All @@ -102,7 +102,7 @@ func (e *ec2Util) deriveEC2MetadataFromIMDS() error {
if hostname, err := md.GetMetadata("hostname"); err == nil {
e.Hostname = hostname
} else {
log.Println("E! [EC2] Fetch hostname from EC2 metadata fail:", err)
fmt.Println("E! [EC2] Fetch hostname from EC2 metadata fail:", err)
}

// More information on API: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
Expand All @@ -112,7 +112,7 @@ func (e *ec2Util) deriveEC2MetadataFromIMDS() error {
e.PrivateIP = instanceIdentityDocument.PrivateIP
e.InstanceID = instanceIdentityDocument.InstanceID
} else {
log.Println("E! [EC2] Fetch identity document from EC2 metadata fail:", err)
fmt.Println("E! [EC2] Fetch identity document from EC2 metadata fail:", err)
}

return nil
Expand Down

0 comments on commit 74ba948

Please sign in to comment.