-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable canary report generation (#643)
-Merge release branch PR to main #634
- Loading branch information
Showing
3 changed files
with
105 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import boto3 | ||
from datetime import datetime | ||
import xml.etree.ElementTree as ET | ||
import os | ||
|
||
|
||
xml_path = "../canary/integration_tests.xml" | ||
|
||
|
||
def readXML_and_publish_metrics_to_cw(): | ||
if os.path.isfile(xml_path): | ||
tree = ET.parse(xml_path) | ||
testsuite = tree.find("testsuite") | ||
failures = testsuite.attrib["failures"] | ||
tests = testsuite.attrib["tests"] | ||
successes = int(tests) - int(failures) | ||
else: | ||
failures = 0 | ||
successes = 0 | ||
tests = 1 | ||
|
||
timestamp = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") | ||
|
||
print(f"Failures: {failures}") | ||
print(f"Total tests: {tests}") | ||
print(f"Success: {successes}") | ||
|
||
# push to cloudwatch | ||
cw_client = boto3.client("cloudwatch") | ||
project_name = "CodeBuild-Run-All-Tests" | ||
|
||
# Define the metric data | ||
metric_data = [ | ||
{ | ||
"MetricName": "failures", | ||
"Timestamp": timestamp, | ||
"Dimensions": [ | ||
{"Name": "CodeBuild Project Name", "Value": project_name}, | ||
], | ||
"Value": int(failures), | ||
"Unit": "Count", | ||
}, | ||
{ | ||
"MetricName": "total_tests", | ||
"Timestamp": timestamp, | ||
"Dimensions": [ | ||
{"Name": "CodeBuild Project Name", "Value": project_name}, | ||
], | ||
"Value": int(tests), | ||
"Unit": "Count", | ||
}, | ||
{ | ||
"MetricName": "successes", | ||
"Timestamp": timestamp, | ||
"Dimensions": [ | ||
{"Name": "CodeBuild Project Name", "Value": project_name}, | ||
], | ||
"Value": int(successes), | ||
"Unit": "Count", | ||
}, | ||
] | ||
|
||
# Use the put_metric_data method to push the metric data to CloudWatch | ||
try: | ||
response = cw_client.put_metric_data( | ||
Namespace="Canary_Metrics", MetricData=metric_data | ||
) | ||
if response["ResponseMetadata"]["HTTPStatusCode"] == 200: | ||
print("Successfully pushed data to CloudWatch") | ||
# return 200 status code if successful | ||
return 200 | ||
else: | ||
# raise exception if the status code is not 200 | ||
raise Exception( | ||
"Unexpected response status code: {}".format( | ||
response["ResponseMetadata"]["HTTPStatusCode"] | ||
) | ||
) | ||
except Exception as e: | ||
print("Error pushing data to CloudWatch: {}".format(e)) | ||
# raise exception if there was an error pushing data to CloudWatch | ||
raise | ||
|
||
|
||
def main(): | ||
readXML_and_publish_metrics_to_cw() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters