Skip to content

Commit 947074e

Browse files
authored
Merge pull request #229 from jsitu777/enable-cloudwatch-canary
Enable custom canary reporting to cloudwatch
2 parents de6ca75 + 09c999d commit 947074e

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

test/canary/canary.buildspec.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ phases:
1919
commands:
2020
# Run tests
2121
- docker run --name ack-canary $(env | cut -f1 -d= | sed 's/^/-e /') --mount type=bind,source="$(pwd)/",target="/${SERVICE}-controller/" ${ECR_CACHE_URI}:latest
22-
22+
23+
post_build:
24+
commands:
25+
- docker cp ack-canary:/sagemaker-controller/test/canary/integration_tests.xml /tmp/results.xml || true
2326
# Push test image to cache ECR repo
2427
- docker push ${ECR_CACHE_URI}:latest || true
2528

26-
29+
reports:
30+
IntegrationTestReport:
31+
files:
32+
- "results.xml"
33+
base-directory: "/tmp"
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import boto3
2+
from datetime import datetime
3+
import xml.etree.ElementTree as ET
4+
import os
5+
6+
7+
xml_path = "../integration_tests.xml"
8+
9+
def readXML_and_publish_metrics_to_cw():
10+
if os.path.isfile(xml_path):
11+
tree = ET.parse(xml_path)
12+
testsuite = tree.find("testsuite")
13+
failures = testsuite.attrib["failures"]
14+
tests = testsuite.attrib["tests"]
15+
successes = int(tests) - int(failures)
16+
else:
17+
print("f{xml_path} does not exists.")
18+
print(os.getcwd())
19+
failures = 0
20+
successes = 0
21+
tests = 23
22+
23+
timestamp = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
24+
25+
print(f"Failures: {failures}")
26+
print(f"Total tests: {tests}")
27+
print(f"Success: {successes}")
28+
29+
# push to cloudwatch
30+
cw_client = boto3.client("cloudwatch")
31+
project_name = os.getenv("PROJECT_NAME")
32+
33+
# Define the metric data
34+
metric_data = [
35+
{
36+
"MetricName": "failures",
37+
"Timestamp": timestamp,
38+
"Dimensions": [
39+
{"Name": "CodeBuild Project Name", "Value": project_name},
40+
],
41+
"Value": int(failures),
42+
"Unit": "Count",
43+
},
44+
{
45+
"MetricName": "total_tests",
46+
"Timestamp": timestamp,
47+
"Dimensions": [
48+
{"Name": "CodeBuild Project Name", "Value": project_name},
49+
],
50+
"Value": int(tests),
51+
"Unit": "Count",
52+
},
53+
{
54+
"MetricName": "successes",
55+
"Timestamp": timestamp,
56+
"Dimensions": [
57+
{"Name": "CodeBuild Project Name", "Value": project_name},
58+
],
59+
"Value": int(successes),
60+
"Unit": "Count",
61+
},
62+
]
63+
64+
# Use the put_metric_data method to push the metric data to CloudWatch
65+
try:
66+
response = cw_client.put_metric_data(
67+
Namespace="Canary_Metrics", MetricData=metric_data
68+
)
69+
if response["ResponseMetadata"]["HTTPStatusCode"] == 200:
70+
print("Successfully pushed data to CloudWatch")
71+
# return 200 status code if successful
72+
return 200
73+
else:
74+
# raise exception if the status code is not 200
75+
raise Exception(
76+
"Unexpected response status code: {}".format(
77+
response["ResponseMetadata"]["HTTPStatusCode"]
78+
)
79+
)
80+
except Exception as e:
81+
print("Error pushing data to CloudWatch: {}".format(e))
82+
# raise exception if there was an error pushing data to CloudWatch
83+
raise
84+
85+
86+
def main():
87+
readXML_and_publish_metrics_to_cw()
88+
89+
90+
if __name__ == "__main__":
91+
main()

test/canary/scripts/run_test.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ function print_controller_logs() {
2828
}
2929

3030
function cleanup {
31+
#push to metrics to cloudwatch
32+
echo "Pushing Codebuild stats to Cloudwatch."
33+
cd $SCRIPTS_DIR
34+
python push_stats_to_cloudwatch.py
35+
3136
echo "Cleaning up resources"
3237
set +e
3338
kubectl delete monitoringschedules --all
@@ -66,6 +71,7 @@ function cleanup {
6671
}
6772
trap cleanup EXIT
6873

74+
6975
# Update kubeconfig
7076
aws --region $CLUSTER_REGION eks update-kubeconfig --name $CLUSTER_NAME
7177

@@ -87,7 +93,7 @@ pushd $E2E_DIR
8793

8894
# run tests
8995
echo "Run Tests"
90-
pytest_args=( -n 15 --dist loadfile --log-cli-level INFO )
96+
pytest_args=( -n 15 --dist loadfile --log-cli-level INFO --junitxml ../canary/integration_tests.xml)
9197
if [[ $SERVICE_REGION =~ ^(eu-north-1|eu-west-3)$ ]]; then
9298
# If select_regions_1 true we run the notebook_instance test
9399
pytest_args+=(-m "canary or select_regions_1")

0 commit comments

Comments
 (0)