Пример #1
0
    class TestDisableProfiling:
        @before
        def before(self):
            self.config = AgentConfiguration(
                should_profile=True,
                sampling_interval=timedelta(milliseconds=1),
                minimum_time_reporting=timedelta(seconds=1),
                reporting_interval=timedelta(minutes=1),
                max_stack_depth=998)
            self.agent_config_merger = AgentConfigurationMerger(
                default=self.config)

        def test_it_sets_should_profile_to_false(self):
            self.agent_config_merger.disable_profiling()
            assert AgentConfiguration.get().should_profile is False

        def test_it_leaves_other_values_untouched(self):
            self.agent_config_merger.disable_profiling()
            assert AgentConfiguration.get().sampling_interval == timedelta(
                milliseconds=1)
            assert AgentConfiguration.get(
            ).minimum_time_reporting == timedelta(seconds=1)
            assert AgentConfiguration.get().reporting_interval == timedelta(
                minutes=1)
            assert AgentConfiguration.get().max_stack_depth == 998
Пример #2
0
    def test_user_overrides_are_not_overridden_at_merge_with(self):
        agent_config_merger = AgentConfigurationMerger(
            default=AgentConfiguration(), user_overrides=self.config)
        self.assert_init_values()

        agent_config_merger.merge_with(
            configure_agent_response=self.configure_agent_response)
        self.assert_init_values()
Пример #3
0
 def before(self):
     self.config = AgentConfiguration(
         should_profile=True,
         sampling_interval=timedelta(milliseconds=1),
         minimum_time_reporting=timedelta(seconds=1),
         reporting_interval=timedelta(minutes=1),
         max_stack_depth=998)
     self.agent_config_merger = AgentConfigurationMerger(
         default=self.config)
Пример #4
0
    def test_default_values_are_overridden_at_merge_with(self):
        agent_config_merger = AgentConfigurationMerger(default=self.config)
        self.assert_init_values()

        agent_config_merger.merge_with(
            configure_agent_response=self.configure_agent_response)
        assert AgentConfiguration.get().should_profile is False
        assert AgentConfiguration.get().sampling_interval == timedelta(
            milliseconds=2000)
        assert AgentConfiguration.get().minimum_time_reporting == timedelta(
            milliseconds=21000)
        assert AgentConfiguration.get().reporting_interval == timedelta(
            seconds=123)
        assert AgentConfiguration.get().max_stack_depth == 1001
Пример #5
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
Пример #6
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()
Пример #7
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()
Пример #8
0
    def test_a_user_override_is_not_overridden_at_merge(self):
        agent_config_merger = AgentConfigurationMerger(
            default=self.config, user_overrides=self.overide_config)
        assert AgentConfiguration.get().should_profile is True
        assert AgentConfiguration.get().sampling_interval == timedelta(
            seconds=9)
        assert AgentConfiguration.get().minimum_time_reporting == timedelta(
            seconds=1)
        assert AgentConfiguration.get().reporting_interval == timedelta(
            minutes=1)
        assert AgentConfiguration.get().max_stack_depth == 998

        agent_config_merger.merge_with(
            configure_agent_response=self.configure_agent_response)
        assert AgentConfiguration.get().should_profile is False
        assert AgentConfiguration.get().sampling_interval == timedelta(
            seconds=9)
        assert AgentConfiguration.get().minimum_time_reporting == timedelta(
            milliseconds=21000)
        assert AgentConfiguration.get().reporting_interval == timedelta(
            seconds=123)
        assert AgentConfiguration.get().max_stack_depth == 1001
