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 ()
0 commit comments