|
25 | 25 | VersionState, |
26 | 26 | ) |
27 | 27 | from localstack.services.lambda_.invocation.logs import LogHandler, LogItem |
28 | | -from localstack.services.lambda_.invocation.metrics import record_cw_metric_invocation |
| 28 | +from localstack.services.lambda_.invocation.metrics import ( |
| 29 | + record_cw_metric_error, |
| 30 | + record_cw_metric_invocation, |
| 31 | +) |
29 | 32 | from localstack.services.lambda_.invocation.runtime_executor import get_runtime_executor |
30 | 33 | from localstack.utils.strings import truncate |
31 | 34 | from localstack.utils.threads import FuncThread, start_thread |
@@ -215,14 +218,24 @@ def invoke(self, *, invocation: Invocation) -> InvocationResult: |
215 | 218 | ) |
216 | 219 |
|
217 | 220 | function_id = self.function_version.id |
218 | | - # MAYBE: reuse threads |
219 | | - start_thread( |
220 | | - lambda *args, **kwargs: record_cw_metric_invocation( |
221 | | - function_name=self.function.function_name, |
222 | | - region_name=self.function_version.id.region, |
223 | | - ), |
224 | | - name=f"record-cloudwatch-metric-{function_id.function_name}:{function_id.qualifier}", |
225 | | - ) |
| 221 | + # Record CloudWatch metrics in separate threads |
| 222 | + # MAYBE reuse threads rather than starting new threads upon every invocation |
| 223 | + if invocation_result.is_error: |
| 224 | + start_thread( |
| 225 | + lambda *args, **kwargs: record_cw_metric_error( |
| 226 | + function_name=self.function.function_name, |
| 227 | + region_name=self.function_version.id.region, |
| 228 | + ), |
| 229 | + name=f"record-cloudwatch-metric-error-{function_id.function_name}:{function_id.qualifier}", |
| 230 | + ) |
| 231 | + else: |
| 232 | + start_thread( |
| 233 | + lambda *args, **kwargs: record_cw_metric_invocation( |
| 234 | + function_name=self.function.function_name, |
| 235 | + region_name=self.function_version.id.region, |
| 236 | + ), |
| 237 | + name=f"record-cloudwatch-metric-{function_id.function_name}:{function_id.qualifier}", |
| 238 | + ) |
226 | 239 | # MAYBE: consider using the same prefix logging as in error case for execution environment. |
227 | 240 | # possibly as separate named logger. |
228 | 241 | LOG.debug("Got logs for invocation '%s'", invocation.request_id) |
|
0 commit comments