in codeguru_profiler_agent/agent_metadata/aws_lambda.py [0:0]
def get_metadata_for_configure_agent_call(self, lambda_context=None):
"""
This gathers metadata from self and from given lambda context to build a map used for the configure_agent call
:param lambda_context: a LambdaContext object which contains mainly the context from lambda framework.
See https://docs.aws.amazon.com/lambda/latest/dg/python-context.html for details about the context.
:return: a map with all metadata we want to send in configure_agent call.
"""
# get the singleton lambda context. The decorator should set it.
if lambda_context is None:
lambda_context = LambdaContext.get()
as_map = {
COMPUTE_PLAFORM_KEY: COMPUTE_PLAFORM_VALUE,
LAMBDA_FUNCTION_ARN_KEY: self.function_arn,
AGENT_ID_KEY: self.agent_id
}
if self.memory_limit_mb:
as_map[LAMBDA_MEMORY_LIMIT_IN_MB_KEY] = str(self.memory_limit_mb)
if self.execution_env:
as_map[EXECUTION_ENVIRONMENT_KEY] = self.execution_env
'''
Adding a specific condition to ignore MagicMock instances from being added to the metadata since
it causes boto to raise a ParamValidationError, similar to https://github.com/boto/botocore/issues/2063.
'''
if lambda_context.context is not None and not isinstance(lambda_context.context, MagicMock):
as_map[AWS_REQUEST_ID_KEY] = lambda_context.context.aws_request_id
as_map[LAMBDA_REMAINING_TIME_IN_MILLISECONDS_KEY] = \
str(lambda_context.context.get_remaining_time_in_millis())
if lambda_context.last_execution_duration:
as_map[LAMBDA_PREVIOUS_EXECUTION_TIME_IN_MILLISECONDS_KEY] = \
str(int(lambda_context.last_execution_duration.total_seconds() * 1000))
return as_map