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

Replace uses of agentinfo with agenthealth. #925

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"

"github.com/aws/amazon-cloudwatch-agent/handlers/agentinfo"
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/stats/provider"
)

const (
Expand Down Expand Up @@ -116,7 +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()
provider.GetFlagsStats().SetFlag(provider.FlagSharedConfigFallback)
}
}
return ses
Expand Down
17 changes: 17 additions & 0 deletions cfg/envconfig/envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ const (
CWAGENT_LOG_LEVEL = "CWAGENT_LOG_LEVEL"
CWAGENT_USAGE_DATA = "CWAGENT_USAGE_DATA"
IMDS_NUMBER_RETRY = "IMDS_NUMBER_RETRY"
RunInContainer = "RUN_IN_CONTAINER"
RunInAWS = "RUN_IN_AWS"
RunWithIRSA = "RUN_WITH_IRSA"
UseDefaultConfig = "USE_DEFAULT_CONFIG"
HostName = "HOST_NAME"
PodName = "POD_NAME"
HostIP = "HOST_IP"
CWConfigContent = "CW_CONFIG_CONTENT"
)

const (
// TrueValue is the expected string set on an environment variable to indicate true.
TrueValue = "True"
)

var (
Expand All @@ -40,3 +53,7 @@ func IsUsageDataEnabled() bool {
})
return usageDataEnabled
}

func IsRunningInContainer() bool {
return os.Getenv(RunInContainer) == TrueValue
}
10 changes: 10 additions & 0 deletions cfg/envconfig/envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ func TestIsUsageDataEnabled(t *testing.T) {
t.Setenv(CWAGENT_USAGE_DATA, "FALSE")
assert.False(t, getUsageDataEnabled())
}

func TestIsRunningInContainer(t *testing.T) {
assert.False(t, IsRunningInContainer())

t.Setenv(RunInContainer, "TRUE")
assert.False(t, IsRunningInContainer())

t.Setenv(RunInContainer, TrueValue)
assert.True(t, IsRunningInContainer())
}
4 changes: 2 additions & 2 deletions extension/agenthealth/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ func (ah *agentHealth) Handlers() ([]awsmiddleware.RequestHandler, []awsmiddlewa
return requestHandlers, responseHandlers
}

