Пример #1
0
 def around(self):
     self.subject = AWSLambda(
         function_arn="the_lambda_function_arn",
         memory_limit_mb=None,
         execution_env=None,
         agent_id="a509707f-12db-462d-b5c9-18bc19c69bf0")
     LambdaContext.get()
     yield
     LambdaContext._singleton = None
Пример #2
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
 def profiler_decorate(event, context):
     start_time = datetime.now()
     global _profiler
     if _profiler is None:
         _profiler = profiler_factory(profiling_group_name=profiling_group_name,
                                      region_name=region_name,
                                      environment_override=environment_override,
                                      context=context, env=env)
     LambdaContext.get().context = context
     if not _profiler.start():
         # if start() failed, there is high chance it will fail again
         # so we disable the profiler to prevent further attempts.
         _profiler = _EmptyProfiler()
     try:
         return function(event, context)
     finally:
         LambdaContext.get().last_execution_duration = datetime.now() - start_time
         _profiler.pause()
Пример #4
0
 def before(self):
     self.context = Mock()
     self.context.invoked_function_arn = "the_lambda_function_arn"
     self.context.aws_request_id = "the_aws_request_id"
     self.context.get_remaining_time_in_millis = Mock(
         return_value=30125)
     self.subject = AWSLambda(
         function_arn="the_lambda_function_arn",
         memory_limit_mb=512,
         execution_env="AWS_Lambda_python3.8",
         agent_id="a509707f-12db-462d-b5c9-18bc19c69bf0")
     self.lambda_context = LambdaContext()
     self.lambda_context.context = self.context
     self.lambda_context.last_execution_duration = timedelta(
         seconds=1, milliseconds=234, microseconds=987)
Пример #5
0
 def test_it_can_return_last_execution_duration_independently(self):
     LambdaContext.get().last_execution_duration = timedelta(
         seconds=1, milliseconds=234, microseconds=987)
     metadata = self.subject.get_metadata_for_configure_agent_call()
     assert metadata[
         "LambdaPreviousExecutionTimeInMilliseconds"] == "1234"
 def test_last_call_duration_is_set_in_lambda_context_singleton(self):
     self.handler({}, self.context)
     assert LambdaContext.get().last_execution_duration
 def test_context_is_set_in_lambda_context_singleton(self):
     self.handler({}, self.context)
     assert LambdaContext.get().context is self.context