Skip to content

Commit

Permalink
Adding health check
Browse files Browse the repository at this point in the history
  • Loading branch information
Paramadon committed Oct 17, 2024
1 parent 3d58301 commit c92d6bc
Showing 1 changed file with 43 additions and 30 deletions.
73 changes: 43 additions & 30 deletions test/agent_otel_merging/agent_otel_merging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package agent_otel_merging

import (
"encoding/json"
"fmt"
"github.com/aws/amazon-cloudwatch-agent-test/util/awsservice"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -63,40 +65,51 @@ func sendPayload(t *testing.T) {
}

func verifyMetricsInCloudWatch(t *testing.T) {
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("2006-01-02T15:04:05Z"),
"--end-time", time.Now().Format("2006-01-02T15:04:05Z"),
"--period", "60", // Period in seconds
"--statistics", "Average")

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

// Parse the CloudWatch response
type MetricStatistics struct {
Datapoints []struct {
Timestamp time.Time `json:"Timestamp"`
Average float64 `json:"Average"`
} `json:"Datapoints"`
Label string `json:"Label"`
}
metricName := "test_metric"
namespace := "CWAgent-testing-otel"

dimensionName := "service.Name"
dimensionValue := "test-service"

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")
dimensions := []types.Dimension{
{
Name: &dimensionName,
Value: &dimensionValue,
},
}
startTime := time.Now().Add(-5 * time.Minute)
endTime := time.Now()
periodInSeconds := int32(60)
statType := []types.Statistic{types.StatisticAverage}

resp, err := awsservice.GetMetricStatistics(
metricName,
namespace,
dimensions,
startTime,
endTime,
periodInSeconds,
statType,
nil,
)
require.NoError(t, err, "Failed to fetch metric from CloudWatch")

// Ensure we have datapoints in the response
require.NotEmpty(t, resp.Datapoints, "No data points found for the metric")

// Optionally log or assert the values
for _, dp := range resp.Datapoints {
t.Logf("Timestamp: %v, Average: %v", *dp.Timestamp, *dp.Average)
}
}

func verifyHealthCheck(t *testing.T) {
endpoint := "http://localhost:13133/health/status"

verifyCmd := exec.Command("curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", endpoint)
verifyOutput, err := verifyCmd.CombinedOutput()
require.NoError(t, err, "Failed to execute curl command: %s", verifyOutput)
resp, err := http.Get(endpoint)
require.NoError(t, err, "Failed to send HTTP request")

defer resp.Body.Close()

httpCode := string(verifyOutput)
require.Equal(t, "200", httpCode, "Expected HTTP status code 200, got %s", httpCode)
require.Equal(t, 200, resp.StatusCode, "Expected HTTP status code 200, got %d", resp.StatusCode)
}

0 comments on commit c92d6bc

Please sign in to comment.