Пример #9
0
    def __init__(self,
                 profiling_group_name,
                 region_name=None,
                 aws_session=None,
                 environment_override=dict()):
        """
        NOTE: The profiler MUST be instantiated using keyword arguments. We provide no compatibility for the order
        (or number) of arguments.

        - Configuration

            :param profiling_group_name: name of the profiling group where the profiles will be stored.
            :param region_name: AWS Region to report to, given profiling group name must exist in that region. Note
                that this value overwrites what is used in aws_session. If not provided, boto3 will search
                configuration for the region. (e.g. "us-west-2")
            :param aws_session: The boto3.Session that this profiler should be using for communicating with the backend
                Check https://boto3.amazonaws.com/v1/documentation/api/latest/guide/session.html for more details.

        - Advanced Configuration Options - We recommend not to touch these
            :param environment_override: custom dependency container dictionary. allows custom behavior to be injected
                but please note that we do not guarantee compatibility between any different profiler agent versions for
                this api (default: dict()). Possible keys:
                    - reporting_interval: delay between profile reports in datetime.timedelta (default: None)
                    - sampling_interval: delay between each sample in datetime.timedelta (default: 1 seconds)
                    - reporting_mode: Reporting mode to be used, two modes are supported: "codeguru_service" and "file".
                                      "file" mode is only used for testing at the moment. (default: "codeguru_service")
                    - file_prefix: path + file prefix to use for profile reports when in "file" reporting mode
                                   (default: './profile-{profiling_group_name}' only used when reporting mode is "file")
                    - cpu_limit_percentage: cpu limit (%) for profiler (default: 30)
                    - max_threads: the max number of threads getting sampled (default: 100)
                    - killswitch_filepath: file path pointing to the killswitch file (default: "/var/tmp/killProfiler")
                    - host_weight: A scale factor used to rescale the profile collected in this host to make the profile
                                   representative of the whole fleet (default: 1)
                    - endpoint_url: url used for submitting profile (default: None, will target codeguru prod APIs)
                    - excluded_threads: set of thread names to be excluded from sampling (default: set())
        """
        self._profiler_runner_instance = None
        self.environment = {}
        try:
            if not profiling_group_name:
                logger.info(
                    "Profiler must be passed a non empty profiling group name, CodeGuru Profiler will not start. "
                    "Please specify a ``profiling_group_name`` when configuring the ``Profiler`` class."
                )
                return

            # This is the profiler instance-wide dependency container aka environment
            # It is used to contain shared dependencies and configuration, and can also be used to override the behavior
            # on the profiler classes without needing to monkey-patch.
            self.environment = self._set_default_environment(
                profiling_group_name)
            self.environment["profiling_group_name"] = profiling_group_name
            self.environment["region_name"] = region_name
            self.environment["aws_session"] = aws_session

            default_config = AgentConfiguration(
                should_profile=self.environment["should_profile"],
                sampling_interval=self.environment["sampling_interval"],
                reporting_interval=self.environment["reporting_interval"],
                minimum_time_reporting=self.
                environment["minimum_time_reporting"],
                max_stack_depth=self.environment["max_stack_depth"],
                cpu_limit_percentage=self.environment["cpu_limit_percentage"])
            user_overrides = AgentConfiguration(
                should_profile=environment_override.get('should_profile'),
                sampling_interval=environment_override.get(
                    'sampling_interval'),
                reporting_interval=environment_override.get(
                    'reporting_interval'),
                minimum_time_reporting=environment_override.get(
                    'minimum_time_reporting'),
                max_stack_depth=environment_override.get('max_stack_depth'),
                cpu_limit_percentage=environment_override.get(
                    'cpu_limit_percentage'))
            agent_config_merger = AgentConfigurationMerger(
                default=default_config, user_overrides=user_overrides)
            self.environment["agent_config_merger"] = agent_config_merger

            # Removing all keys from the environment that were used for the AgentConfigurationMerger
            # to make sure the rest of the code would read it from the AgentConfiguration.
            for key in default_config.as_dict().keys():
                del self.environment[key]
            for key in user_overrides.as_dict().keys():
                del environment_override[key]

            self.environment = self._setup_final_environment(
                self.environment, environment_override)
            profiler_runner_factory = self.environment.get(
                "profiler_runner_factory") or ProfilerRunner
            self._profiler_runner_instance = profiler_runner_factory(
                environment=self.environment)
        except:
            logger.info(
                "Caught exception while creating the CodeGuru Profiler Agent instance",
                exc_info=True)
            if environment_override.get("allow_top_level_exceptions") is True:
                raise