Skip to content

Commit

Permalink
Add shared config fallback stat to agent info. (aws#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Sep 6, 2023
1 parent 96b9e03 commit a640535
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
3 changes: 3 additions & 0 deletions cfg/aws/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
29 changes: 22 additions & 7 deletions handlers/agentinfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -47,6 +48,7 @@ var (
version = readVersionFile()
fullVersion = getFullVersion(version)
id = uuid.NewString()
sharedConfigFallback atomic.Bool
)

var isRunningAsRoot = defaultIsRunningAsRoot
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions handlers/agentinfo/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()))
}

0 comments on commit a640535

Please sign in to comment.