Skip to content

Commit

Permalink
using awsservice function
Browse files Browse the repository at this point in the history
  • Loading branch information
Paramadon committed Oct 16, 2024
1 parent 92874f4 commit f19c9b1
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions test/agent_otel_merging/agent_otel_merging_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package agent_otel_merging

import (
"encoding/json"
"fmt"
"github.com/aws/amazon-cloudwatch-agent-test/environment"
"github.com/aws/amazon-cloudwatch-agent-test/util/awsservice"
"github.com/aws/amazon-cloudwatch-agent-test/util/common"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"github.com/stretchr/testify/require"
"os"
"os/exec"
Expand Down Expand Up @@ -45,35 +47,24 @@ func TestOtelMerging(t *testing.T) {
"-H", "Content-Type: application/json", "-d", payloadStr)
curlOutput, err := curlCmd.CombinedOutput()
require.NoError(t, err, "Failed to send metrics: %s", curlOutput)
time.Sleep(1 * time.Minute)
time.Sleep(2 * time.Minute)

//Verifying payload reach CW
verifyCmd := exec.Command("aws", "cloudwatch", "get-metric-statistics",
"--namespace", "CWAgent-testing-otel",
"--metric-name", "test_metric",
"--dimensions", "Name=service.name,Value=test-service",
"--start-time", time.Now().Add(-5*time.Minute).Format("2024-01-02T15:04:05Z"),
"--end-time", time.Now().Format("2024-01-02T15:04:05Z"),
"--period", "60", // Period in seconds
"--statistics", "Average")

verifyOutput, err := verifyCmd.CombinedOutput()
require.NoError(t, err, "Failed to fetch metric: %s", verifyOutput)

type MetricStatistics struct {
Datapoints []struct {
Timestamp time.Time `json:"Timestamp"`
Average float64 `json:"Average"`
} `json:"Datapoints"`
Label string `json:"Label"`
metricDimensions := []types.Dimension{
{
Name: aws.String("service.name"),
Value: aws.String("test-service"),
},
}
statList := []types.Statistic{
types.StatisticAverage,
}
stats, err := awsservice.GetMetricStatistics("test_metric", "CWAgent-testing-otel-merging",
metricDimensions,
time.Now().Add(-5*time.Minute),
time.Now(), 60, statList, nil)

var stats MetricStatistics
err = json.Unmarshal(verifyOutput, &stats)
require.NoError(t, err, "Failed to parse CloudWatch response")
require.NotEmpty(t, stats.Datapoints, "No data points found for the metric")

t.Logf("CloudWatch Metric Output: %s", verifyOutput)

common.StopAgent()
}

0 comments on commit f19c9b1

Please sign in to comment.