Skip to content

Commit

Permalink
appsignals test on ec2
Browse files Browse the repository at this point in the history
  • Loading branch information
movence committed Dec 22, 2023
1 parent 63de48b commit aa99432
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 34 deletions.
4 changes: 4 additions & 0 deletions generator/test_case_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ var testTypeToTestConfig = map[string][]testConfig{
terraformDir: "terraform/ec2/creds",
targets: map[string]map[string]struct{}{"os": {"al2": {}}},
},
{
testDir: "./test/app_signals",
targets: map[string]map[string]struct{}{"os": {"al2": {}}, "arc": {"amd64": {}}},
},
},
/*
You can only place 1 mac instance on a dedicate host a single time.
Expand Down
2 changes: 1 addition & 1 deletion terraform/eks/daemon/app_signals/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ resource "kubernetes_daemonset" "service" {
# Template Files
##########################################
locals {
cwagent_config = "../../../../${var.test_dir}/resources/config.json"
cwagent_config = "../../../../${var.test_dir}/agent_configs/config.json"
server_consumer = "../../../../${var.test_dir}/resources/metrics/server_consumer.json"
client_producer = "../../../../${var.test_dir}/resources/metrics/client_producer.json"
traces = "../../../../${var.test_dir}/resources/traces/traces.json"
Expand Down
File renamed without changes.
31 changes: 28 additions & 3 deletions test/app_signals/app_signals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func init() {

var (
eksTestRunners []*test_runner.EKSTestRunner
ec2TestRunners []*test_runner.TestRunner
)

func getEksTestRunners(env *environment.MetaData) []*test_runner.EKSTestRunner {
Expand All @@ -53,22 +54,41 @@ func getEksTestRunners(env *environment.MetaData) []*test_runner.EKSTestRunner {

eksTestRunners = []*test_runner.EKSTestRunner{
{
Runner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsServerConsumerTestName, "HostedIn.EKS.Cluster"},
Runner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsServerConsumerTestName, "HostedIn.EKS.Cluster", env.ComputeType},
Env: *env,
},
{
Runner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsClientProducerTestName, "HostedIn.EKS.Cluster"},
Runner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsClientProducerTestName, "HostedIn.EKS.Cluster", env.ComputeType},
Env: *env,
},
{
Runner: &AppSignalsTracesRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsTracesTestName, env.EKSClusterName},
Runner: &AppSignalsTracesRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsTracesTestName, env.EKSClusterName, env.ComputeType},
Env: *env,
},
}
}
return eksTestRunners
}

func getEc2TestRunners(env *environment.MetaData) []*test_runner.TestRunner {
if ec2TestRunners == nil {
factory := dimension.GetDimensionFactory(*env)

ec2TestRunners = []*test_runner.TestRunner{
{
TestRunner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsServerConsumerTestName, "HostedIn.Environment", env.ComputeType},
},
{
TestRunner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsClientProducerTestName, "HostedIn.Environment", env.ComputeType},
},
{
TestRunner: &AppSignalsTracesRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsTracesTestName, "Generic", env.ComputeType},
},
}
}
return ec2TestRunners
}

func (suite *AppSignalsTestSuite) TestAllInSuite() {
env := environment.GetEnvironmentMetaData()
switch env.ComputeType {
Expand All @@ -77,6 +97,11 @@ func (suite *AppSignalsTestSuite) TestAllInSuite() {
for _, testRunner := range getEksTestRunners(env) {
testRunner.Run(suite, env)
}
case computetype.EC2:
log.Println("Environment compute type is EC2")
for _, testRunner := range getEc2TestRunners(env) {
suite.AddToSuiteResult(testRunner.Run())
}
default:
return
}
Expand Down
52 changes: 47 additions & 5 deletions test/app_signals/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ package app_signals
import (
"time"

"github.com/aws/amazon-cloudwatch-agent-test/environment/computetype"
"github.com/aws/amazon-cloudwatch-agent-test/test/metric"
"github.com/aws/amazon-cloudwatch-agent-test/test/metric/dimension"
"github.com/aws/amazon-cloudwatch-agent-test/test/status"
"github.com/aws/amazon-cloudwatch-agent-test/test/test_runner"
"github.com/aws/amazon-cloudwatch-agent-test/util/common"
"github.com/aws/aws-sdk-go-v2/aws"
)

const testRetryCount = 6
Expand All @@ -21,12 +24,13 @@ type AppSignalsMetricsRunner struct {
test_runner.BaseTestRunner
testName string
dimensionKey string
computeType computetype.ComputeType
}

func (t *AppSignalsMetricsRunner) Validate() status.TestGroupResult {
metricsToFetch := t.GetMeasuredMetrics()
testResults := make([]status.TestResult, len(metricsToFetch))
instructions := GetInstructionsFromTestName(t.testName)
instructions := GetInstructionsFromTestName(t.testName, t.computeType)

for i, metricName := range metricsToFetch {
var testResult status.TestResult
Expand Down Expand Up @@ -59,18 +63,56 @@ func (t *AppSignalsMetricsRunner) GetMeasuredMetrics() []string {
}

func (e *AppSignalsMetricsRunner) GetAgentConfigFileName() string {
return ""
return "config.json"
}

func GetInstructionsFromTestName(testName string) []dimension.Instruction {
func (e *AppSignalsMetricsRunner) SetupAfterAgentRun() error {
// sends metrics data only for EC2
if e.computeType == computetype.EC2 {
common.RunCommand("pwd")
cmd := `while true; export START_TIME=$(date +%s%N); do
cat ./resources/metrics/server_consumer.json | sed -e "s/START_TIME/$START_TIME/" > server_consumer.json;
curl -H 'Content-Type: application/json' -d @server_consumer.json -i http://127.0.0.1:4316/v1/metrics --verbose;
cat ./resources/metrics/client_producer.json | sed -e "s/START_TIME/$START_TIME/" > client_producer.json;
curl -H 'Content-Type: application/json' -d @client_producer.json -i http://127.0.0.1:4316/v1/metrics --verbose;
sleep 5; done`
return common.RunAsyncCommand(cmd)
}

return nil
}

func GetInstructionsFromTestName(testName string, computeType computetype.ComputeType) []dimension.Instruction {
var instructions []dimension.Instruction
switch testName {
case AppSignalsClientProducerTestName:
return metric.ClientProducerInstructions
instructions = metric.ClientProducerInstructions
case AppSignalsServerConsumerTestName:
return metric.ServerConsumerInstructions
instructions = metric.ServerConsumerInstructions
default:
return nil
}

if computeType == computetype.EKS {
instructions = append(instructions, []dimension.Instruction{
{
Key: "HostedIn.EKS.Cluster",
Value: dimension.UnknownDimensionValue(),
},
{
Key: "HostedIn.K8s.Namespace",
Value: dimension.ExpectedDimensionValue{Value: aws.String("default")},
},
}...)
} else {
//EC2
instructions = append(instructions, dimension.Instruction{
Key: "HostedIn.Environment",
Value: dimension.ExpectedDimensionValue{Value: aws.String("Generic")},
})
}

return instructions
}

var _ test_runner.ITestRunner = (*AppSignalsMetricsRunner)(nil)
42 changes: 33 additions & 9 deletions test/app_signals/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ import (
"fmt"
"time"

"github.com/aws/amazon-cloudwatch-agent-test/environment/computetype"
"github.com/aws/amazon-cloudwatch-agent-test/test/status"
"github.com/aws/amazon-cloudwatch-agent-test/test/test_runner"
"github.com/aws/amazon-cloudwatch-agent-test/util/awsservice"
"github.com/aws/amazon-cloudwatch-agent-test/util/common"
)

const (
lookbackDuration = time.Duration(-5) * time.Minute
EKSClusterAnnotation = "HostedIn_EKS_Cluster"
EC2Annotation = "HostedIn_Environment"
)

var annotations = map[string]interface{}{
"aws_remote_target": "remote-target",
"aws_remote_operation": "remote-operation",
"aws_local_service": "service-name",
"aws_remote_service": "service-name-remote",
"HostedIn_K8s_Namespace": "default",
"aws_local_operation": "replaced-operation",
"aws_remote_target": "remote-target",
"aws_remote_operation": "remote-operation",
"aws_local_service": "service-name",
"aws_remote_service": "service-name-remote",
"aws_local_operation": "replaced-operation",
}

type AppSignalsTracesRunner struct {
test_runner.BaseTestRunner
testName string
clusterName string
hostedIn string
computeType computetype.ComputeType
}

func (t *AppSignalsTracesRunner) Validate() status.TestGroupResult {
Expand All @@ -40,7 +43,15 @@ func (t *AppSignalsTracesRunner) Validate() status.TestGroupResult {
Status: status.FAILED,
}
timeNow := time.Now()
annotations[EKSClusterAnnotation] = t.clusterName

// "Generic" means EC2
if t.hostedIn == "Generic" {
annotations[EC2Annotation] = t.hostedIn
} else {
annotations[EKSClusterAnnotation] = t.hostedIn
annotations["HostedIn_K8s_Namespace"] = "default"
}

xrayFilter := awsservice.FilterExpression(annotations)
traceIds, err := awsservice.GetTraceIDs(timeNow.Add(lookbackDuration), timeNow, xrayFilter)
if err != nil {
Expand Down Expand Up @@ -71,7 +82,20 @@ func (t *AppSignalsTracesRunner) GetMeasuredMetrics() []string {
}

func (e *AppSignalsTracesRunner) GetAgentConfigFileName() string {
return ""
return "config.json"
}

func (e *AppSignalsTracesRunner) SetupAfterAgentRun() error {
// sends metrics data only for EC2
if e.computeType == computetype.EC2 {
cmd := `while true; chmod +x ./resources/traceid_generator.go; export START_TIME=$(date +%s%N); export TRACE_ID=$(go run ./resources/traceid_generator.go); do
cat ./resources/traces/traces.json | sed -e "s/START_TIME/$START_TIME/" | sed -e "s/TRACE_ID/$TRACE_ID/" > traces.json;
curl -H 'Content-Type: application/json' -d @traces.json -i http://127.0.0.1:4316/v1/traces --verbose;
sleep 5; done`
return common.RunAsyncCommand(cmd)
}

return nil
}

var _ test_runner.ITestRunner = (*AppSignalsTracesRunner)(nil)
16 changes: 0 additions & 16 deletions test/metric/app_signals_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ var (
}

ServerConsumerInstructions = []dimension.Instruction{
{
Key: "HostedIn.EKS.Cluster",
Value: dimension.UnknownDimensionValue(),
},
{
Key: "HostedIn.K8s.Namespace",
Value: dimension.ExpectedDimensionValue{Value: aws.String("default")},
},
{
Key: "Service",
Value: dimension.ExpectedDimensionValue{Value: aws.String("service-name")},
Expand All @@ -39,14 +31,6 @@ var (
}

ClientProducerInstructions = []dimension.Instruction{
{
Key: "HostedIn.EKS.Cluster",
Value: dimension.UnknownDimensionValue(),
},
{
Key: "HostedIn.K8s.Namespace",
Value: dimension.ExpectedDimensionValue{Value: aws.String("default")},
},
{
Key: "Service",
Value: dimension.ExpectedDimensionValue{Value: aws.String("service-name")},
Expand Down

0 comments on commit aa99432

Please sign in to comment.