Exemplo n.º 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
Exemplo n.º 2
0
        def test_it_ignores_memory_limit_if_none(self):
            subject = AWSLambda(function_arn="the_arn",
                                memory_limit_mb=None,
                                execution_env="AWS_Lambda_python3.8")

            assert subject.serialize_to_map() == {
                "computeType": "aws_lambda",
                "functionArn": "the_arn",
                "executionEnv": "AWS_Lambda_python3.8"
            }
Exemplo n.º 3
0
        def test_it_ignores_execution_env_if_none(self):
            subject = AWSLambda(function_arn="the_arn",
                                memory_limit_mb=512,
                                execution_env=None)

            assert subject.serialize_to_map() == {
                "computeType": "aws_lambda",
                "functionArn": "the_arn",
                "memoryLimitInMB": 512
            }
Exemplo n.º 4
0
        def test_it_returns_a_map(self):
            subject = AWSLambda(function_arn="the_arn",
                                memory_limit_mb=512,
                                execution_env="AWS_Lambda_python3.8")

            assert subject.serialize_to_map() == {
                "computeType": "aws_lambda",
                "functionArn": "the_arn",
                "memoryLimitInMB": 512,
                "executionEnv": "AWS_Lambda_python3.8"
            }
Exemplo n.º 5
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)
Exemplo n.º 6
0
    class TestGetMetadataForConfigureAgentCall:
        @before
        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)

        def test_it_returns_the_metadata_needed_for_configure_agent_call(self):
            assert self.subject.get_metadata_for_configure_agent_call(
                lambda_context=self.lambda_context) == {
                    "ComputePlatform": "AWSLambda",
                    "AgentId": "a509707f-12db-462d-b5c9-18bc19c69bf0",
                    "AwsRequestId": "the_aws_request_id",
                    "ExecutionEnvironment": "AWS_Lambda_python3.8",
                    "LambdaFunctionArn": "the_lambda_function_arn",
                    "LambdaMemoryLimitInMB": "512",
                    "LambdaRemainingTimeInMilliseconds": "30125",
                    "LambdaPreviousExecutionTimeInMilliseconds": "1234"
                }

        class TestWhenSomeFieldsAreMissing:
            @pytest.fixture(autouse=True)
            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

            def test_it_returns_at_least_the_compute_platform(self):
                metadata = self.subject.get_metadata_for_configure_agent_call()
                assert metadata["ComputePlatform"] == "AWSLambda"

            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"
Exemplo n.º 7
0
        class TestWhenSomeFieldsAreMissing:
            @pytest.fixture(autouse=True)
            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

            def test_it_returns_at_least_the_compute_platform(self):
                metadata = self.subject.get_metadata_for_configure_agent_call()
                assert metadata["ComputePlatform"] == "AWSLambda"

            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 _create_lambda_profiler(profiling_group_name, region_name, environment_override, context, env=os.environ):
    """
    Calls build_profiler module to create the profiler object. If we fail to create it we return a no-op profiler
    so that we don't even go through this method again.
    """
    from codeguru_profiler_agent.profiler_builder import build_profiler
    from codeguru_profiler_agent.agent_metadata.agent_metadata import AgentMetadata
    from codeguru_profiler_agent.agent_metadata.aws_lambda import AWSLambda
    override = {'agent_metadata': AgentMetadata(AWSLambda.look_up_metadata(context))}
    override.update(environment_override)
    profiler = build_profiler(pg_name=profiling_group_name, region_name=region_name, override=override, env=env)
    if profiler is None:
        return _EmptyProfiler()
    return profiler
Exemplo n.º 9
0
 def test_it_returns_none(self):
     subject = AWSLambda.look_up_metadata(context=None, env={})
     assert subject is None
Exemplo n.º 10
0
 def test_memory_limit_is_set_to_none(self):
     subject = AWSLambda.look_up_metadata(
         self.context, self.env)
     assert subject.memory_limit_mb is None
Exemplo n.º 11
0
 def test_it_finds_the_execution_env(self):
     subject = AWSLambda.look_up_metadata(
         self.context, self.env)
     assert subject.execution_env == "AWS_Lambda_python3.6"
Exemplo n.º 12
0
 def test_it_still_returns_the_arn(self):
     subject = AWSLambda.look_up_metadata(self.context, {})
     assert subject.function_arn == "the_lambda_function_arn"
Exemplo n.º 13
0
 def test_when_env_is_not_available_other_values_are_set_to_none(
         self):
     subject = AWSLambda.look_up_metadata(self.context, {})
     assert subject.execution_env is None
     assert subject.memory_limit_mb is None
Exemplo n.º 14
0
 def test_when_env_is_not_available_it_still_returns_at_least_the_arn(
         self):
     subject = AWSLambda.look_up_metadata(self.context, {})
     assert subject.function_arn == "the_lambda_function_arn"
Exemplo n.º 15
0
 def test_it_finds_memory_limit(self):
     subject = AWSLambda.look_up_metadata(
         self.context, self.env)
     assert subject.memory_limit_mb == 2048
Exemplo n.º 16
0
 def test_it_finds_the_arn(self):
     subject = AWSLambda.look_up_metadata(
         self.context, self.env)
     assert subject.function_arn == "the_lambda_function_arn"