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

Add resource metric integration test #453

Open
wants to merge 65 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
3609cd7
Create config file
varunch77 Jan 13, 2025
1952125
Move config file
varunch77 Jan 13, 2025
edd5418
Add resource test
varunch77 Jan 13, 2025
80975df
Write logs
varunch77 Jan 13, 2025
02c34c2
Attempt at checking entity information in logs
varunch77 Jan 13, 2025
2f93774
Revert to validating logs
varunch77 Jan 14, 2025
a9c2c82
Pause deletion of logs for testing
varunch77 Jan 14, 2025
01c488a
Edit config file
varunch77 Jan 14, 2025
044f757
Revert config change
varunch77 Jan 14, 2025
5dbad82
Update config file again
varunch77 Jan 14, 2025
50e7764
Add memory info to config file
varunch77 Jan 14, 2025
dc78832
Reduce append dimensions in config
varunch77 Jan 14, 2025
8e71d15
Update config
varunch77 Jan 14, 2025
c763050
Revert config to minimal version
varunch77 Jan 14, 2025
49c83fa
Another config change
varunch77 Jan 14, 2025
db541b5
Change namespace
varunch77 Jan 14, 2025
5689ad1
Update agent config
varunch77 Jan 17, 2025
a937887
Use default namespace
varunch77 Jan 17, 2025
ad88aec
Merge branch 'main' into resource-metric-integ-test
varunch77 Jan 17, 2025
ce01fa6
Codify curl command
varunch77 Jan 17, 2025
31ea8fb
Unhardcode instance id
varunch77 Jan 17, 2025
b8a54e2
Codify curl command
varunch77 Jan 17, 2025
aa8f340
validate request response
varunch77 Jan 17, 2025
ac08ce5
print type of response
varunch77 Jan 17, 2025
17a05f2
print type
varunch77 Jan 17, 2025
92445d2
Validate request response
varunch77 Jan 17, 2025
cb15441
Add missing packages
varunch77 Jan 17, 2025
aeb11ba
Update packages
varunch77 Jan 17, 2025
7b412b1
Fix package
varunch77 Jan 17, 2025
2f07084
Fix small errors
varunch77 Jan 17, 2025
6e07fec
Add print statements
varunch77 Jan 17, 2025
2b75825
Add print statements
varunch77 Jan 17, 2025
8b61efd
Remove instanceType
varunch77 Jan 17, 2025
8f10cff
Edit comments
varunch77 Jan 17, 2025
8fa2b80
Merge branch 'main' into resource-metric-integ-test
varunch77 Jan 21, 2025
44fca67
Get rid of logs
varunch77 Jan 22, 2025
6907b5d
Move out of logs folder
varunch77 Jan 23, 2025
6738efd
Import fix
varunch77 Jan 23, 2025
5bca823
Fix copy path file
varunch77 Jan 23, 2025
fd57b84
Run linter
varunch77 Jan 23, 2025
43e16ac
use consts
varunch77 Jan 23, 2025
efaef1a
Remove config file copy
varunch77 Jan 23, 2025
d44c45c
Run linter
varunch77 Jan 23, 2025
ccff2e8
Print entity again
varunch77 Jan 23, 2025
1cedc10
Print entire response
varunch77 Jan 23, 2025
1bc7930
Print
varunch77 Jan 23, 2025
4b3de9f
print reponse
varunch77 Jan 23, 2025
d5bc869
print
varunch77 Jan 23, 2025
f4de16c
fully print out body
varunch77 Jan 23, 2025
023da57
print
varunch77 Jan 23, 2025
32c0502
print
varunch77 Jan 23, 2025
de05cbc
Use helper functions to validate entity
varunch77 Jan 23, 2025
bf32e2d
MCreate helper function for making api call
varunch77 Jan 23, 2025
2192042
Fix errors
varunch77 Jan 23, 2025
c91c12f
Add forgotten file
varunch77 Jan 23, 2025
1c5bba2
Run linter
varunch77 Jan 23, 2025
6101dfe
Use test case structure
varunch77 Jan 24, 2025
c160ac4
Run linter
varunch77 Jan 24, 2025
4106a3a
Remove old version of test function
varunch77 Jan 24, 2025
564f56a
Merge branch 'main' into resource-metric-integ-test
varunch77 Jan 24, 2025
19b1659
Add test to metric_value_benchmark suite
varunch77 Jan 24, 2025
2e1c551
remove unussed const
varunch77 Jan 24, 2025
14e278e
Update imports
varunch77 Jan 24, 2025
0b3ff93
fix import
varunch77 Jan 24, 2025
ae22da3
remove config file path
varunch77 Jan 24, 2025
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
105 changes: 105 additions & 0 deletions test/cloudwatchlogs/publish_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@
package cloudwatchlogs

import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"

v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -274,6 +282,103 @@ func TestLogGroupClass(t *testing.T) {
}
}

