in codeguru_profiler_agent/profiler.py [0: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