Skip to content

Commit

Permalink
Add flag set options to replace existing agentinfo.
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien committed Oct 24, 2023
1 parent 0956f3c commit bf9d719
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 35 deletions.
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())
}
2 changes: 1 addition & 1 deletion extension/agenthealth/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestExtension(t *testing.T) {
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
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)
}
4 changes: 3 additions & 1 deletion plugins/outputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"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/internal/publisher"
"github.com/aws/amazon-cloudwatch-agent/internal/retryer"
Expand Down Expand Up @@ -82,7 +83,6 @@ func (c *CloudWatch) Capabilities() consumer.Capabilities {
}

func (c *CloudWatch) Start(_ context.Context, host component.Host) error {
// TODO: set c.RegionType and c.Mode in FlagsStats
c.publisher, _ = publisher.NewPublisher(
publisher.NewNonBlockingFifoQueue(metricChanBufferSize),
maxConcurrentPublisher,
Expand All @@ -97,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 Down
14 changes: 12 additions & 2 deletions plugins/outputs/cloudwatchlogs/cloudwatchlogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cloudwatchlogs
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"sync"
"time"
Expand All @@ -21,6 +22,8 @@ import (
configaws "github.com/aws/amazon-cloudwatch-agent/cfg/aws"
"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth"
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/stats/provider"
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/useragent"
"github.com/aws/amazon-cloudwatch-agent/handlers"
"github.com/aws/amazon-cloudwatch-agent/internal"
"github.com/aws/amazon-cloudwatch-agent/internal/retryer"
Expand All @@ -44,6 +47,10 @@ const (
attributesInFields = "attributesInFields"
)

var (
containerInsightsRegexp = regexp.MustCompile("^/aws/.*containerinsights/.*/(performance|prometheus)$")
)

type CloudWatchLogs struct {
Region string `toml:"region"`
RegionType string `toml:"region_type"`
Expand Down Expand Up @@ -139,8 +146,11 @@ func (c *CloudWatchLogs) getDest(t Target) *cwDest {
Logger: configaws.SDKLogger{},
},
)
// TODO: set c.RegionType and c.Mode in FlagsStats
// TODO: set containerinsights flag based on group
provider.GetFlagsStats().SetFlagWithValue(provider.FlagRegionType, c.RegionType)
provider.GetFlagsStats().SetFlagWithValue(provider.FlagMode, c.Mode)
if containerInsightsRegexp.MatchString(t.Group) {
useragent.Get().SetContainerInsightsFlag()
}
client.Handlers.Build.PushBackNamed(handlers.NewRequestCompressionHandler([]string{"PutLogEvents"}))
if c.middleware != nil {
if err := awsmiddleware.NewConfigurer(c.middleware.Handlers()).Configure(awsmiddleware.SDKv1(&client.Handlers)); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions plugins/processors/ec2tagger/ec2metadataprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/aws/aws-sdk-go/aws/ec2metadata"

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

Expand Down Expand Up @@ -52,7 +52,7 @@ func (c *metadataClient) InstanceID(ctx context.Context) (string, error) {
log.Printf("D! could not get instance id without imds v1 fallback enable thus enable fallback")
instanceInner, errorInner := c.metadataFallbackEnabled.GetMetadataWithContext(ctx, "instance-id")
if errorInner == nil {
agentinfo.SetImdsFallbackSucceed()
provider.GetFlagsStats().SetFlag(provider.FlagIMDSFallbackSucceed)
}
return instanceInner, errorInner
}
Expand All @@ -65,7 +65,7 @@ func (c *metadataClient) Hostname(ctx context.Context) (string, error) {
log.Printf("D! could not get hostname without imds v1 fallback enable thus enable fallback")
hostnameInner, errorInner := c.metadataFallbackEnabled.GetMetadataWithContext(ctx, "hostname")
if errorInner == nil {
agentinfo.SetImdsFallbackSucceed()
provider.GetFlagsStats().SetFlag(provider.FlagIMDSFallbackSucceed)
}
return hostnameInner, errorInner
}
Expand All @@ -78,7 +78,7 @@ func (c *metadataClient) Get(ctx context.Context) (ec2metadata.EC2InstanceIdenti
log.Printf("D! could not get instance document without imds v1 fallback enable thus enable fallback")
instanceDocumentInner, errorInner := c.metadataFallbackEnabled.GetInstanceIdentityDocumentWithContext(ctx)
if errorInner == nil {
agentinfo.SetImdsFallbackSucceed()
provider.GetFlagsStats().SetFlag(provider.FlagIMDSFallbackSucceed)
}
return instanceDocumentInner, errorInner
}
Expand Down
26 changes: 14 additions & 12 deletions translator/config/envconst.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@

package config

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

const (
RUN_IN_CONTAINER = "RUN_IN_CONTAINER"
RUN_IN_CONTAINER_TRUE = "True"
RUN_IN_AWS = "RUN_IN_AWS"
RUN_IN_AWS_TRUE = "True"
RUN_WITH_IRSA = "RUN_WITH_IRSA"
RUN_WITH_IRSA_TRUE = "True"
USE_DEFAULT_CONFIG = "USE_DEFAULT_CONFIG"
USE_DEFAULT_CONFIG_TRUE = "True"
HOST_NAME = "HOST_NAME"
POD_NAME = "POD_NAME"
HOST_IP = "HOST_IP"
CWConfigContent = "CW_CONFIG_CONTENT"
RUN_IN_CONTAINER = envconfig.RunInContainer
RUN_IN_CONTAINER_TRUE = envconfig.TrueValue
RUN_IN_AWS = envconfig.RunInAWS
RUN_IN_AWS_TRUE = envconfig.TrueValue
RUN_WITH_IRSA = envconfig.RunWithIRSA
RUN_WITH_IRSA_TRUE = envconfig.TrueValue
USE_DEFAULT_CONFIG = envconfig.UseDefaultConfig
USE_DEFAULT_CONFIG_TRUE = envconfig.TrueValue
HOST_NAME = envconfig.HostName
POD_NAME = envconfig.PodName
HOST_IP = envconfig.HostIP
CWConfigContent = envconfig.CWConfigContent
)
6 changes: 3 additions & 3 deletions translator/util/ec2util/ec2util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"

configaws "github.com/aws/amazon-cloudwatch-agent/cfg/aws"
"github.com/aws/amazon-cloudwatch-agent/handlers/agentinfo"
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/stats/provider"
"github.com/aws/amazon-cloudwatch-agent/internal/retryer"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
Expand Down Expand Up @@ -116,7 +116,7 @@ func (e *ec2Util) deriveEC2MetadataFromIMDS() error {
hostnameInner, errInner := mdEnableFallback.GetMetadata("hostname")
if errInner == nil {
e.Hostname = hostnameInner
agentinfo.SetImdsFallbackSucceed()
provider.GetFlagsStats().SetFlag(provider.FlagIMDSFallbackSucceed)
} else {
fmt.Println("E! [EC2] Fetch hostname from EC2 metadata fail:", errInner)
}
Expand All @@ -136,7 +136,7 @@ func (e *ec2Util) deriveEC2MetadataFromIMDS() error {
e.AccountID = instanceIdentityDocumentInner.AccountID
e.PrivateIP = instanceIdentityDocumentInner.PrivateIP
e.InstanceID = instanceIdentityDocumentInner.InstanceID
agentinfo.SetImdsFallbackSucceed()
provider.GetFlagsStats().SetFlag(provider.FlagIMDSFallbackSucceed)
} else {
fmt.Println("E! [EC2] Fetch identity document from EC2 metadata fail:", errInner)
}
Expand Down

0 comments on commit bf9d719

Please sign in to comment.