Skip to content

Commit 1826399

Browse files
committed
fix ecs otlp test
1 parent f0afcf7 commit 1826399

5 files changed

Lines changed: 149 additions & 81 deletions

File tree

generator/test_case_generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ var testTypeToTestConfig = map[string][]testConfig{
381381
targets: map[string]map[string]struct{}{"metadataEnabled": {"enabled": {}}},
382382
},
383383
{
384-
testDir: "./test/otel_collect/ecs/host_metrics",
384+
testDir: "./test/otel_collect/ecs/otlp",
385385
targets: map[string]map[string]struct{}{"metadataEnabled": {"enabled": {}}},
386386
},
387387
},

test/otel_collect/ecs/host_metrics/host_metrics_ecs_test.go

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[
2+
{
3+
"name": "cloudwatch_agent",
4+
"image": "${cwagent_image}",
5+
"essential": true,
6+
"secrets": [
7+
{
8+
"name": "CW_CONFIG_CONTENT",
9+
"valueFrom": "${cwagent_ssm_parameter_arn}"
10+
},
11+
{
12+
"name": "PROMETHEUS_CONFIG_CONTENT",
13+
"valueFrom": "${prometheus_ssm_parameter_arn}"
14+
}
15+
],
16+
"logConfiguration": {
17+
"logDriver": "awslogs",
18+
"options": {
19+
"awslogs-region": "${region}",
20+
"awslogs-stream-prefix": "${testing_id}",
21+
"awslogs-group": "${log_group}"
22+
}
23+
},
24+
"cpu": 1,
25+
"memory": 1792
26+
},
27+
{
28+
"name": "otlp_pusher",
29+
"image": "curlimages/curl:8.10.1",
30+
"essential": false,
31+
"links": [
32+
"cloudwatch_agent"
33+
],
34+
"environment": [
35+
{
36+
"name": "TEST_ID",
37+
"value": "${testing_id}"
38+
}
39+
],
40+
"entryPoint": [
41+
"sh",
42+
"-c"
43+
],
44+
"command": [
45+
"while true; do S=$$(date +%s); NOW=$${S}000000000; START=$$(expr $${S} - 10)000000000; printf '{\"resourceMetrics\":[{\"resource\":{\"attributes\":[{\"key\":\"TestId\",\"value\":{\"stringValue\":\"%s\"}}]},\"scopeMetrics\":[{\"metrics\":[{\"name\":\"otlp_test_counter\",\"sum\":{\"dataPoints\":[{\"asInt\":\"1\",\"startTimeUnixNano\":\"%s\",\"timeUnixNano\":\"%s\",\"attributes\":[{\"key\":\"TestId\",\"value\":{\"stringValue\":\"%s\"}}]}],\"aggregationTemporality\":1,\"isMonotonic\":true}},{\"name\":\"otlp_test_gauge\",\"gauge\":{\"dataPoints\":[{\"asDouble\":42.0,\"timeUnixNano\":\"%s\",\"attributes\":[{\"key\":\"TestId\",\"value\":{\"stringValue\":\"%s\"}}]}]}}]}]}]}' \"$${TEST_ID}\" \"$${START}\" \"$${NOW}\" \"$${TEST_ID}\" \"$${NOW}\" \"$${TEST_ID}\" > /tmp/p.json; curl -s -X POST http://cloudwatch_agent:4318/v1/metrics -H 'Content-Type: application/json' -d @/tmp/p.json; sleep 10; done"
46+
],
47+
"logConfiguration": {
48+
"logDriver": "awslogs",
49+
"options": {
50+
"awslogs-region": "${region}",
51+
"awslogs-stream-prefix": "${testing_id}-pusher",
52+
"awslogs-group": "${log_group}"
53+
}
54+
},
55+
"cpu": 1,
56+
"memory": 256
57+
}
58+
]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
//go:build !windows
5+
6+
package otlp
7+
8+
import (
9+
"strings"
10+
"testing"
11+
"time"
12+
13+
"github.com/stretchr/testify/suite"
14+
15+
"github.com/aws/amazon-cloudwatch-agent-test/environment"
16+
"github.com/aws/amazon-cloudwatch-agent-test/test/otel_collect/otlpvalidation"
17+
"github.com/aws/amazon-cloudwatch-agent-test/test/status"
18+
"github.com/aws/amazon-cloudwatch-agent-test/test/test_runner"
19+
)
20+
21+
func init() {
22+
environment.RegisterEnvironmentMetaDataFlags()
23+
}
24+
25+
// clusterNamePrefix matches the ECS cluster naming in terraform/ecs_ec2/daemon.
26+
const clusterNamePrefix = "cwagent-integ-test-cluster-"
27+
28+
// ECSOtlpTestRunner validates that a workload (sidecar) running on the ECS daemon
29+
// can publish OTLP metrics to the agent's OTLP receiver and have them reach CloudWatch.
30+
type ECSOtlpTestRunner struct {
31+
test_runner.BaseTestRunner
32+
}
33+
34+
var _ test_runner.ITestRunner = (*ECSOtlpTestRunner)(nil)
35+
36+
func (t *ECSOtlpTestRunner) GetTestName() string { return "ecs_otlp" }
37+
38+
// GetAgentConfigFileName returns "" — config is pre-loaded by Terraform via SSM, no restart needed.
39+
func (t *ECSOtlpTestRunner) GetAgentConfigFileName() string { return "" }
40+
41+
func (t *ECSOtlpTestRunner) GetMeasuredMetrics() []string {
42+
return []string{"otlp_test_counter", "otlp_test_gauge"}
43+
}
44+
45+
func (t *ECSOtlpTestRunner) Validate() status.TestGroupResult {
46+
env := environment.GetEnvironmentMetaData()
47+
// Give the sidecar time to push and the agent to export before querying.
48+
time.Sleep(3 * time.Minute)
49+
// Isolate by the per-run testing_id, which the sidecar stamps as the TestId
50+
// attribute and is embedded in the ECS cluster ARN.
51+
labels := map[string]string{
52+
"TestId": testIDFromClusterArn(env.EcsClusterArn),
53+
}
54+
return otlpvalidation.ValidateOtlpMetricsWithLabels(t.GetTestName(), env.Region, t.GetMeasuredMetrics(), labels)
55+
}
56+
57+
// testIDFromClusterArn extracts the testing_id suffix from the ECS cluster ARN.
58+
func testIDFromClusterArn(clusterArn string) string {
59+
if i := strings.LastIndex(clusterArn, clusterNamePrefix); i != -1 {
60+
return clusterArn[i+len(clusterNamePrefix):]
61+
}
62+
return clusterArn
63+
}
64+
65+
func TestECSOtlpSuite(t *testing.T) {
66+
suite.Run(t, new(ECSOtlpTestSuite))
67+
}
68+
69+
type ECSOtlpTestSuite struct {
70+
suite.Suite
71+
test_runner.TestSuite
72+
}
73+
74+
func (suite *ECSOtlpTestSuite) GetSuiteName() string {
75+
return "ECSOtlp"
76+
}
77+
78+
func (suite *ECSOtlpTestSuite) TestAllInSuite() {
79+
env := environment.GetEnvironmentMetaData()
80+
ecsTestRunner := &test_runner.ECSTestRunner{
81+
Runner: &ECSOtlpTestRunner{},
82+
RunStrategy: &test_runner.ECSAgentRunStrategy{},
83+
Env: *env,
84+
}
85+
ecsTestRunner.Run(suite, env)
86+
suite.Assert().Equal(status.SUCCESSFUL, suite.Result.GetStatus(), "ECS OTLP Test Suite Failed")
87+
}

test/otel_collect/ecs/host_metrics/resources/config.json renamed to test/otel_collect/ecs/otlp/resources/config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"opentelemetry": {
66
"cluster_name": "cwagent-ecs-integ-test",
77
"collect": {
8-
"host_metrics": {
9-
"collection_interval": 30
8+
"otlp": {
9+
"grpc_endpoint": "0.0.0.0:4317",
10+
"http_endpoint": "0.0.0.0:4318"
1011
}
1112
}
1213
}

0 commit comments

Comments
 (0)