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

Add writing to CWAgent's log before a panic #421

Merged
merged 24 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions cfg/aws/refreshable_shared_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Refreshable_shared_credentials_provider struct {
func (p *Refreshable_shared_credentials_provider) Retrieve() (credentials.Value, error) {

p.SetExpiration(time.Now().Add(p.ExpiryWindow), 0)
creds, e := p.sharedCredentialsProvider.Retrieve()
creds, err := p.sharedCredentialsProvider.Retrieve()
SaxyPandaBear marked this conversation as resolved.
Show resolved Hide resolved

return creds, e
return creds, err
}
35 changes: 17 additions & 18 deletions cmd/config-downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package main
import (
"flag"
"io/ioutil"
"log"
"os"

"strings"

configaws "github.com/aws/amazon-cloudwatch-agent/cfg/aws"
Expand Down Expand Up @@ -125,26 +125,24 @@ func main() {
if inputConfig != "" {
f, err := os.Open(inputConfig)
if err != nil {
fmt.Printf("E! Failed to open Common Config: %v\n", err)
panic(err)
log.Panicf("E! Failed to open Common Config: %v", err)
SaxyPandaBear marked this conversation as resolved.
Show resolved Hide resolved
}

if err := cc.Parse(f); err != nil {
fmt.Printf("E! Failed to parse Common Config: %v\n", err)
panic(err)
log.Panicf("E! Failed to open Common Config: %v", err)
}
}
util.SetProxyEnv(cc.ProxyMap())
util.SetSSLEnv(cc.SSLMap())
var errorMessage string
if downloadLocation == "" || outputDir == "" {
executable, e := os.Executable()
if e == nil {
errorMessage = fmt.Sprintf("usage: " + filepath.Base(executable) + " --output-dir <path> --download-source ssm:<parameter-store-name> ")
executable, err := os.Executable()
if err == nil {
errorMessage = fmt.Sprintf("E! usage: " + filepath.Base(executable) + " --output-dir <path> --download-source ssm:<parameter-store-name> ")
} else {
errorMessage = fmt.Sprintf("usage: --output-dir <path> --download-source ssm:<parameter-store-name> ")
errorMessage = fmt.Sprintf("E! usage: --output-dir <path> --download-source ssm:<parameter-store-name> ")
}
panic(errorMessage)
log.Panicf(errorMessage)
}

mode = sdkutil.DetectAgentMode(mode)
Expand All @@ -154,12 +152,12 @@ func main() {
if region == "" && downloadLocation != locationDefault {
fmt.Println("Unable to determine aws-region.")
if mode == config.ModeEC2 {
errorMessage = fmt.Sprintf("Please check if you can access the metadata service. For exampe, on linux, run 'wget -q -O - http://169.254.169.254/latest/meta-data/instance-id && echo' ")
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' ")
SaxyPandaBear marked this conversation as resolved.
Show resolved Hide resolved
} else {
errorMessage = fmt.Sprintf("Please make sure the credentials and region set correctly on your hosts.\n" +
errorMessage = fmt.Sprintf("E! Please make sure the credentials and region set correctly on your hosts.\n" +
"Refer to http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html")
}
panic(errorMessage)
log.Panicf(errorMessage)
}

// clean up output dir for tmp files before writing out new tmp file.
Expand Down Expand Up @@ -187,7 +185,8 @@ func main() {

locationArray := strings.SplitN(downloadLocation, locationSeparator, 2)
if locationArray == nil || len(locationArray) < 2 && downloadLocation != locationDefault {
panic(fmt.Sprintf("downloadLocation %s is malformated.\n", downloadLocation))
errorMessage := fmt.Sprintf("E! downloadLocation %s is malformated.", downloadLocation)
log.Panicf(errorMessage)
khanhntd marked this conversation as resolved.
Show resolved Hide resolved
}

var config, outputFilePath string
Expand All @@ -209,25 +208,25 @@ func main() {
config, err = readFromFile(locationArray[1])
}
default:
panic(fmt.Sprintf("location type %s is not supported.", locationArray[0]))
log.Panicf("E! Location type %s is not supported.", locationArray[0])
}

if err != nil {
panic(fmt.Sprintf("Fail to fetch/remove json config: %v\n", err))
log.Panicf("E! Fail to fetch/remove json config: %v", err)
}

if multiConfig != "remove" {
outputFilePath = filepath.Join(outputDir, outputFilePath+context.TmpFileSuffix)
err = ioutil.WriteFile(outputFilePath, []byte(config), 0644)
if err != nil {
panic(fmt.Sprintf("Failed to write the json file %v: %v\n", outputFilePath, err))
log.Panicf("E! Failed to write the json file %v: %v", outputFilePath, err)
} else {
fmt.Printf("Successfully fetched the config and saved in %s\n", outputFilePath)
}
} else {
outputFilePath = filepath.Join(outputDir, outputFilePath)
if err := os.Remove(outputFilePath); err != nil {
panic(fmt.Sprintf("Failed to remove the json file %v: %v", outputFilePath, err))
log.Panicf("E! Failed to remove the json file %v: %v", outputFilePath, err)
} else {
fmt.Printf("Successfully removed the config file %s\n", outputFilePath)
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/config-translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"flag"
"fmt"
"log"
"os"
"os/user"
Expand Down Expand Up @@ -90,16 +89,16 @@ func main() {

mergedJsonConfigMap, err := cmdutil.GenerateMergedJsonConfigMap(ctx)
if err != nil {
panic(fmt.Sprintf("E! Failed to generate merged json config: %v", err))
log.Panicf("E! Failed to generate merged json config: %v", err)
}

if !ctx.RunInContainer() {
// run as user only applies to non container situation.
current, e := user.Current()
if e == nil && current.Name == "root" {
current, err := user.Current()
if err == nil && current.Name == "root" {
runAsUser, err := cmdutil.DetectRunAsUser(mergedJsonConfigMap)
if err != nil {
panic("E! Failed to detectRunAsUser\n")
log.Panicf("E! Failed to detectRunAsUser")
khanhntd marked this conversation as resolved.
Show resolved Hide resolved
}
cmdutil.VerifyCredentials(ctx, runAsUser)
}
Expand Down
5 changes: 3 additions & 2 deletions integration/clean/clean_ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ package main

import (
"context"
"log"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
smithyTime "github.com/aws/smithy-go/time"
"log"
"time"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion integration/test/agent_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func RunShellScript(path string) {
}

func ReplaceLocalStackHostName(pathIn string) {
out, err := exec.Command("bash", "-c", "sed -i 's/localhost.localstack.cloud/'\"$LOCAL_STACK_HOST_NAME\"'/g' " + pathIn).Output()
out, err := exec.Command("bash", "-c", "sed -i 's/localhost.localstack.cloud/'\"$LOCAL_STACK_HOST_NAME\"'/g' "+pathIn).Output()

if err != nil {
log.Fatal(fmt.Sprint(err) + string(out))
Expand Down
14 changes: 7 additions & 7 deletions integration/test/ca_bundle/ca_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func TestBundle(t *testing.T) {
log.Printf("resource file location %s find target %t", parameter.dataInput, parameter.findTarget)
t.Run(fmt.Sprintf("resource file location %s find target %t", parameter.dataInput, parameter.findTarget), func(t *testing.T) {
test.ReplaceLocalStackHostName(parameter.dataInput + configJSON)
test.CopyFile(parameter.dataInput + configJSON, configOutputPath)
test.CopyFile(parameter.dataInput + commonConfigTOML, commonConfigOutputPath)
test.StartAgent(configOutputPath);
time.Sleep(agentRuntime);
log.Printf("Agent has been running for : %s", agentRuntime.String());
test.StopAgent();
output := test.ReadAgentOutput(agentRuntime);
test.CopyFile(parameter.dataInput+configJSON, configOutputPath)
test.CopyFile(parameter.dataInput+commonConfigTOML, commonConfigOutputPath)
test.StartAgent(configOutputPath)
time.Sleep(agentRuntime)
log.Printf("Agent has been running for : %s", agentRuntime.String())
test.StopAgent()
output := test.ReadAgentOutput(agentRuntime)
containsTarget := outputLogContainsTarget(output)
if (parameter.findTarget && !containsTarget) || (!parameter.findTarget && containsTarget) {
t.Errorf("Find target is %t contains target is %t", parameter.findTarget, containsTarget)
Expand Down
2 changes: 1 addition & 1 deletion internal/containerinsightscommon/nodeCapacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type NodeCapacity struct {

func NewNodeCapacity() *NodeCapacity {
if _, err := os.Lstat("/rootfs/proc"); os.IsNotExist(err) {
panic("/rootfs/proc doesn't exists")
log.Panic("E! /rootfs/proc does not exist")
}
if err := os.Setenv(GoPSUtilProcDirEnv, "/rootfs/proc"); err != nil {
log.Printf("E! NodeCapacity cannot set goPSUtilProcDirEnv to /rootfs/proc %v", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/ecsservicediscovery/containerinstanceprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func NewContainerInstanceProcessor(ecs *ecs.ECS, ec2 *ec2.EC2, s *ProcessorStats
// initiate the container instance metadata LRU caching
lru, err := simplelru.NewLRU(ec2metadataCacheSize, nil)
if err != nil {
panic(err)
log.Panicf("E! Initial container instance with caching failed because of %v", err)
}
p.ec2MetaDataCache = lru
return p
}

func splitMapKeys(a map[string]*EC2MetaData, size int) [][]string {
if size == 0 {
panic("splitMapKeys size cannot be zero.")
log.Panic("splitMapKeys size cannot be zero.")
}

result := make([][]string, 0)
Expand Down
4 changes: 3 additions & 1 deletion internal/ecsservicediscovery/taskdefinitionprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package ecsservicediscovery

import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/hashicorp/golang-lru/simplelru"
Expand Down Expand Up @@ -31,7 +33,7 @@ func NewTaskDefinitionProcessor(ecs *ecs.ECS, s *ProcessorStats) *TaskDefinition
// initiate the caching
lru, err := simplelru.NewLRU(taskDefCacheSize, nil)
if err != nil {
panic(err)
log.Panicf("E! Initial task definition with caching failed because of %v", err)
}
p.taskDefCache = lru
return p
Expand Down
6 changes: 5 additions & 1 deletion internal/publisher/blockingfifoqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@

package publisher

import (
"log"
)

// It is a FIFO queue with the functionality that block the caller if the queue size reaches to the maxSize
type BlockingFifoQueue struct {
queue chan interface{}
}

func NewBlockingFifoQueue(size int) *BlockingFifoQueue {
if size <= 0 {
panic("Queue Size should be larger than 0!")
log.Panic("E! Queue Size should be larger than 0!")
}

return &BlockingFifoQueue{queue: make(chan interface{}, size)}
Expand Down
2 changes: 1 addition & 1 deletion internal/publisher/nonblockingfifoqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type NonBlockingFifoQueue struct {

func NewNonBlockingFifoQueue(size int) *NonBlockingFifoQueue {
if size <= 0 {
panic("Queue Size should be larger than 0!")
log.Panic("E! Queue Size should be larger than 0!")
}
return &NonBlockingFifoQueue{
queue: list.New(),
Expand Down
2 changes: 1 addition & 1 deletion internal/publisher/nonblockinglifoqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type node struct {

func NewNonBlockingLifoQueue(size int) *NonBlockingLifoQueue {
if size <= 0 {
panic("Queue Size should be larger than 0!")
log.Panic("E! Queue Size should be larger than 0!")
}
return &NonBlockingLifoQueue{maxSize: size}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/cadvisor/cadvisor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *Cadvisor) Gather(acc telegraf.Accumulator) error {
var err error

if c.manager == nil && c.initManager() != nil {
panic("Cannot initiate manager")
log.Panicf("E! Cannot initiate manager")
}

req := &cinfo.ContainerInfoRequest{
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/logfile/logfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func TestLogsFileAutoRemoval(t *testing.T) {
}()

wg.Add(1)

go func() {
defer wg.Done()

Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/logfile/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func (tail *Tail) waitForChanges() error {
case <-tail.Dying():
return ErrStop
}
panic("unreachable")

}

func (tail *Tail) openReader() {
Expand Down Expand Up @@ -593,7 +593,7 @@ func (tail *Tail) exitOnDeletion() {
// with the last chunk of variable size.
func partitionString(s string, chunkSize int) []string {
if chunkSize <= 0 {
panic("invalid chunkSize")
panic("Invalid chunkSize")
SaxyPandaBear marked this conversation as resolved.
Show resolved Hide resolved
}
length := len(s)
chunks := 1 + length/chunkSize
Expand Down
1 change: 0 additions & 1 deletion plugins/inputs/logfile/tail/watch/inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
return tomb.ErrDying
}
}
panic("unreachable")
}

func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, pos int64) (*FileChanges, error) {
Expand Down
1 change: 0 additions & 1 deletion plugins/inputs/logfile/tail/watch/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func (fw *PollingFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
return tomb.ErrDying
}
}
panic("unreachable")
}

func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, pos int64) (*FileChanges, error) {
Expand Down
10 changes: 5 additions & 5 deletions plugins/inputs/logfile/tail/winfile/winfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func Open(path string, mode int, perm uint32) (fd syscall.Handle, err error) {
default:
createmode = syscall.OPEN_EXISTING
}
h, e := syscall.CreateFile(pathp, access, sharemode, sa, createmode, syscall.FILE_ATTRIBUTE_NORMAL, 0)
return h, e
h, err := syscall.CreateFile(pathp, access, sharemode, sa, createmode, syscall.FILE_ATTRIBUTE_NORMAL, 0)
return h, err
}

// https://github.com/jnwhiteh/golang/blob/master/src/pkg/syscall/syscall_windows.go#L211
Expand All @@ -69,9 +69,9 @@ func makeInheritSa() *syscall.SecurityAttributes {

// https://github.com/jnwhiteh/golang/blob/master/src/pkg/os/file_windows.go#L133
func OpenFile(name string, flag int, perm os.FileMode) (file *os.File, err error) {
r, e := Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
if e != nil {
return nil, e
r, err := Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
if err != nil {
return nil, err
}
return os.NewFile(uintptr(r), name), nil
}
Expand Down
18 changes: 9 additions & 9 deletions plugins/inputs/prometheus_scraper/metrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func Test_metricAppender_Add_BadMetricName(t *testing.T) {
{Name: "name_b", Value: "value_b"},
}

r, e := ma.Add(ls, ts, v)
r, err := ma.Add(ls, ts, v)
assert.Equal(t, uint64(0), r)
assert.Equal(t, "metricName of the times-series is missing", e.Error())
assert.Equal(t, "metricName of the times-series is missing", err.Error())
}

func Test_metricAppender_Add(t *testing.T) {
Expand All @@ -35,9 +35,9 @@ func Test_metricAppender_Add(t *testing.T) {
{Name: "tag_a", Value: "a"},
}

ref, e := ma.Add(ls, ts, v)
ref, err := ma.Add(ls, ts, v)
assert.Equal(t, ref, uint64(0))
assert.Nil(t, e)
assert.Nil(t, err)
mac, _ := ma.(*metricAppender)
assert.Equal(t, 1, len(mac.batch))

Expand Down Expand Up @@ -68,9 +68,9 @@ func Test_metricAppender_Rollback(t *testing.T) {
{Name: "tag_a", Value: "a"},
}

ref, e := ma.Add(ls, ts, v)
ref, err := ma.Add(ls, ts, v)
assert.Equal(t, ref, uint64(0))
assert.Nil(t, e)
assert.Nil(t, err)
mac, _ := ma.(*metricAppender)
assert.Equal(t, 1, len(mac.batch))

Expand All @@ -89,12 +89,12 @@ func Test_metricAppender_Commit(t *testing.T) {
{Name: "tag_a", Value: "a"},
}

ref, e := ma.Add(ls, ts, v)
ref, err := ma.Add(ls, ts, v)
assert.Equal(t, ref, uint64(0))
assert.Nil(t, e)
assert.Nil(t, err)
mac, _ := ma.(*metricAppender)
assert.Equal(t, 1, len(mac.batch))
err := ma.Commit()
err = ma.Commit()
assert.Equal(t, nil, err)

pmb := <-mbCh
Expand Down
Loading