Best Python code snippet using localstack_python
cloudwatch_util.py
Source:cloudwatch_util.py
...5from localstack.utils.aws import aws_stack6# ---------------7# Lambda metrics8# ---------------9def dimension_lambda(kwargs):10 func_name = kwargs.get('func_name')11 if not func_name:12 func_name = kwargs.get('func_arn').split(':function:')[1].split(':')[0]13 return [{14 'Name': 'FunctionName',15 'Value': func_name16 }]17def publish_lambda_metric(metric, value, kwargs):18 # publish metric only if CloudWatch service is available19 if not config.service_port('cloudwatch'):20 return21 cw_client = aws_stack.connect_to_service('cloudwatch')22 cw_client.put_metric_data(Namespace='AWS/Lambda',23 MetricData=[{24 'MetricName': metric,25 'Dimensions': dimension_lambda(kwargs),26 'Timestamp': datetime.now(),27 'Value': value28 }]29 )30def publish_lambda_duration(time_before, kwargs):31 time_after = now_utc()32 publish_lambda_metric('Duration', time_after - time_before, kwargs)33def publish_lambda_error(time_before, kwargs):34 publish_lambda_metric('Invocations', 1, kwargs)35 publish_lambda_metric('Errors', 1, kwargs)36def publish_lambda_result(time_before, result, kwargs):37 if isinstance(result, Response) and result.status_code >= 400:38 return publish_lambda_error(time_before, kwargs)39 publish_lambda_metric('Invocations', 1, kwargs)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!