From a64053509c062274577813cbe9b5977d6e2aaf33 Mon Sep 17 00:00:00 2001 From: Jeffrey Chien Date: Wed, 6 Sep 2023 17:20:57 -0400 Subject: [PATCH] Add shared config fallback stat to agent info. (#842) --- cfg/aws/credentials.go | 3 +++ handlers/agentinfo/info.go | 29 ++++++++++++++++++++++------- handlers/agentinfo/info_test.go | 13 +++++++++++++ 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/cfg/aws/credentials.go b/cfg/aws/credentials.go index a2e3dc146a..34ab40f54a 100644 --- a/cfg/aws/credentials.go +++ b/cfg/aws/credentials.go @@ -18,6 +18,8 @@ import ( "github.com/aws/aws-sdk-go/aws/endpoints" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sts" + + "github.com/aws/amazon-cloudwatch-agent/handlers/agentinfo" ) const ( @@ -114,6 +116,7 @@ func getSession(config *aws.Config) *session.Session { if len(found) > 0 { log.Printf("W! Unused shared config file(s) found: %v. If you would like to use them, "+ "please update your common-config.toml.", found) + agentinfo.RecordSharedConfigFallback() } } return ses diff --git a/handlers/agentinfo/info.go b/handlers/agentinfo/info.go index 581cc85f41..77d238d620 100644 --- a/handlers/agentinfo/info.go +++ b/handlers/agentinfo/info.go @@ -16,6 +16,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" "github.com/aws/aws-sdk-go/aws" @@ -47,6 +48,7 @@ var ( version = readVersionFile() fullVersion = getFullVersion(version) id = uuid.NewString() + sharedConfigFallback atomic.Bool ) var isRunningAsRoot = defaultIsRunningAsRoot @@ -65,13 +67,14 @@ type agentInfo struct { } type agentStats struct { - CpuPercent *float64 `json:"cpu,omitempty"` - MemoryBytes *uint64 `json:"mem,omitempty"` - FileDescriptorCount *int32 `json:"fd,omitempty"` - ThreadCount *int32 `json:"th,omitempty"` - LatencyMillis *int64 `json:"lat,omitempty"` - PayloadBytes *int `json:"load,omitempty"` - StatusCode *int `json:"code,omitempty"` + CpuPercent *float64 `json:"cpu,omitempty"` + MemoryBytes *uint64 `json:"mem,omitempty"` + FileDescriptorCount *int32 `json:"fd,omitempty"` + ThreadCount *int32 `json:"th,omitempty"` + LatencyMillis *int64 `json:"lat,omitempty"` + PayloadBytes *int `json:"load,omitempty"` + StatusCode *int `json:"code,omitempty"` + SharedConfigFallback *int `json:"scfb,omitempty"` } func New(groupName string) AgentInfo { @@ -119,6 +122,7 @@ func (ai *agentInfo) RecordOpData(latency time.Duration, payloadBytes int, err e stats.MemoryBytes = ai.memoryBytes() stats.FileDescriptorCount = ai.fileDescriptorCount() stats.ThreadCount = ai.threadCount() + stats.SharedConfigFallback = getSharedConfigFallback() ai.nextUpdate = now.Add(updateInterval) } @@ -297,3 +301,14 @@ func isUsageDataEnabled() bool { func defaultIsRunningAsRoot() bool { return os.Getuid() == 0 } + +func RecordSharedConfigFallback() { + sharedConfigFallback.Store(true) +} + +func getSharedConfigFallback() *int { + if sharedConfigFallback.Load() { + return aws.Int(1) + } + return nil +} diff --git a/handlers/agentinfo/info_test.go b/handlers/agentinfo/info_test.go index 3a3e0eea6c..10e49ba602 100644 --- a/handlers/agentinfo/info_test.go +++ b/handlers/agentinfo/info_test.go @@ -147,6 +147,9 @@ func TestGetAgentStats(t *testing.T) { stats.StatusCode = nil assert.Empty(t, getAgentStats(stats)) + + stats.SharedConfigFallback = aws.Int(1) + assert.Equal(t, "\"scfb\":1", getAgentStats(stats)) } func TestGetStatusCode(t *testing.T) { @@ -233,3 +236,13 @@ func TestIsUsageDataEnabled(t *testing.T) { t.Setenv(envconfig.CWAGENT_USAGE_DATA, "FALSE") assert.False(t, getUsageDataEnabled()) } + +func TestSharedConfigFallback(t *testing.T) { + defer sharedConfigFallback.Store(false) + assert.Nil(t, getSharedConfigFallback()) + RecordSharedConfigFallback() + assert.Equal(t, 1, *(getSharedConfigFallback())) + RecordSharedConfigFallback() + RecordSharedConfigFallback() + assert.Equal(t, 1, *(getSharedConfigFallback())) +}