func newAgentHealth(logger *zap.Logger, cfg *Config) (*agentHealth, error) {
return &agentHealth{logger: logger, cfg: cfg}, nil
func NewAgentHealth(logger *zap.Logger, cfg *Config) awsmiddleware.Extension {
return &agentHealth{logger: logger, cfg: cfg}
}
6 changes: 3 additions & 3 deletions extension/agenthealth/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import (

func TestExtension(t *testing.T) {
ctx := context.Background()
extension, err := newAgentHealth(zap.NewNop(), &Config{IsUsageDataEnabled: true})
assert.NoError(t, err)
cfg := &Config{IsUsageDataEnabled: true}
extension := NewAgentHealth(zap.NewNop(), cfg)
assert.NotNil(t, extension)
assert.NoError(t, extension.Start(ctx, componenttest.NewNopHost()))
requestHandlers, responseHandlers := extension.Handlers()
// user agent, client stats, stats
assert.Len(t, requestHandlers, 3)
// client stats
assert.Len(t, responseHandlers, 1)
extension.cfg.IsUsageDataEnabled = false
cfg.IsUsageDataEnabled = false
requestHandlers, responseHandlers = extension.Handlers()
// user agent
assert.Len(t, requestHandlers, 1)
Expand Down
2 changes: 1 addition & 1 deletion extension/agenthealth/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ func createDefaultConfig() component.Config {
}

func createExtension(_ context.Context, settings extension.CreateSettings, cfg component.Config) (extension.Extension, error) {
return newAgentHealth(settings.Logger, cfg.(*Config))
return NewAgentHealth(settings.Logger, cfg.(*Config)), nil
}
3 changes: 3 additions & 0 deletions extension/agenthealth/handler/stats/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Stats struct {
ImdsFallbackSucceed *int `json:"ifs,omitempty"`
AppSignals *int `json:"as,omitempty"`
EnhancedContainerInsights *int `json:"eci,omitempty"`
RunningInContainer *int `json:"ric,omitempty"`
RegionType *string `json:"rt,omitempty"`
Mode *string `json:"m,omitempty"`
}

// Merge the other Stats into the current. If the field is not nil,
Expand Down
60 changes: 50 additions & 10 deletions extension/agenthealth/handler/stats/provider/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@ import (

"github.com/aws/aws-sdk-go/aws"

"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/stats/agent"
)

const (
flagGetInterval = 5 * time.Minute
)

type Flag int
type BoolFlag int

const (
FlagIMDSFallbackSucceed = iota
FlagIMDSFallbackSucceed BoolFlag = iota
FlagSharedConfigFallback
FlagAppSignal
FlagEnhancedContainerInsights
FlagRunningInContainer
)

type StringFlag int

const (
FlagMode StringFlag = iota
FlagRegionType
)

var (
Expand All @@ -32,7 +41,8 @@ var (

type FlagStats interface {
agent.StatsProvider
SetFlag(flag Flag)
SetFlag(flag BoolFlag)
SetFlagWithValue(flag StringFlag, value string)
}

type flagStats struct {
Expand All @@ -47,31 +57,61 @@ func (p *flagStats) update() {
p.mu.Lock()
defer p.mu.Unlock()
p.stats = agent.Stats{
ImdsFallbackSucceed: p.getFlag(FlagIMDSFallbackSucceed),
SharedConfigFallback: p.getFlag(FlagSharedConfigFallback),
AppSignals: p.getFlag(FlagAppSignal),
EnhancedContainerInsights: p.getFlag(FlagEnhancedContainerInsights),
ImdsFallbackSucceed: p.getIntFlag(FlagIMDSFallbackSucceed, false),
SharedConfigFallback: p.getIntFlag(FlagSharedConfigFallback, false),
AppSignals: p.getIntFlag(FlagAppSignal, false),
EnhancedContainerInsights: p.getIntFlag(FlagEnhancedContainerInsights, false),
RunningInContainer: p.getIntFlag(FlagRunningInContainer, true),
Mode: p.getStringFlag(FlagMode),
RegionType: p.getStringFlag(FlagRegionType),
}
}

func (p *flagStats) getFlag(flag Flag) *int {
func (p *flagStats) getIntFlag(flag BoolFlag, missingAsZero bool) *int {
if _, ok := p.flags.Load(flag); ok {
return aws.Int(1)
}
if missingAsZero {
return aws.Int(0)
}
return nil
}

func (p *flagStats) SetFlag(flag Flag) {
func (p *flagStats) getStringFlag(flag StringFlag) *string {
value, ok := p.flags.Load(flag)
if !ok {
return nil
}
var str string
str, ok = value.(string)
if !ok {
return nil
}
return aws.String(str)
}

func (p *flagStats) SetFlag(flag BoolFlag) {
if _, ok := p.flags.Load(flag); !ok {
p.flags.Store(flag, true)
p.update()
}
}

func (p *flagStats) SetFlagWithValue(flag StringFlag, value string) {
if _, ok := p.flags.Load(flag); !ok {
p.flags.Store(flag, value)
p.update()
}
}

func newFlagStats(interval time.Duration) *flagStats {
return &flagStats{
stats := &flagStats{
intervalStats: newIntervalStats(interval),
}
if envconfig.IsRunningInContainer() {
stats.SetFlag(FlagRunningInContainer)
}
return stats
}

func GetFlagsStats() FlagStats {
Expand Down
9 changes: 9 additions & 0 deletions extension/agenthealth/handler/stats/provider/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
"time"

"github.com/stretchr/testify/assert"

"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
)

func TestFlagStats(t *testing.T) {
t.Setenv(envconfig.RunInContainer, envconfig.TrueValue)
provider := newFlagStats(time.Microsecond)
got := provider.stats
assert.Nil(t, got.ImdsFallbackSucceed)
assert.Nil(t, got.SharedConfigFallback)
assert.NotNil(t, got.RunningInContainer)
assert.Equal(t, 1, *got.RunningInContainer)
provider.SetFlag(FlagIMDSFallbackSucceed)
assert.Nil(t, got.ImdsFallbackSucceed)
got = provider.stats
Expand All @@ -25,4 +30,8 @@ func TestFlagStats(t *testing.T) {
got = provider.stats
assert.NotNil(t, got.SharedConfigFallback)
assert.Equal(t, 1, *got.SharedConfigFallback)
provider.SetFlagWithValue(FlagMode, "test")
got = provider.stats
assert.NotNil(t, got.Mode)
assert.Equal(t, "test", *got.Mode)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ replace github.com/openshift/api v3.9.0+incompatible => github.com/openshift/api
require (
github.com/BurntSushi/toml v1.3.2
github.com/Jeffail/gabs v1.4.0
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20231023161526-9bd8785e9c2e
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20231023230448-f645697bf350
github.com/aws/aws-sdk-go v1.45.24
github.com/aws/aws-sdk-go-v2 v1.21.2
github.com/aws/aws-sdk-go-v2/config v1.18.25
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awsemfex
github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awsemfexporter v0.0.0-20231023161526-9bd8785e9c2e/go.mod h1:UAXcRSojI8I0Kb9iS9a2v7J/iPrQ1loJIsBprSaVdFo=
github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awsxrayexporter v0.0.0-20231023161526-9bd8785e9c2e h1:wwkcWoKzZM1S92tSxSRxraQcUsJk+CS8UsKUR5Wcgow=
github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awsxrayexporter v0.0.0-20231023161526-9bd8785e9c2e/go.mod h1:cr4dmBlfnMVYT+gyKUAKh39zQu5u/UAukxQj15MdZ18=
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20231023161526-9bd8785e9c2e h1:tlA6NWgE+cKvBErHLl+oDAYG+oJ3TJV+N6jeJnYr5iI=
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20231023161526-9bd8785e9c2e/go.mod h1:uOQa5/9Jle9VADEdWCXL4AbJr35NJQil30tapcTHQlw=
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20231023230448-f645697bf350 h1:+75XAqf0Og8cshAdekRcqWf3v38Uw34XJRFbul6jbv0=
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20231023230448-f645697bf350/go.mod h1:uOQa5/9Jle9VADEdWCXL4AbJr35NJQil30tapcTHQlw=
github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/awsutil v0.0.0-20231023161526-9bd8785e9c2e h1:uQk5BvFVNMNCQswMx3gilWwPiqikvWx3BBFwMs85Stw=
github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/awsutil v0.0.0-20231023161526-9bd8785e9c2e/go.mod h1:9iAsO2SC8NIsa8/xCmC2Pj4MZPmYdvm+1/n89M74JS4=
github.com/amazon-contributing/opentelemetry-collector-contrib/internal/aws/containerinsight v0.0.0-20231023161526-9bd8785e9c2e h1:FvMVzM0uAQmE1lPdKdmSCPpZnE0O3yg14J2oMwrBXt0=
Expand Down
18 changes: 10 additions & 8 deletions plugins/outputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

"github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudwatch"
Expand All @@ -22,11 +23,12 @@ import (
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
"golang.org/x/exp/maps"

configaws "github.com/aws/amazon-cloudwatch-agent/cfg/aws"
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/stats/provider"
"github.com/aws/amazon-cloudwatch-agent/handlers"
"github.com/aws/amazon-cloudwatch-agent/handlers/agentinfo"
"github.com/aws/amazon-cloudwatch-agent/internal/publisher"
"github.com/aws/amazon-cloudwatch-agent/internal/retryer"
"github.com/aws/amazon-cloudwatch-agent/internal/util/collections"
Expand Down Expand Up @@ -54,6 +56,7 @@ const (

type CloudWatch struct {
config *Config
logger *zap.Logger
svc cloudwatchiface.CloudWatchAPI
// todo: may want to increase the size of the chan since the type changed.
// 1 telegraf Metric could have many Fields.
Expand All @@ -69,7 +72,6 @@ type CloudWatch struct {
aggregator Aggregator
aggregatorShutdownChan chan struct{}
aggregatorWaitGroup sync.WaitGroup
agentInfo agentinfo.AgentInfo
lastRequestBytes int
}

Expand All @@ -81,7 +83,6 @@ func (c *CloudWatch) Capabilities() consumer.Capabilities {
}

func (c *CloudWatch) Start(_ context.Context, host component.Host) error {
c.agentInfo = agentinfo.New("", c.config.RegionType, c.config.Mode)
c.publisher, _ = publisher.NewPublisher(
publisher.NewNonBlockingFifoQueue(metricChanBufferSize),
maxConcurrentPublisher,
Expand All @@ -96,6 +97,8 @@ func (c *CloudWatch) Start(_ context.Context, host component.Host) error {
Filename: c.config.SharedCredentialFilename,
Token: c.config.Token,
}
provider.GetFlagsStats().SetFlagWithValue(provider.FlagRegionType, c.config.RegionType)
provider.GetFlagsStats().SetFlagWithValue(provider.FlagMode, c.config.Mode)
configProvider := credentialConfig.Credentials()
logger := models.NewLogger("outputs", "cloudwatch", "")
logThrottleRetryer := retryer.NewLogThrottleRetryer(logger)
Expand All @@ -108,8 +111,9 @@ func (c *CloudWatch) Start(_ context.Context, host component.Host) error {
Logger: configaws.SDKLogger{},
})
svc.Handlers.Build.PushBackNamed(handlers.NewRequestCompressionHandler([]string{opPutLogEvents, opPutMetricData}))
svc.Handlers.Build.PushBackNamed(handlers.NewCustomHeaderHandler("User-Agent", c.agentInfo.UserAgent()))
svc.Handlers.Build.PushBackNamed(handlers.NewDynamicCustomHeaderHandler("X-Amz-Agent-Stats", c.agentInfo.StatsHeader))
if c.config.MiddlewareID != nil {
awsmiddleware.TryConfigure(c.logger, host, *c.config.MiddlewareID, awsmiddleware.SDKv1(&svc.Handlers))
}
//Format unique roll up list
c.config.RollupDimensions = GetUniqueRollupList(c.config.RollupDimensions)
c.svc = svc
Expand Down Expand Up @@ -267,7 +271,7 @@ func (c *CloudWatch) publish() {
if !bufferFullOccurred {
// Set to true so this only happens once per push.
bufferFullOccurred = true
// Keep interval above above 1 second.
// Keep interval above 1 second.
if currentInterval.Seconds() > 1 {
currentInterval /= 2
if currentInterval.Seconds() < 1 {
Expand Down Expand Up @@ -341,9 +345,7 @@ func (c *CloudWatch) WriteToCloudWatch(req interface{}) {
}
var err error
for i := 0; i < defaultRetryCount; i++ {
startTime := time.Now()
_, err = c.svc.PutMetricData(params)
c.agentInfo.RecordOpData(time.Since(startTime), c.lastRequestBytes, err)
if err != nil {
awsErr, ok := err.(awserr.Error)
if !ok {
Expand Down
Loading