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

Adding Statuscode Handler to Agent Health Extension #1423

Open
wants to merge 98 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
2348639
fixing issue
Paramadon Nov 12, 2024
b7fac5e
trying to fix logs
Paramadon Nov 12, 2024
45536b8
fixing issue
Paramadon Nov 12, 2024
75b024a
adding debug statements (swapping logger with log.print)
Paramadon Nov 13, 2024
b41de09
adding more log statments
Paramadon Nov 13, 2024
90a22a3
fixing tags
Paramadon Nov 13, 2024
8c6d037
fixing logs
Paramadon Nov 13, 2024
6ff99f8
fixing logs
Paramadon Nov 13, 2024
667f608
adding a log
Paramadon Nov 13, 2024
9de9448
fixing stats function for statuscodehandler
Paramadon Nov 13, 2024
1e690e3
fixing issue
Paramadon Nov 13, 2024
4c6adcf
making operation names shorter
Paramadon Nov 13, 2024
dc129c9
Last commit works to reduce name- this one is for filtering operation…
Paramadon Nov 13, 2024
07a06fa
adding status code only filter
Paramadon Nov 13, 2024
0f76726
removing status code from otel config
Paramadon Nov 13, 2024
965ccdb
had to add return statments to filter
Paramadon Nov 13, 2024
a8268fd
commenting out try configure for ec2tagger
Paramadon Nov 13, 2024
8ddb463
adding ecw client for ec2 tagger
Paramadon Nov 13, 2024
3c15e81
changing the middleware to metricID
Paramadon Nov 13, 2024
947b8f3
trying to get stat
Paramadon Nov 14, 2024
a76ef49
everything works just removing some debug statement and filter from h…
Paramadon Nov 15, 2024
abf484b
fixing filter issue
Paramadon Nov 15, 2024
c849f61
previous commit works, just cleaning up debug statemtns
Paramadon Nov 15, 2024
bd33dbc
adding unit tests
Paramadon Nov 15, 2024
822680f
fixing test
Paramadon Nov 19, 2024
d547e1c
fixing unit tests
Paramadon Nov 19, 2024
5dc40f4
running make fmt
Paramadon Nov 19, 2024
e5bf295
fixing unit tests
Paramadon Nov 19, 2024
e07463a
fixing tests
Paramadon Nov 19, 2024
a5ba52e
fixing yamls
Paramadon Nov 19, 2024
f3c8d46
fixing yamls
Paramadon Nov 19, 2024
f64b332
removing internal changes
Paramadon Nov 19, 2024
fa4c163
restoring files
Paramadon Nov 19, 2024
457e48c
removing unnecessary file changes and passing unit test
Paramadon Nov 19, 2024
2705e87
cleaning up code
Paramadon Nov 19, 2024
3083626
fixing up pointer issue and passing in operations
Paramadon Nov 20, 2024
9e40bd8
adding agent health to prometheus
Paramadon Nov 20, 2024
5745ee6
resolving comments
Paramadon Nov 22, 2024
6e0fdd3
resolving comments
Paramadon Nov 22, 2024
8aee0d9
adding tests
Paramadon Nov 22, 2024
f344518
resolving comments
Paramadon Nov 22, 2024
a7e0415
resolving comments
Paramadon Nov 22, 2024
d91706d
adding tests
Paramadon Nov 22, 2024
59f735f
fixing race condition
Paramadon Nov 25, 2024
ebd92bd
fixing formats
Paramadon Nov 25, 2024
44c4006
fixing tests
Paramadon Nov 25, 2024
ecb4be5
removing log statements
Paramadon Nov 25, 2024
dfa3d4b
fixed names
Paramadon Nov 25, 2024
39e3a55
Merge branch 'main' of github.com:aws/amazon-cloudwatch-agent into Co…
Paramadon Nov 26, 2024
b589993
temp save
Paramadon Dec 2, 2024
0e3b86b
This fixes issues with pr and makes provider singleton instead of han…
Paramadon Dec 4, 2024
b14a43c
fixing lint
Paramadon Dec 4, 2024
14528bd
fixing lint
Paramadon Dec 4, 2024
e9e062d
Merge branch 'main' of github.com:aws/amazon-cloudwatch-agent into Co…
Paramadon Dec 4, 2024
cd78e5e
fixing issue
Paramadon Dec 4, 2024
aff4bc5
fixing lint
Paramadon Dec 4, 2024
7d31af3
fixing make test
Paramadon Dec 4, 2024
aed19c9
restoring internal folder
Paramadon Dec 4, 2024
6ee61b5
adding Describe Tasts
Paramadon Dec 4, 2024
a8ee7e3
adding to api status codes
Paramadon Dec 5, 2024
1887230
fixing unit tests
Paramadon Dec 5, 2024
1e31fb5
restring files and fixing lint
Paramadon Dec 5, 2024
188955e
fixing unit tests and lint
Paramadon Dec 5, 2024
424b762
restoring test data
Paramadon Dec 5, 2024
25e5ffd
fixing issue
Paramadon Dec 5, 2024
6a48268
adding singleton unit test
Paramadon Dec 5, 2024
af57c05
moving map
Paramadon Dec 5, 2024
5d3b20d
fixing channels
Paramadon Dec 5, 2024
471842c
removing random files
Paramadon Dec 5, 2024
c275271
moving filter to handler
Paramadon Dec 5, 2024
344db54
Merge branch 'main' into CodeHandler
Paramadon Dec 5, 2024
c31465f
restoring files
Paramadon Dec 5, 2024
48e879f
merging CodeHandler
Paramadon Dec 5, 2024
ed0313d
removing logs
Paramadon Dec 5, 2024
7fb22ef
fixing op filter
Paramadon Dec 5, 2024
106a329
changing parameter name
Paramadon Dec 5, 2024
8db2db6
Merge branch 'CodeHandler' of github.com:aws/amazon-cloudwatch-agent …
Paramadon Dec 6, 2024
aff8062
adding assume role
Paramadon Dec 6, 2024
3017b84
resolving comments
Paramadon Dec 6, 2024
93ef867
fixing race
Paramadon Dec 10, 2024
504eabe
fixng tlx
Paramadon Dec 10, 2024
a900640
removing unnecessary logs
Paramadon Dec 10, 2024
91920ea
fixing lint
Paramadon Dec 10, 2024
5cacf23
shouldn't add to stats map if ShouldResetStats is false
Paramadon Dec 11, 2024
97e82e1
moving around functions
Paramadon Dec 11, 2024
c061e3d
moving around functions
Paramadon Dec 11, 2024
9023e49
resolving comments
Paramadon Dec 11, 2024
94a4bd0
pushing changes
Paramadon Dec 11, 2024
acfde6b
removing unecessar tls files
Paramadon Dec 11, 2024
d321723
fixing lint
Paramadon Dec 11, 2024
c0d46b7
Merge branch 'main' into CodeHandler
Paramadon Dec 11, 2024
b3ca2b9
reverting a json
Paramadon Dec 11, 2024
e2b75bf
Merge branch 'CodeHandler' of github.com:aws/amazon-cloudwatch-agent …
Paramadon Dec 11, 2024
381bd53
Improved performance
Paramadon Dec 11, 2024
402743b
Improving performance
Paramadon Dec 12, 2024
6dc1193
Adding race
Paramadon Dec 12, 2024
bb73b8f
fixing ling
Paramadon Dec 12, 2024
1eae32a
fixing lint
Paramadon Dec 12, 2024
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
1 change: 0 additions & 1 deletion cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ func runAgent(ctx context.Context,
c.OutputFilters = outputFilters
c.InputFilters = inputFilters
c.AllowUnusedFields = true

err = loadTomlConfigIntoAgent(c)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions extension/agenthealth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type Config struct {
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
IsUsageDataEnabled bool `mapstructure:"is_usage_data_enabled"`
Stats agent.StatsConfig `mapstructure:"stats"`
StatusCodeOnly *bool `mapstructure:"is_status_code_only,omitempty"`
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
}

var _ component.Config = (*Config)(nil)
17 changes: 13 additions & 4 deletions extension/agenthealth/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@ var _ awsmiddleware.Extension = (*agentHealth)(nil)
func (ah *agentHealth) Handlers() ([]awsmiddleware.RequestHandler, []awsmiddleware.ResponseHandler) {
var responseHandlers []awsmiddleware.ResponseHandler
requestHandlers := []awsmiddleware.RequestHandler{useragent.NewHandler(ah.cfg.IsUsageDataEnabled)}
if ah.cfg.IsUsageDataEnabled {
req, res := stats.NewHandlers(ah.logger, ah.cfg.Stats)
requestHandlers = append(requestHandlers, req...)
responseHandlers = append(responseHandlers, res...)

if !ah.cfg.IsUsageDataEnabled {
ah.logger.Debug("Usage data is disabled, skipping stats handlers")
return requestHandlers, responseHandlers
}

statusCodeOnly := false
if ah.cfg.StatusCodeOnly != nil {
statusCodeOnly = *ah.cfg.StatusCodeOnly
}
statsRequestHandlers, statsResponseHandlers := stats.NewHandlers(ah.logger, ah.cfg.Stats, statusCodeOnly)
requestHandlers = append(requestHandlers, statsRequestHandlers...)
responseHandlers = append(responseHandlers, statsResponseHandlers...)

return requestHandlers, responseHandlers
}

Expand Down
2 changes: 1 addition & 1 deletion extension/agenthealth/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestExtension(t *testing.T) {
assert.NoError(t, extension.Start(ctx, componenttest.NewNopHost()))
requestHandlers, responseHandlers := extension.Handlers()
// user agent, client stats, stats
assert.Len(t, requestHandlers, 3)
assert.Len(t, requestHandlers, 4)
// client stats
assert.Len(t, responseHandlers, 1)
cfg.IsUsageDataEnabled = false
Expand Down
107 changes: 92 additions & 15 deletions extension/agenthealth/handler/stats/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package agent

import (
"encoding/json"
"log"
"strings"

"github.com/aws/amazon-cloudwatch-agent/internal/util/collections"
Expand All @@ -15,21 +16,22 @@ const (
)

type Stats 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"`
SharedConfigFallback *int `json:"scfb,omitempty"`
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"`
EntityRejected *int `json:"ent,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"`
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"`
EntityRejected *int `json:"ent,omitempty"`
StatusCodes map[string][5]int `json:"codes,omitempty"` //represents status codes 200,400,408,413,429,
}

// Merge the other Stats into the current. If the field is not nil,
Expand Down Expand Up @@ -80,6 +82,45 @@ func (s *Stats) Merge(other Stats) {
if other.EntityRejected != nil {
s.EntityRejected = other.EntityRejected
}
if other.StatusCodes != nil {
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
log.Println("Merging status codes from another source.")
Paramadon marked this conversation as resolved.
Show resolved Hide resolved

if s.StatusCodes == nil {
log.Println("Initializing status codes map as it was nil.")
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
s.StatusCodes = make(map[string][5]int)
}

for key, value := range other.StatusCodes {
log.Printf("Processing key: %s with value: 200=%d, 400=%d, 408=%d, 413=%d, 429=%d", key, value[0], value[1], value[2], value[3], value[4])

if existing, ok := s.StatusCodes[key]; ok {
log.Printf(
"Key %s already exists. Existing: 200=%d, 400=%d, 408=%d, 413=%d, 429=%d. Merging with: 200=%d, 400=%d, 408=%d, 413=%d, 429=%d",
key, existing[0], existing[1], existing[2], existing[3], existing[4],
value[0], value[1], value[2], value[3], value[4],
)

//Merge the values for each status code
s.StatusCodes[key] = [5]int{
existing[0] + value[0], // 200
existing[1] + value[1], // 400
existing[2] + value[2], // 408
existing[3] + value[3], // 413
existing[4] + value[4], // 429
}

log.Printf(
"Updated key %s: 200=%d, 400=%d, 408=%d, 413=%d, 429=%d",
key, s.StatusCodes[key][0], s.StatusCodes[key][1], s.StatusCodes[key][2], s.StatusCodes[key][3], s.StatusCodes[key][4],
)
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
} else {
log.Printf("Key %s does not exist. Adding it with: 200=%d, 400=%d, 408=%d, 413=%d, 429=%d", key, value[0], value[1], value[2], value[3], value[4])
s.StatusCodes[key] = value
}
}
log.Println("Merging of status codes completed.")
}

}

func (s *Stats) Marshal() (string, error) {
Expand Down Expand Up @@ -118,3 +159,39 @@ type StatsConfig struct {
// UsageFlags are the usage flags to set on start up.
UsageFlags map[Flag]any `mapstructure:"usage_flags,omitempty"`
}

var StatusCodeOperations = []string{ // all the operations that are allowed
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
"DescribeInstances",
"DescribeTags",
"DescribeVolumes",
"DescribeContainerInstances",
"DescribeServices",
"DescribeTaskDefinition",
"ListServices",
"ListTasks",
"CreateLogGroup",
"CreateLogStream",
}

// NewStatusCodeOperationsFilter creates a new filter for allowed operations and status codes.
func NewStatusCodeOperationsFilter() OperationsFilter {
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
allowed := collections.NewSet[string](StatusCodeOperations...)
Paramadon marked this conversation as resolved.
Show resolved Hide resolved

return OperationsFilter{
operations: allowed,
allowAll: allowed.Contains(AllowAllOperations),
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
}
}

func NewStatusCodeAndOtherOperationsFilter(operations []string) OperationsFilter {
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
allowed := collections.NewSet[string](StatusCodeOperations...)

for _, operation := range operations {
allowed.Add(operation)
}

return OperationsFilter{
operations: allowed,
allowAll: allowed.Contains(AllowAllOperations),
}
}
117 changes: 42 additions & 75 deletions extension/agenthealth/handler/stats/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,105 +10,72 @@ import (
"github.com/stretchr/testify/assert"
)

func TestMerge(t *testing.T) {
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
stats := &Stats{CpuPercent: aws.Float64(1.2)}
assert.EqualValues(t, 1.2, *stats.CpuPercent)
assert.Nil(t, stats.MemoryBytes)
func TestMergeWithStatusCodes(t *testing.T) {
stats := &Stats{
StatusCodes: map[string][5]int{
"operation1": {1, 2, 3, 4, 5},
},
}

stats.Merge(Stats{
CpuPercent: aws.Float64(1.3),
MemoryBytes: aws.Uint64(123),
StatusCodes: map[string][5]int{
"operation1": {2, 3, 4, 5, 6}, // Existing operation with new values
"operation2": {0, 1, 2, 3, 4}, // New operation
},
})
assert.EqualValues(t, 1.3, *stats.CpuPercent)
assert.EqualValues(t, 123, *stats.MemoryBytes)

assert.Equal(t, [5]int{3, 5, 7, 9, 11}, stats.StatusCodes["operation1"]) // Values should sum
assert.Equal(t, [5]int{0, 1, 2, 3, 4}, stats.StatusCodes["operation2"]) // New operation added

stats.Merge(Stats{
CpuPercent: aws.Float64(1.5),
MemoryBytes: aws.Uint64(133),
FileDescriptorCount: aws.Int32(456),
ThreadCount: aws.Int32(789),
LatencyMillis: aws.Int64(1234),
PayloadBytes: aws.Int(5678),
StatusCode: aws.Int(200),
SharedConfigFallback: aws.Int(1),
ImdsFallbackSucceed: aws.Int(1),
AppSignals: aws.Int(1),
EnhancedContainerInsights: aws.Int(1),
RunningInContainer: aws.Int(0),
RegionType: aws.String("RegionType"),
Mode: aws.String("Mode"),
StatusCodes: nil,
})
assert.EqualValues(t, 1.5, *stats.CpuPercent)
assert.EqualValues(t, 133, *stats.MemoryBytes)
assert.EqualValues(t, 456, *stats.FileDescriptorCount)
assert.EqualValues(t, 789, *stats.ThreadCount)
assert.EqualValues(t, 1234, *stats.LatencyMillis)
assert.EqualValues(t, 5678, *stats.PayloadBytes)
assert.EqualValues(t, 200, *stats.StatusCode)
assert.EqualValues(t, 1, *stats.ImdsFallbackSucceed)
assert.EqualValues(t, 1, *stats.SharedConfigFallback)
assert.EqualValues(t, 1, *stats.AppSignals)
assert.EqualValues(t, 1, *stats.EnhancedContainerInsights)
assert.EqualValues(t, 0, *stats.RunningInContainer)
assert.EqualValues(t, "RegionType", *stats.RegionType)
assert.EqualValues(t, "Mode", *stats.Mode)

assert.Equal(t, [5]int{3, 5, 7, 9, 11}, stats.StatusCodes["operation1"])
assert.Equal(t, [5]int{0, 1, 2, 3, 4}, stats.StatusCodes["operation2"])
}

func TestMarshal(t *testing.T) {
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
func TestMarshalWithStatusCodes(t *testing.T) {
testCases := map[string]struct {
stats *Stats
want string
}{
"WithEmpty": {
stats: &Stats{},
want: "",
},
"WithPartial": {
"WithEmptyStatusCodes": {
stats: &Stats{
CpuPercent: aws.Float64(1.2),
MemoryBytes: aws.Uint64(123),
ThreadCount: aws.Int32(789),
PayloadBytes: aws.Int(5678),
StatusCodes: map[string][5]int{},
},
want: `"cpu":1.2,"mem":123,"th":789,"load":5678`,
want: "",
},
"WithFull": {
"WithStatusCodes": {
stats: &Stats{
CpuPercent: aws.Float64(1.2),
MemoryBytes: aws.Uint64(123),
FileDescriptorCount: aws.Int32(456),
ThreadCount: aws.Int32(789),
LatencyMillis: aws.Int64(1234),
PayloadBytes: aws.Int(5678),
StatusCode: aws.Int(200),
ImdsFallbackSucceed: aws.Int(1),
StatusCodes: map[string][5]int{
"operation1": {1, 2, 3, 4, 5},
"operation2": {0, 1, 2, 3, 4},
},
},
want: `"cpu":1.2,"mem":123,"fd":456,"th":789,"lat":1234,"load":5678,"code":200,"ifs":1`,
want: `"codes":{"operation1":[1,2,3,4,5],"operation2":[0,1,2,3,4]}`,
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
got, err := testCase.stats.Marshal()
assert.NoError(t, err)
assert.Equal(t, testCase.want, got)
assert.Contains(t, got, testCase.want)
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

func TestOperationFilter(t *testing.T) {
testCases := map[string]struct {
allowedOperations []string
testOperations []string
want []bool
}{
"WithNoneAllowed": {allowedOperations: nil, testOperations: []string{"nothing", "is", "allowed"}, want: []bool{false, false, false}},
"WithSomeAllowed": {allowedOperations: []string{"are"}, testOperations: []string{"some", "are", "allowed"}, want: []bool{false, true, false}},
"WithAllAllowed": {allowedOperations: []string{"*"}, testOperations: []string{"all", "are", "allowed"}, want: []bool{true, true, true}},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
filter := NewOperationsFilter(testCase.allowedOperations...)
for index, testOperation := range testCase.testOperations {
assert.Equal(t, testCase.want[index], filter.IsAllowed(testOperation))
}
})
func TestMergeFullWithStatusCodes(t *testing.T) {
stats := &Stats{
CpuPercent: aws.Float64(1.0),
StatusCodes: map[string][5]int{"operation1": {1, 0, 0, 0, 0}},
}
stats.Merge(Stats{
CpuPercent: aws.Float64(2.0),
StatusCodes: map[string][5]int{"operation1": {0, 1, 0, 0, 0}, "operation2": {1, 1, 1, 1, 1}},
})

assert.Equal(t, 2.0, *stats.CpuPercent)
assert.Equal(t, [5]int{1, 1, 0, 0, 0}, stats.StatusCodes["operation1"])
assert.Equal(t, [5]int{1, 1, 1, 1, 1}, stats.StatusCodes["operation2"])
}
21 changes: 17 additions & 4 deletions extension/agenthealth/handler/stats/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,25 @@ const (
headerKeyAgentStats = "X-Amz-Agent-Stats"
)

func NewHandlers(logger *zap.Logger, cfg agent.StatsConfig) ([]awsmiddleware.RequestHandler, []awsmiddleware.ResponseHandler) {
filter := agent.NewOperationsFilter(cfg.Operations...)
func NewHandlers(logger *zap.Logger, cfg agent.StatsConfig, statuscodeonly bool) ([]awsmiddleware.RequestHandler, []awsmiddleware.ResponseHandler) {
statusCodeFilter := agent.NewStatusCodeOperationsFilter()
if statuscodeonly {
statusCodeStats := provider.GetStatusCodeStats(statusCodeFilter)
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
return []awsmiddleware.RequestHandler{statusCodeStats}, []awsmiddleware.ResponseHandler{statusCodeStats}
}
filter := agent.NewStatusCodeAndOtherOperationsFilter(cfg.Operations)
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
clientStats := client.NewHandler(filter)
stats := newStatsHandler(logger, filter, []agent.StatsProvider{clientStats, provider.GetProcessStats(), provider.GetFlagsStats()})
statusCodeStats := provider.GetStatusCodeStats(statusCodeFilter)

stats := newStatsHandler(logger, filter, []agent.StatsProvider{
clientStats,
provider.GetProcessStats(),
provider.GetFlagsStats(),
statusCodeStats,
})
agent.UsageFlags().SetValues(cfg.UsageFlags)
return []awsmiddleware.RequestHandler{stats, clientStats}, []awsmiddleware.ResponseHandler{clientStats}

return []awsmiddleware.RequestHandler{stats, clientStats, statusCodeStats}, []awsmiddleware.ResponseHandler{statusCodeStats}
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
}

type statsHandler struct {
Expand Down
15 changes: 6 additions & 9 deletions extension/agenthealth/handler/stats/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ func TestStatsHandler(t *testing.T) {
ThreadCount: aws.Int32(789),
LatencyMillis: aws.Int64(1234),
PayloadBytes: aws.Int(5678),
StatusCode: aws.Int(200),
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
ImdsFallbackSucceed: aws.Int(1),
SharedConfigFallback: aws.Int(1),
StatusCodes: map[string][5]int{
"pmd": {1, 0, 0, 0, 0},
"di": {0, 1, 0, 0, 0},
},
}
handler := newStatsHandler(
zap.NewNop(),
Expand All @@ -59,15 +62,9 @@ func TestStatsHandler(t *testing.T) {
assert.Equal(t, "", req.Header.Get(headerKeyAgentStats))
handler.filter = agent.NewOperationsFilter(agent.AllowAllOperations)
handler.HandleRequest(ctx, req)
assert.Equal(t, `"cpu":1.2,"mem":123,"fd":456,"th":789,"lat":1234,"load":5678,"code":200,"scfb":1,"ifs":1`, req.Header.Get(headerKeyAgentStats))
assert.Equal(t, `"cpu":1.2,"mem":123,"fd":456,"th":789,"lat":1234,"load":5678,"scfb":1,"ifs":1,"codes":{"di":[0,1,0,0,0],"pmd":[1,0,0,0,0]}`, req.Header.Get(headerKeyAgentStats))
stats.StatusCode = aws.Int(404)
stats.LatencyMillis = nil
handler.HandleRequest(ctx, req)
assert.Equal(t, `"cpu":1.2,"mem":123,"fd":456,"th":789,"load":5678,"code":404,"scfb":1,"ifs":1`, req.Header.Get(headerKeyAgentStats))
}

func TestNewHandlers(t *testing.T) {
Paramadon marked this conversation as resolved.
Show resolved Hide resolved
requestHandlers, responseHandlers := NewHandlers(zap.NewNop(), agent.StatsConfig{})
assert.Len(t, requestHandlers, 2)
assert.Len(t, responseHandlers, 1)
assert.Equal(t, `"cpu":1.2,"mem":123,"fd":456,"th":789,"load":5678,"code":404,"scfb":1,"ifs":1,"codes":{"di":[0,1,0,0,0],"pmd":[1,0,0,0,0]}`, req.Header.Get(headerKeyAgentStats))
}
Loading
Loading