1
1
from __future__ import annotations
2
2
3
- import datetime
4
3
import logging
5
4
import math
6
5
import time
@@ -118,19 +117,10 @@ def upload_metrics_to_elasticsearch(
118
117
time_start = time .time ()
119
118
try :
120
119
for metric in raw_data :
121
- if (
122
- isinstance (metric ["timestamp" ], int )
123
- and isinstance (metric ["value" ], float )
124
- and isinstance (metric ["name" ], str )
125
- ):
126
-
127
120
result = self .push_metric (
128
121
ElasticMetric (
129
122
run_uuid = run_uuid ,
130
- name = metric ["name" ],
131
- created_at = datetime .datetime .now (),
132
- timestamp = int (metric ["timestamp" ]),
133
- value = float (metric ["value" ]),
123
+ ** metric
134
124
),
135
125
index ,
136
126
)
@@ -141,7 +131,8 @@ def upload_metrics_to_elasticsearch(
141
131
)
142
132
143
133
return int (time .time () - time_start )
144
- except Exception :
134
+ except Exception as e :
135
+ self .safe_logger .error (f'Upload metric exception: { e } ' )
145
136
return - 1
146
137
147
138
def push_alert (self , alert : ElasticAlert , index : str ) -> int :
@@ -159,7 +150,8 @@ def push_alert(self, alert: ElasticAlert, index: str) -> int:
159
150
time_start = time .time ()
160
151
alert .save (using = self .es , index = index )
161
152
return int (time .time () - time_start )
162
- except Exception :
153
+ except Exception as e :
154
+ self .safe_logger .error (f"Push alert exception: { e } " )
163
155
return - 1
164
156
165
157
def push_metric (self , metric : ElasticMetric , index : str ) -> int :
@@ -177,9 +169,12 @@ def push_metric(self, metric: ElasticMetric, index: str) -> int:
177
169
time_start = time .time ()
178
170
metric .save (using = self .es , index = index )
179
171
return int (time .time () - time_start )
180
- except Exception :
172
+ except Exception as e :
173
+ print ('error' + str (e ))
174
+ self .safe_logger .error (f'Exception pushing metric: { e } ' )
181
175
return - 1
182
176
177
+
183
178
def push_telemetry (self , telemetry : ChaosRunTelemetry , index : str ):
184
179
if not index :
185
180
raise Exception ("index cannot be None or empty" )
@@ -189,7 +184,7 @@ def push_telemetry(self, telemetry: ChaosRunTelemetry, index: str):
189
184
elastic_chaos .save (using = self .es , index = index )
190
185
return int (time .time () - time_start )
191
186
except Exception as e :
192
- self .safe_logger .info ("Elastic push telemetry error: " + str ( e ) )
187
+ self .safe_logger .info (f "Elastic push telemetry error: { e } " )
193
188
return - 1
194
189
195
190
def search_telemetry (self , run_uuid : str , index : str ):
@@ -210,6 +205,7 @@ def search_telemetry(self, run_uuid: str, index: str):
210
205
ElasticChaosRunTelemetry (** hit .to_dict ()) for hit in result
211
206
]
212
207
except NotFoundError :
208
+ self .safe_logger .error ("Search telemetry not found" )
213
209
return []
214
210
return documents
215
211
@@ -229,6 +225,7 @@ def search_alert(self, run_uuid: str, index: str) -> list[ElasticAlert]:
229
225
result = search .execute ()
230
226
documents = [ElasticAlert (** hit .to_dict ()) for hit in result ]
231
227
except NotFoundError :
228
+ self .safe_logger .error ("Search alert not found" )
232
229
return []
233
230
return documents
234
231
@@ -248,5 +245,6 @@ def search_metric(self, run_uuid: str, index: str) -> list[ElasticMetric]:
248
245
result = search .execute ()
249
246
documents = [ElasticMetric (** hit .to_dict ()) for hit in result ]
250
247
except NotFoundError :
248
+ self .safe_logger .error ("Search metric not found" )
251
249
return []
252
250
return documents
0 commit comments