Beispiel #1
0
    def before(self):
        codeguru_client_builder = CodeGuruClientBuilder(
            environment={"aws_session": boto3.session.Session()})

        self.client_stubber = Stubber(codeguru_client_builder.codeguru_client)

        self.clear_lambda_specific_environment_variables_for_test_run()

        profile_encoder = MagicMock(name="profile_encoder",
                                    spec=ProfileEncoder)
        profile_encoder.encode.side_effect = lambda **args: args[
            "output_stream"].write(b"test-profile-encoder-output")
        self.environment = {
            "profiling_group_name": profiling_group_name,
            "profile_encoder": profile_encoder,
            "codeguru_profiler_builder": codeguru_client_builder,
            "agent_metadata": AgentMetadata(fleet_info=DefaultFleetInfo()),
            "reporting_interval": timedelta(minutes=13),
            "sampling_interval": timedelta(seconds=13),
            "minimum_time_reporting": timedelta(minutes=13),
            "max_stack_depth": 1300
        }
        default_config = AgentConfiguration(
            should_profile=True,
            sampling_interval=self.environment["sampling_interval"],
            reporting_interval=self.environment["reporting_interval"],
            minimum_time_reporting=self.environment["minimum_time_reporting"])
        self.environment["agent_config_merger"] = AgentConfigurationMerger(
            default_config)
        self.subject = SdkReporter(environment=self.environment)
        self.subject.setup()
Beispiel #2
0
    def test_beta_endpoint_call_report_and_refresh_and_do_not_override_one_setting_of_default_agent_configuration(
            self):
        self.environment["agent_config_merger"] = AgentConfigurationMerger(
            default=self.agent_config,
            user_overrides=AgentConfiguration(sampling_interval=timedelta(
                seconds=2)))

        sdk_reporter = SdkReporter(self.environment)
        sdk_reporter.setup()

        assert AgentConfiguration.get().should_profile is True
        assert AgentConfiguration.get().sampling_interval == timedelta(
            seconds=2)
        assert AgentConfiguration.get().reporting_interval == timedelta(
            minutes=13)
        assert AgentConfiguration.get().minimum_time_reporting == timedelta(
            minutes=6)
        assert AgentConfiguration.get().max_stack_depth == 2345
        assert AgentConfiguration.get().cpu_limit_percentage == 29

        assert sdk_reporter.report(self.profile) is True

        sdk_reporter.refresh_configuration()
        assert AgentConfiguration.get().should_profile is True
        assert AgentConfiguration.get().sampling_interval == timedelta(
            seconds=2)
        assert AgentConfiguration.get().reporting_interval == timedelta(
            minutes=5)
        assert AgentConfiguration.get().minimum_time_reporting == timedelta(
            seconds=60)
        assert AgentConfiguration.get().max_stack_depth == 1000
        assert AgentConfiguration.get().cpu_limit_percentage == 10
Beispiel #3
0
class TestSdkReporter:
    def before(self):
        codeguru_client_builder = CodeGuruClientBuilder(
            environment={"aws_session": boto3.session.Session()})

        self.client_stubber = Stubber(codeguru_client_builder.codeguru_client)

        self.clear_lambda_specific_environment_variables_for_test_run()

        profile_encoder = MagicMock(name="profile_encoder",
                                    spec=ProfileEncoder)
        profile_encoder.encode.side_effect = lambda **args: args[
            "output_stream"].write(b"test-profile-encoder-output")
        self.environment = {
            "profiling_group_name": profiling_group_name,
            "profile_encoder": profile_encoder,
            "codeguru_profiler_builder": codeguru_client_builder,
            "agent_metadata": AgentMetadata(fleet_info=DefaultFleetInfo()),
            "errors_metadata": errors_metadata,
            "reporting_interval": timedelta(minutes=13),
            "sampling_interval": timedelta(seconds=13),
            "minimum_time_reporting": timedelta(minutes=13),
            "max_stack_depth": 1300
        }
        default_config = AgentConfiguration(
            should_profile=True,
            sampling_interval=self.environment["sampling_interval"],
            reporting_interval=self.environment["reporting_interval"],
            minimum_time_reporting=self.environment["minimum_time_reporting"])
        self.environment["agent_config_merger"] = AgentConfigurationMerger(
            default_config)
        self.subject = SdkReporter(environment=self.environment)
        self.subject.setup()
        self.subject.errors_metadata.reset()

    def clear_lambda_specific_environment_variables_for_test_run(self):
        keys_to_delete = [
            LAMBDA_TASK_ROOT, LAMBDA_RUNTIME_DIR, LAMBDA_EXECUTION_ENV,
            LAMBDA_MEMORY_SIZE_ENV
        ]
        for key in keys_to_delete:
            if key in os.environ:
                os.environ.__delitem__(key)
    def _report_profile(self, now):
        previous_last_report_attempted_value = self.last_report_attempted
        self.last_report_attempted = now
        self._add_overhead_metric_to_profile()
        logger.info("Attempting to report profile data: " + str(self.profile))
        if self.profile.is_empty():
            logger.info("Report was cancelled because it was empty")
            return False
        is_reporting_successful = self.reporter.report(self.profile)
        '''
        If we attempt to create a Profiling Group in the report() call, we do not want to update the last_report_attempted_value
        since we did not actually report a profile.

        This will occur only in the case of profiling using CodeGuru Profiler Python agent Lambda layer.
        '''
        if SdkReporter.check_create_pg_called_during_submit_profile == True:
            self.last_report_attempted = previous_last_report_attempted_value
            SdkReporter.reset_check_create_pg_called_during_submit_profile_flag(
            )
        return is_reporting_successful
 def _select_collector(environment):
     reporting_mode = environment.get('reporting_mode')
     if reporting_mode == "codeguru_service":
         environment["codeguru_profiler_builder"] = CodeGuruClientBuilder(environment)
         return LocalAggregator(
             reporter=SdkReporter(environment=environment),
             environment=environment)
     elif reporting_mode == "file":
         return LocalAggregator(
             reporter=FileReporter(environment=environment),
             environment=environment)
     else:
         raise ValueError("Invalid reporting mode for CodeGuru Profiler detected: {}".format(reporting_mode))
Beispiel #6
0
    def test_beta_endpoint_call_report_and_refresh_and_do_not_override_user_overrides_agent_configuration(
            self):
        self.environment["agent_config_merger"] = AgentConfigurationMerger(
            user_overrides=self.agent_config)

        sdk_reporter = SdkReporter(self.environment)
        sdk_reporter.setup()

        self.assert_initial_values()
        assert sdk_reporter.report(self.profile) is True

        sdk_reporter.refresh_configuration()
        self.assert_initial_values()