func TestResourceMetrics(t *testing.T) {
varunch77 marked this conversation as resolved.
Show resolved Hide resolved
instanceId := awsservice.GetInstanceId()
configPath := "resources/config_log_resource.json"
logFile, err := os.Create(logFilePath)
assert.NoError(t, err, "Error occurred creating log file for writing")

defer logFile.Close()
defer os.Remove(logFilePath)
defer awsservice.DeleteLogGroupAndStream(instanceId, instanceId)
varunch77 marked this conversation as resolved.
Show resolved Hide resolved

// start agent and write metrics and logs
common.CopyFile(configPath, configOutputPath)
common.StartAgent(configOutputPath, true, false)
time.Sleep(2 * time.Minute)
varunch77 marked this conversation as resolved.
Show resolved Hide resolved
writeLogLines(t, logFile, 100)
time.Sleep(2 * time.Minute)
common.StopAgent()

// this section builds, signs, and sends the request
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-west-2"))
varunch77 marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
signer := v4.NewSigner()

body := []byte(fmt.Sprintf(`{
"Namespace": "CWAgent",
"MetricName": "cpu_usage_idle",
"Dimensions": [
{"Name": "InstanceId", "Value": "%s"},
{"Name": "cpu", "Value": "cpu-total"}
]
}`, instanceId))

h := sha256.New()
h.Write(body)
payloadHash := hex.EncodeToString(h.Sum(nil))

// essentially trying to convert this curl command:

// curl -i -X POST monitoring.us-west-2.amazonaws.com -H 'Content-Type: application/json' \
// -H 'Content-Encoding: amz-1.0' \
// --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
// -H "x-amz-security-token: $AWS_SESSION_TOKEN" \
// --aws-sigv4 "aws:amz:us-west-2:monitoring" \
// -H 'X-Amz-Target: com.amazonaws.cloudwatch.v2013_01_16.CloudWatchVersion20130116.ListEntitiesForMetric' \
// -d '{
// "Namespace": "CWAgent",
// "MetricName": "cpu_usage_idle",
// "Dimensions": [{"Name": "InstanceId", "Value": "i-0123456789012"}, { "Name": "cpu", "Value": "cpu-total"}]
// }'

// build the request
req, err := http.NewRequest("POST", "https://monitoring.us-west-2.amazonaws.com/", bytes.NewReader(body))
varunch77 marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err, "Error creating request")

// set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Amz-Target", "com.amazonaws.cloudwatch.v2013_01_16.CloudWatchVersion20130116.ListEntitiesForMetric")
req.Header.Set("Content-Encoding", "amz-1.0")

// set creds
credentials, err := cfg.Credentials.Retrieve(context.TODO())
assert.NoError(t, err, "Error getting credentials")

req.Header.Set("x-amz-security-token", credentials.SessionToken)

// sign the request
err = signer.SignHTTP(context.TODO(), credentials, req, payloadHash, "monitoring", "us-west-2", time.Now())
assert.NoError(t, err, "Error signing the request")

// send the request
client := &http.Client{}
resp, err := client.Do(req)
assert.NoError(t, err, "Error sending the request")
defer resp.Body.Close()

// parse and verify the response
var response struct {
varunch77 marked this conversation as resolved.
Show resolved Hide resolved
Entities []struct {
KeyAttributes struct {
Type string `json:"Type"`
ResourceType string `json:"ResourceType"`
Identifier string `json:"Identifier"`
} `json:"KeyAttributes"`
} `json:"Entities"`
}

err = json.NewDecoder(resp.Body).Decode(&response)
assert.NoError(t, err, "Error parsing JSON response")

// Verify the KeyAttributes
assert.NotEmpty(t, response.Entities, "No entities found in the response")
entity := response.Entities[0]
assert.Equal(t, "AWS::Resource", entity.KeyAttributes.Type)
assert.Equal(t, "AWS::EC2::Instance", entity.KeyAttributes.ResourceType)
assert.Equal(t, instanceId, entity.KeyAttributes.Identifier)
}

func writeLogLines(t *testing.T, f *os.File, iterations int) {
log.Printf("Writing %d lines to %s", iterations*len(logLineIds), f.Name())

Expand Down
46 changes: 46 additions & 0 deletions test/cloudwatchlogs/resources/config_log_resource.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"agent": {
"metrics_collection_interval": 10,
"run_as_user": "root",
"debug": true
},
"metrics": {
"metrics_collected": {
"cpu": {
"resources": [
"*"
],
"measurement": [
"cpu_usage_idle",
"cpu_usage_nice",
"cpu_usage_guest"
],
"metrics_collection_interval": 10
},
"memory": {
"metrics_collection_interval": 10,
"measurement": [
"mem_used",
"mem_free"
]
}
},
"append_dimensions": {
"InstanceId": "${aws:InstanceId}"
}
},
"logs": {
varunch77 marked this conversation as resolved.
Show resolved Hide resolved
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/tmp/cwagent_log_test.log*",
"log_group_name": "{instance_id}",
"log_stream_name": "{instance_id}",
"timezone": "UTC"
}
]
}
}
}
}
2 changes: 1 addition & 1 deletion test/e2e/jmx/jmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
//------------------------------------------------------------------------------

var (
env *environment.MetaData
env *environment.MetaData
varunch77 marked this conversation as resolved.
Show resolved Hide resolved
)

//------------------------------------------------------------------------------
Expand Down
Loading