-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
934 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package agent | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/aws/amazon-cloudwatch-agent/internal/util/collections" | ||
) | ||
|
||
const ( | ||
AllowAllOperations = "*" | ||
) | ||
|
||
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"` | ||
} | ||
|
||
// Merge the other Stats into the current. If the field is not nil, | ||
// then it'll overwrite the existing one. | ||
func (s *Stats) Merge(other Stats) { | ||
if other.CpuPercent != nil { | ||
s.CpuPercent = other.CpuPercent | ||
} | ||
if other.MemoryBytes != nil { | ||
s.MemoryBytes = other.MemoryBytes | ||
} | ||
if other.FileDescriptorCount != nil { | ||
s.FileDescriptorCount = other.FileDescriptorCount | ||
} | ||
if other.ThreadCount != nil { | ||
s.ThreadCount = other.ThreadCount | ||
} | ||
if other.LatencyMillis != nil { | ||
s.LatencyMillis = other.LatencyMillis | ||
} | ||
if other.PayloadBytes != nil { | ||
s.PayloadBytes = other.PayloadBytes | ||
} | ||
if other.StatusCode != nil { | ||
s.StatusCode = other.StatusCode | ||
} | ||
if other.SharedConfigFallback != nil { | ||
s.SharedConfigFallback = other.SharedConfigFallback | ||
} | ||
if other.ImdsFallbackSucceed != nil { | ||
s.ImdsFallbackSucceed = other.ImdsFallbackSucceed | ||
} | ||
if other.AppSignals != nil { | ||
s.AppSignals = other.AppSignals | ||
} | ||
if other.EnhancedContainerInsights != nil { | ||
s.EnhancedContainerInsights = other.EnhancedContainerInsights | ||
} | ||
} | ||
|
||
func (s *Stats) Marshal() (string, error) { | ||
raw, err := json.Marshal(s) | ||
if err != nil { | ||
return "", err | ||
} | ||
content := strings.TrimPrefix(string(raw), "{") | ||
return strings.TrimSuffix(content, "}"), nil | ||
} | ||
|
||
type StatsProvider interface { | ||
Stats(operation string) Stats | ||
} | ||
|
||
type OperationsFilter struct { | ||
operations collections.Set[string] | ||
allowAll bool | ||
} | ||
|
||
func (of OperationsFilter) IsAllowed(operationName string) bool { | ||
return of.allowAll || of.operations.Contains(operationName) | ||
} | ||
|
||
func NewOperationsFilter(operations ...string) OperationsFilter { | ||
allowed := collections.NewSet[string](operations...) | ||
return OperationsFilter{ | ||
operations: allowed, | ||
allowAll: allowed.Contains(AllowAllOperations), | ||
} | ||
} | ||
|
||
type StatsConfig struct { | ||
// Operations are the allowed operation names to gather stats for. | ||
Operations []string `mapstructure:"operations,omitempty"` | ||
} |
106 changes: 106 additions & 0 deletions
106
extension/agenthealth/handler/stats/agent/agent_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package agent | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMerge(t *testing.T) { | ||
stats := &Stats{CpuPercent: aws.Float64(1.2)} | ||
assert.EqualValues(t, 1.2, *stats.CpuPercent) | ||
assert.Nil(t, stats.MemoryBytes) | ||
stats.Merge(Stats{ | ||
CpuPercent: aws.Float64(1.3), | ||
MemoryBytes: aws.Uint64(123), | ||
}) | ||
assert.EqualValues(t, 1.3, *stats.CpuPercent) | ||
assert.EqualValues(t, 123, *stats.MemoryBytes) | ||
stats.Merge(Stats{ | ||
FileDescriptorCount: aws.Int32(456), | ||
ThreadCount: aws.Int32(789), | ||
LatencyMillis: aws.Int64(1234), | ||
PayloadBytes: aws.Int(5678), | ||
StatusCode: aws.Int(200), | ||
ImdsFallbackSucceed: aws.Int(1), | ||
SharedConfigFallback: aws.Int(1), | ||
AppSignals: aws.Int(1), | ||
EnhancedContainerInsights: aws.Int(1), | ||
}) | ||
assert.EqualValues(t, 1.3, *stats.CpuPercent) | ||
assert.EqualValues(t, 123, *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) | ||
} | ||
|
||
func TestMarshal(t *testing.T) { | ||
testCases := map[string]struct { | ||
stats *Stats | ||
want string | ||
}{ | ||
"WithEmpty": { | ||
stats: &Stats{}, | ||
want: "", | ||
}, | ||
"WithPartial": { | ||
stats: &Stats{ | ||
CpuPercent: aws.Float64(1.2), | ||
MemoryBytes: aws.Uint64(123), | ||
ThreadCount: aws.Int32(789), | ||
PayloadBytes: aws.Int(5678), | ||
}, | ||
want: `"cpu":1.2,"mem":123,"th":789,"load":5678`, | ||
}, | ||
"WithFull": { | ||
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), | ||
}, | ||
want: `"cpu":1.2,"mem":123,"fd":456,"th":789,"lat":1234,"load":5678,"code":200,"ifs":1`, | ||
}, | ||
} | ||
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) | ||
}) | ||
} | ||
} | ||
|
||
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)) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.