Skip to content

Commit 84992f3

Browse files
authored
Implement OTLP export integ tests (#669)
* Implement OTLP export integ tests
1 parent e54af57 commit 84992f3

9 files changed

Lines changed: 681 additions & 0 deletions

File tree

generator/test_case_generator.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ var testTypeToTestConfig = map[string][]testConfig{
196196
testDir: "./test/system_metrics/disabled",
197197
targets: map[string]map[string]struct{}{"os": {"al2": {}}, "arc": {"amd64": {}}},
198198
},
199+
{testDir: "./test/otlp_export/hostmetrics"},
200+
{testDir: "./test/otlp_export/statsd"},
201+
{testDir: "./test/otlp_export/collectd"},
199202
},
200203
testTypeKeyEc2SELinux: {
201204
{testDir: "./test/ca_bundle"},
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
receivers:
2+
collectd:
3+
endpoint: 127.0.0.1:25826
4+
5+
exporters:
6+
otlphttp:
7+
auth:
8+
authenticator: sigv4auth/monitoring
9+
endpoint: https://monitoring.us-west-2.amazonaws.com
10+
11+
extensions:
12+
sigv4auth/monitoring:
13+
region: us-west-2
14+
service: monitoring
15+
16+
processors:
17+
resourcedetection:
18+
detectors:
19+
- env
20+
- system
21+
transform/scope:
22+
metric_statements:
23+
- context: scope
24+
statements:
25+
- set(attributes["aws.cloudwatch.source"], "cloudwatch-agent")
26+
- set(attributes["aws.cloudwatch.solution"], "cwagent-ec2-metrics")
27+
batch:
28+
send_batch_size: 500
29+
send_batch_max_size: 500
30+
timeout: 1m0s
31+
32+
service:
33+
extensions:
34+
- sigv4auth/monitoring
35+
pipelines:
36+
metrics/collectd:
37+
receivers:
38+
- collectd
39+
processors:
40+
- resourcedetection
41+
- transform/scope
42+
- batch
43+
exporters:
44+
- otlphttp
45+
telemetry:
46+
logs:
47+
level: info
48+
metrics:
49+
level: None
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
//go:build !windows
5+
6+
package collectd_otlp
7+
8+
import (
9+
"bytes"
10+
"encoding/json"
11+
"fmt"
12+
"log"
13+
"net/http"
14+
"os/exec"
15+
"path/filepath"
16+
"testing"
17+
"time"
18+
19+
"github.com/stretchr/testify/suite"
20+
21+
"github.com/aws/amazon-cloudwatch-agent-test/environment"
22+
"github.com/aws/amazon-cloudwatch-agent-test/test/otlp_export/otlpvalidation"
23+
"github.com/aws/amazon-cloudwatch-agent-test/test/status"
24+
"github.com/aws/amazon-cloudwatch-agent-test/test/test_runner"
25+
"github.com/aws/amazon-cloudwatch-agent-test/util/common"
26+
)
27+
28+
func init() {
29+
environment.RegisterEnvironmentMetaDataFlags()
30+
}
31+
32+
type CollectdOtlpTestSuite struct {
33+
suite.Suite
34+
test_runner.TestSuite
35+
}
36+
37+
func (s *CollectdOtlpTestSuite) SetupSuite() {
38+
log.Println(">>>> Starting CollectdOtlpTestSuite")
39+
}
40+
41+
func (s *CollectdOtlpTestSuite) TearDownSuite() {
42+
s.Result.Print()
43+
log.Println(">>>> Finished CollectdOtlpTestSuite")
44+
}
45+
46+
func (s *CollectdOtlpTestSuite) TestAllInSuite() {
47+
runner := &CollectdOtlpTestRunner{}
48+
s.AddToSuiteResult(runner.run())
49+
s.Assert().Equal(status.SUCCESSFUL, s.Result.GetStatus(), "CollectdOtlp Test Suite Failed")
50+
}
51+
52+
func TestCollectdOtlpSuite(t *testing.T) {
53+
suite.Run(t, new(CollectdOtlpTestSuite))
54+
}
55+
56+
type CollectdOtlpTestRunner struct {
57+
test_runner.BaseTestRunner
58+
}
59+
60+
var _ test_runner.ITestRunner = (*CollectdOtlpTestRunner)(nil)
61+
62+
const yamlConfigPath = "/tmp/config.yaml"
63+
const yamlStartCommand = "sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -s -c "
64+
65+
func (t *CollectdOtlpTestRunner) run() status.TestGroupResult {
66+
if err := exec.Command("sudo", "/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl", "-a", "remove-config").Run(); err != nil {
67+
log.Printf("remove-config failed: %v", err)
68+
}
69+
common.CopyFile(filepath.Join("agent_configs", t.GetAgentConfigFileName()), yamlConfigPath)
70+
if err := common.StartAgentWithCommand(yamlConfigPath, false, false, yamlStartCommand); err != nil {
71+
return status.TestGroupResult{
72+
Name: t.GetTestName(),
73+
TestResults: []status.TestResult{{Name: "Starting Agent", Status: status.FAILED, Reason: err}},
74+
}
75+
}
76+
for i := 0; i < 15; i++ {
77+
body := bytes.NewBufferString("[]")
78+
resp, err := http.Post("http://127.0.0.1:25826/", "application/json", body)
79+
if err == nil {
80+
resp.Body.Close()
81+
break
82+
}
83+
time.Sleep(2 * time.Second)
84+
}
85+
if err := t.SetupAfterAgentRun(); err != nil {
86+
return status.TestGroupResult{
87+
Name: t.GetTestName(),
88+
TestResults: []status.TestResult{{Name: "Setup After Agent Run", Status: status.FAILED, Reason: err}},
89+
}
90+
}
91+
time.Sleep(30 * time.Second)
92+
common.StopAgent()
93+
return t.Validate()
94+
}
95+
96+
func (t *CollectdOtlpTestRunner) GetTestName() string { return "CollectdOtlp" }
97+
func (t *CollectdOtlpTestRunner) GetAgentConfigFileName() string { return "collectd_otlp.yaml" }
98+
func (t *CollectdOtlpTestRunner) GetAgentRunDuration() time.Duration {
99+
return 4 * time.Minute
100+
}
101+
func (t *CollectdOtlpTestRunner) GetMeasuredMetrics() []string {
102+
return []string{"gauge.gauge_1"}
103+
}
104+
105+
func (t *CollectdOtlpTestRunner) SetupAfterAgentRun() error {
106+
return sendCollectdHTTPMetrics(t.GetAgentRunDuration())
107+
}
108+
109+
func (t *CollectdOtlpTestRunner) Validate() status.TestGroupResult {
110+
return otlpvalidation.ValidateOtlpMetrics(t.GetTestName(), "us-west-2", t.GetMeasuredMetrics())
111+
}
112+
113+
func sendCollectdHTTPMetrics(duration time.Duration) error {
114+
metrics := []map[string]interface{}{
115+
{"values": []int{1}, "dstypes": []string{"gauge"}, "dsnames": []string{"value"}, "time": 0, "interval": 10, "host": "testhost", "plugin": "gauge_1", "plugin_instance": "", "type": "gauge", "type_instance": "gauge_1"},
116+
{"values": []int{1}, "dstypes": []string{"counter"}, "dsnames": []string{"value"}, "time": 0, "interval": 10, "host": "testhost", "plugin": "counter_1", "plugin_instance": "", "type": "counter", "type_instance": "counter_1"},
117+
}
118+
end := time.Now().Add(duration)
119+
for time.Now().Before(end) {
120+
for j := range metrics {
121+
metrics[j]["time"] = time.Now().Unix()
122+
}
123+
body, err := json.Marshal(metrics)
124+
if err != nil {
125+
return fmt.Errorf("marshal error: %w", err)
126+
}
127+
resp, err := http.Post("http://127.0.0.1:25826/", "application/json", bytes.NewReader(body))
128+
if err != nil {
129+
log.Printf("collectd post error: %v", err)
130+
} else {
131+
resp.Body.Close()
132+
}
133+
time.Sleep(time.Second)
134+
}
135+
return nil
136+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
receivers:
2+
hostmetrics:
3+
collection_interval: 1m0s
4+
scrapers:
5+
cpu:
6+
metrics:
7+
system.cpu.utilization:
8+
enabled: true
9+
memory:
10+
disk:
11+
load:
12+
filesystem:
13+
network:
14+
paging:
15+
processes:
16+
process:
17+
system:
18+
19+
exporters:
20+
otlphttp:
21+
auth:
22+
authenticator: sigv4auth/monitoring
23+
endpoint: https://monitoring.us-west-2.amazonaws.com
24+
25+
extensions:
26+
sigv4auth/monitoring:
27+
region: us-west-2
28+
service: monitoring
29+
30+
processors:
31+
resourcedetection:
32+
detectors:
33+
- env
34+
- system
35+
transform/scope:
36+
metric_statements:
37+
- context: scope
38+
statements:
39+
- set(attributes["aws.cloudwatch.source"], "cloudwatch-agent")
40+
- set(attributes["aws.cloudwatch.solution"], "cwagent-ec2-metrics")
41+
batch:
42+
send_batch_size: 500
43+
send_batch_max_size: 500
44+
timeout: 1m0s
45+
46+
service:
47+
extensions:
48+
- sigv4auth/monitoring
49+
pipelines:
50+
metrics/host:
51+
receivers:
52+
- hostmetrics
53+
processors:
54+
- resourcedetection
55+
- transform/scope
56+
- batch
57+
exporters:
58+
- otlphttp
59+
telemetry:
60+
logs:
61+
level: info
62+
metrics:
63+
level: None
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
//go:build !windows
5+
6+
package hostmetrics
7+
8+
import (
9+
"log"
10+
"os/exec"
11+
"path/filepath"
12+
"testing"
13+
"time"
14+
15+
"github.com/stretchr/testify/suite"
16+
17+
"github.com/aws/amazon-cloudwatch-agent-test/environment"
18+
"github.com/aws/amazon-cloudwatch-agent-test/test/otlp_export/otlpvalidation"
19+
"github.com/aws/amazon-cloudwatch-agent-test/test/status"
20+
"github.com/aws/amazon-cloudwatch-agent-test/test/test_runner"
21+
"github.com/aws/amazon-cloudwatch-agent-test/util/common"
22+
)
23+
24+
func init() {
25+
environment.RegisterEnvironmentMetaDataFlags()
26+
}
27+
28+
type HostMetricsOtlpTestSuite struct {
29+
suite.Suite
30+
test_runner.TestSuite
31+
}
32+
33+
func (s *HostMetricsOtlpTestSuite) SetupSuite() {
34+
log.Println(">>>> Starting HostMetricsOtlpTestSuite")
35+
}
36+
37+
func (s *HostMetricsOtlpTestSuite) TearDownSuite() {
38+
s.Result.Print()
39+
log.Println(">>>> Finished HostMetricsOtlpTestSuite")
40+
}
41+
42+
func (s *HostMetricsOtlpTestSuite) TestAllInSuite() {
43+
runner := &HostMetricsOtlpTestRunner{}
44+
s.AddToSuiteResult(runner.run())
45+
s.Assert().Equal(status.SUCCESSFUL, s.Result.GetStatus(), "HostMetricsOtlp Test Suite Failed")
46+
}
47+
48+
func TestHostMetricsOtlpSuite(t *testing.T) {
49+
suite.Run(t, new(HostMetricsOtlpTestSuite))
50+
}
51+
52+
type HostMetricsOtlpTestRunner struct {
53+
test_runner.BaseTestRunner
54+
}
55+
56+
var _ test_runner.ITestRunner = (*HostMetricsOtlpTestRunner)(nil)
57+
58+
const yamlConfigPath = "/tmp/config.yaml"
59+
const yamlStartCommand = "sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -s -c "
60+
61+
func (t *HostMetricsOtlpTestRunner) run() status.TestGroupResult {
62+
if err := exec.Command("sudo", "/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl", "-a", "remove-config").Run(); err != nil {
63+
log.Printf("remove-config failed: %v", err)
64+
}
65+
common.CopyFile(filepath.Join("agent_configs", t.GetAgentConfigFileName()), yamlConfigPath)
66+
if err := common.StartAgentWithCommand(yamlConfigPath, false, false, yamlStartCommand); err != nil {
67+
return status.TestGroupResult{
68+
Name: t.GetTestName(),
69+
TestResults: []status.TestResult{{Name: "Starting Agent", Status: status.FAILED, Reason: err}},
70+
}
71+
}
72+
time.Sleep(t.GetAgentRunDuration())
73+
common.StopAgent()
74+
result := t.Validate()
75+
return result
76+
}
77+
78+
func (t *HostMetricsOtlpTestRunner) GetTestName() string { return "HostMetricsOtlp" }
79+
func (t *HostMetricsOtlpTestRunner) GetAgentConfigFileName() string { return "hostmetrics_otlp.yaml" }
80+
func (t *HostMetricsOtlpTestRunner) GetAgentRunDuration() time.Duration {
81+
return 4 * time.Minute
82+
}
83+
func (t *HostMetricsOtlpTestRunner) GetMeasuredMetrics() []string {
84+
return []string{
85+
// cpu scraper
86+
"system.cpu.time",
87+
"system.cpu.utilization",
88+
// memory scraper
89+
"system.memory.usage",
90+
// disk scraper
91+
"system.disk.io",
92+
"system.disk.io_time",
93+
"system.disk.merged",
94+
"system.disk.operation_time",
95+
"system.disk.operations",
96+
"system.disk.pending_operations",
97+
"system.disk.weighted_io_time",
98+
// load scraper
99+
"system.cpu.load_average.1m",
100+
"system.cpu.load_average.5m",
101+
"system.cpu.load_average.15m",
102+
// filesystem scraper
103+
"system.filesystem.usage",
104+
"system.filesystem.inodes.usage",
105+
// network scraper
106+
"system.network.io",
107+
"system.network.packets",
108+
"system.network.dropped",
109+
"system.network.errors",
110+
"system.network.connections",
111+
// paging scraper
112+
"system.paging.operations",
113+
"system.paging.faults",
114+
// processes scraper
115+
"system.processes.count",
116+
"system.processes.created",
117+
// process scraper
118+
"process.cpu.time",
119+
"process.memory.usage",
120+
"process.memory.virtual",
121+
"process.disk.io",
122+
// system scraper
123+
"system.uptime",
124+
}
125+
}
126+
127+
func (t *HostMetricsOtlpTestRunner) Validate() status.TestGroupResult {
128+
return otlpvalidation.ValidateOtlpMetrics(t.GetTestName(), "us-west-2", t.GetMeasuredMetrics())
129+
}

0 commit comments

Comments
 (0)