From aa9943226cdd01fec1e9a2fc87716b36bd74bf9a Mon Sep 17 00:00:00 2001 From: Hyunsoo Kim Date: Wed, 20 Dec 2023 15:12:40 -0500 Subject: [PATCH 1/2] appsignals test on ec2 --- generator/test_case_generator.go | 4 ++ terraform/eks/daemon/app_signals/main.tf | 2 +- .../{resources => agent_configs}/config.json | 0 test/app_signals/app_signals_test.go | 31 +++++++++-- test/app_signals/metrics_test.go | 52 +++++++++++++++++-- test/app_signals/traces_test.go | 42 +++++++++++---- test/metric/app_signals_util.go | 16 ------ 7 files changed, 113 insertions(+), 34 deletions(-) rename test/app_signals/{resources => agent_configs}/config.json (100%) diff --git a/generator/test_case_generator.go b/generator/test_case_generator.go index af46a6f44..64b35fbeb 100644 --- a/generator/test_case_generator.go +++ b/generator/test_case_generator.go @@ -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. diff --git a/terraform/eks/daemon/app_signals/main.tf b/terraform/eks/daemon/app_signals/main.tf index 56aba8a37..d9a2fce9f 100644 --- a/terraform/eks/daemon/app_signals/main.tf +++ b/terraform/eks/daemon/app_signals/main.tf @@ -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" diff --git a/test/app_signals/resources/config.json b/test/app_signals/agent_configs/config.json similarity index 100% rename from test/app_signals/resources/config.json rename to test/app_signals/agent_configs/config.json diff --git a/test/app_signals/app_signals_test.go b/test/app_signals/app_signals_test.go index 01e9976b8..91c824800 100644 --- a/test/app_signals/app_signals_test.go +++ b/test/app_signals/app_signals_test.go @@ -45,6 +45,7 @@ func init() { var ( eksTestRunners []*test_runner.EKSTestRunner + ec2TestRunners []*test_runner.TestRunner ) func getEksTestRunners(env *environment.MetaData) []*test_runner.EKSTestRunner { @@ -53,15 +54,15 @@ 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, }, } @@ -69,6 +70,25 @@ func getEksTestRunners(env *environment.MetaData) []*test_runner.EKSTestRunner { 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 { @@ -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 } diff --git a/test/app_signals/metrics_test.go b/test/app_signals/metrics_test.go index 4771cba10..6f7a5630a 100644 --- a/test/app_signals/metrics_test.go +++ b/test/app_signals/metrics_test.go @@ -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 @@ -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 @@ -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) diff --git a/test/app_signals/traces_test.go b/test/app_signals/traces_test.go index 6f7471e05..8e6acc9e2 100644 --- a/test/app_signals/traces_test.go +++ b/test/app_signals/traces_test.go @@ -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 { @@ -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 { @@ -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) diff --git a/test/metric/app_signals_util.go b/test/metric/app_signals_util.go index f4deafd3b..29c4b51d2 100644 --- a/test/metric/app_signals_util.go +++ b/test/metric/app_signals_util.go @@ -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")}, @@ -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")}, From 5f3a76dad78dff10db53bc33652be3d839513501 Mon Sep 17 00:00:00 2001 From: Hyunsoo Kim Date: Fri, 22 Dec 2023 16:38:28 -0500 Subject: [PATCH 2/2] fix lint --- test/app_signals/metrics_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/app_signals/metrics_test.go b/test/app_signals/metrics_test.go index 6f7a5630a..66d08ac58 100644 --- a/test/app_signals/metrics_test.go +++ b/test/app_signals/metrics_test.go @@ -8,13 +8,14 @@ package app_signals import ( "time" + "github.com/aws/aws-sdk-go-v2/aws" + "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