def test_add_request_flushes_every_request(send, tracked_request): AgentContext.build() manager = RequestManager() manager.add_request(tracked_request) assert send.call_count == 1 (command, ), kwargs = send.call_args assert kwargs == {} message = command.message() assert message == { "BatchCommand": { "commands": [ { "StartRequest": { "request_id": REQUEST_ID, "timestamp": START_TIME_STR, } }, { "FinishRequest": { "request_id": REQUEST_ID, "timestamp": END_TIME_STR, } }, ] } } assert not manager.request_buffer._requests # buffer is empty
def report(cls): event = ApplicationEvent() event.event_value = cls.data() event.event_type = 'scout.metadata' event.timestamp = datetime.utcnow() event.source = 'Pid: ' + str(getpid()) AgentContext.socket().send(event)
def install(*args, **kwargs): if "config" in kwargs: ScoutConfig().set(**kwargs["config"]) context = AgentContext.build(config=ScoutConfig()) if not context.config.value("monitor"): logger.info( "APM Not Launching on PID: %s - Configuration 'monitor' is not true", getpid(), ) return False InstrumentManager().install_all() if objtrace is not None: objtrace.enable() logger.debug("APM Launching on PID: %s", getpid()) launched = CoreAgentManager().launch() AppMetadata.report() if launched: AgentContext.socket().stop() return True
def __init__(self): self.core_agent_bin_path = None self.core_agent_bin_version = None self.core_agent_dir = '{}/{}'.format( AgentContext.instance().config.value('core_agent_dir'), AgentContext.instance().config.core_agent_full_name()) self.downloader = CoreAgentDownloader( self.core_agent_dir, AgentContext.instance().config.core_agent_full_name())
def install(): if not AgentContext.instance().config.value('monitor'): logger.info( "APM Not Launching on PID: %s - Configuration 'monitor' is not true", getpid()) return False logger.debug('APM Launching on PID: %s', getpid()) CoreAgentManager().launch() AppMetadata.report() AgentContext.socket().stop()
def test_add_request_handles_only_real_requests(send, tracked_request): AgentContext.build() tracked_request.real_request = False manager = RequestManager() manager.add_request(tracked_request) send.assert_not_called() assert not manager.request_buffer._requests # buffer is empty
def core_agent_manager(core_agent_dir): # Shorten path to socket to prevent core-agent from failing with: # Error opening listener on socket: Custom { kind: InvalidInput, # error: StringError("path must be shorter than SUN_LEN") } socket_path = "{}/test.sock".format(core_agent_dir) ScoutConfig.set(core_agent_dir=core_agent_dir, socket_path=socket_path) AgentContext.build() core_agent_manager = CoreAgentManager() try: yield core_agent_manager finally: assert not is_running(core_agent_manager) ScoutConfig.reset_all()
def test_report_app_metadata(send): AgentContext.build() AppMetadata().report() assert send.call_count == 1 (command, ), kwargs = send.call_args assert kwargs == {} message = command.message() assert message["ApplicationEvent"]["event_type"] == "scout.metadata" # py.test is installed, since it's running tests right now. assert ( "py", py.version) in message["ApplicationEvent"]["event_value"]["libraries"]
def test_report_app_metadata_error_getting_data(send): AgentContext.build() with patch( "scout_apm.core.metadata.AppMetadata.get_python_packages_versions", side_effect=RuntimeError, ): AppMetadata().report() assert send.call_count == 1 (command, ), kwargs = send.call_args assert kwargs == {} message = command.message() assert message["ApplicationEvent"]["event_type"] == "scout.metadata" assert message["ApplicationEvent"]["event_value"] == {}
def test_report_app_metadata_no_pkg_resources(send): AgentContext.build() pkg_resources = sys.modules["pkg_resources"] sys.modules["pkg_resources"] = None try: AppMetadata().report() finally: sys.modules["pkg_resources"] = pkg_resources assert send.call_count == 1 (command, ), kwargs = send.call_args assert kwargs == {} message = command.message() assert message["ApplicationEvent"]["event_type"] == "scout.metadata" assert message["ApplicationEvent"]["event_value"]["libraries"] == []
def test_install_all_installs_only_enabled_instruments(): # Disable all instruments except the last one. ScoutConfig.set( disabled_instruments=InstrumentManager.DEFAULT_INSTRUMENTS[:-1]) AgentContext.build() try: with patch( "scout_apm.core.instrument_manager.InstrumentManager.install" ) as install: # Only the last instrument is installed. InstrumentManager().install_all() install.assert_called_once_with("{}.{}".format( InstrumentManager.INSTRUMENT_NAMESPACE, InstrumentManager.DEFAULT_INSTRUMENTS[-1], )) finally: ScoutConfig.reset_all()
def launch(self): if AgentContext.instance().config.value( 'core_agent_launch') is not True: logger.debug("Not attempting to launch Core Agent " "due to 'core_agent_launch' setting.") return if self.verify() is not True: if AgentContext.instance().config.value( 'core_agent_download') is True: self.download() else: logger.debug("Not attempting to download Core Agent due " "to 'core_agent_download' setting.") if self.verify() is not True: logger.debug("Failed to verify Core Agent. " "Not launching Core Agent.") return False return self.run()
def run_samplers(cls): logger.debug("Starting Samplers. Acquiring samplers lock.") try: if cls._thread_lock.acquire(True): logger.debug("Acquired samplers lock.") instances = [Cpu(), Memory()] while True: for instance in instances: event = ApplicationEvent() event.event_value = instance.run() event.event_type = (instance.metric_type() + "/" + instance.metric_name()) event.timestamp = datetime.utcnow() event.source = "Pid: " + str(getpid()) if event.event_value is not None: AgentContext.socket().send(event) sleep(60) finally: logger.debug("Shutting down samplers thread.") cls._thread_lock.release()
def install(*args, **kwargs): if 'config' in kwargs: ScoutConfig().set(**kwargs['config']) context = AgentContext.build(config=ScoutConfig()) if not context.config.value('monitor'): logger.info( "APM Not Launching on PID: %s - Configuration 'monitor' is not true", getpid()) return False InstrumentManager().install_all() if HAS_OBJTRACE: objtrace.enable() logger.debug('APM Launching on PID: %s', getpid()) CoreAgentManager().launch() AppMetadata.report() AgentContext.socket().stop() return True
def log_level(self): level = AgentContext.instance().config.value('log_level') return ['--log-level', level]
def socket_path(self): socket_path = AgentContext.instance().config.value('socket_path') return ['--socket', socket_path]
def config_file(self): path = AgentContext.instance().config.value('config_file') if path is not None: return ['--config-file', path] else: return []
def flush_request(self, request): batch_command = BatchCommand.from_tracked_request(request) if batch_command is not None: AgentContext.socket().send(batch_command)
def root_url(self): return AgentContext.instance().config.value('download_url')
def test_agent_context_config(): context = AgentContext.build() assert isinstance(context.config, ScoutConfig)
def test_agent_context_provides_socket(): context = AgentContext.build() assert isinstance(context.socket(), CoreAgentSocket)