Ejemplo n.º 1
0
def install(config=None):
    global shutdown_registered
    if config is not None:
        scout_config.set(**config)
    scout_config.log()

    if os.name == "nt":
        logger.info("APM Not Launching on PID: %s - Windows is not supported",
                    os.getpid())
        return False

    if not scout_config.value("monitor"):
        logger.info(
            "APM Not Launching on PID: %s - Configuration 'monitor' is not true",
            os.getpid(),
        )
        return False

    instruments.ensure_all_installed()
    objtrace.enable()

    logger.debug("APM Launching on PID: %s", os.getpid())
    launched = CoreAgentManager().launch()

    report_app_metadata()
    if launched:
        # Stop the thread to avoid running threads pre-fork
        CoreAgentSocketThread.ensure_stopped()

    if scout_config.value(
            "shutdown_timeout_seconds") > 0.0 and not shutdown_registered:
        atexit.register(shutdown)
        shutdown_registered = True

    return True
Ejemplo n.º 2
0
def report_app_metadata():
    CoreAgentSocketThread.send(
        ApplicationEvent(
            event_type="scout.metadata",
            event_value=get_metadata(),
            source="Pid: " + str(getpid()),
            timestamp=dt.datetime.utcnow(),
        ))
Ejemplo n.º 3
0
def test_wait_until_drained_one_slow(socket):
    class SlowCommand(object):
        def message(self):
            time.sleep(0.05)
            return {}

    for _ in range(10):
        CoreAgentSocketThread.send(SlowCommand())

    empty = CoreAgentSocketThread.wait_until_drained(timeout_seconds=0.05)
    assert not empty
    def finish(self):
        logger.debug("Stopping request: %s", self.request_id)
        if self.end_time is None:
            self.end_time = dt.datetime.utcnow()
        if self.is_real_request:
            self.tag("mem_delta", self._get_mem_delta())
            if not self.is_ignored():
                batch_command = BatchCommand.from_tracked_request(self)
                CoreAgentSocketThread.send(batch_command)
            SamplersThread.ensure_started()

        from scout_apm.core.context import context

        context.clear_tracked_request(self)
Ejemplo n.º 5
0
def shutdown():
    timeout_seconds = scout_config.value("shutdown_timeout_seconds")

    def callback(queue_size):
        if scout_config.value("shutdown_message_enabled"):
            print(  # noqa: T001
                ("Scout draining {queue_size} event{s} for up to" +
                 " {timeout_seconds} seconds").format(
                     queue_size=queue_size,
                     s=("" if queue_size == 1 else "s"),
                     timeout_seconds=timeout_seconds,
                 ),
                file=sys.stderr,
            )

    CoreAgentSocketThread.wait_until_drained(timeout_seconds=timeout_seconds,
                                             callback=callback)
Ejemplo n.º 6
0
    def run(self):
        logger.debug("Starting Samplers.")
        instances = [Cpu(), Memory()]

        while True:
            for instance in instances:
                event_value = instance.run()
                if event_value is not None:
                    event_type = instance.metric_type + "/" + instance.metric_name
                    event = ApplicationEvent(
                        event_value=event_value,
                        event_type=event_type,
                        timestamp=dt.datetime.utcnow(),
                        source="Pid: " + str(os.getpid()),
                    )
                    CoreAgentSocketThread.send(event)

            should_stop = self._stop_event.wait(timeout=60)
            if should_stop:
                logger.debug("Stopping Samplers.")
                break
Ejemplo n.º 7
0
    def finish(self):
        from scout_apm.core.context import context

        logger.debug("Stopping request: %s", self.request_id)
        if self.end_time is None:
            self.end_time = dt.datetime.utcnow()

        if self.is_real_request:
            self.tag("mem_delta", self._get_mem_delta())
            if not self.is_ignored() and not self.sent:
                self.sent = True
                batch_command = BatchCommand.from_tracked_request(self)
                if scout_config.value("log_payload_content"):
                    logger.debug(
                        "Sending request: %s. Payload: %s",
                        self.request_id,
                        batch_command.message(),
                    )
                else:
                    logger.debug("Sending request: %s.", self.request_id)
                CoreAgentSocketThread.send(batch_command)
            SamplersThread.ensure_started()

        details = " ".join(
            "{}={}".format(key, value)
            for key, value in [
                ("start_time", self.start_time),
                ("end_time", self.end_time),
                ("duration", (self.end_time - self.start_time).total_seconds()),
                ("active_spans", len(self.active_spans)),
                ("complete_spans", len(self.complete_spans)),
                ("tags", len(self.tags)),
                ("hit_max", self.hit_max),
                ("is_real_request", self.is_real_request),
                ("sent", self.sent),
            ]
        )
        logger.debug("Request %s %s", self.request_id, details)
        context.clear_tracked_request(self)
Ejemplo n.º 8
0
def test_wait_until_drained_one_item(socket):
    CoreAgentSocketThread._command_queue.put(Command(), False)

    empty = CoreAgentSocketThread.wait_until_drained()
    assert empty
Ejemplo n.º 9
0
def test_wait_until_drained_empty(socket):
    empty = CoreAgentSocketThread.wait_until_drained()
    assert empty
Ejemplo n.º 10
0
def test_send_network_error(sendall, socket):
    sendall.side_effect = OSError
    CoreAgentSocketThread.send(Command())
Ejemplo n.º 11
0
def test_send_serialization_error(socket):
    CoreAgentSocketThread.send(NonSerializableCommand())
Ejemplo n.º 12
0
def test_send(socket):
    CoreAgentSocketThread.send(Command())
Ejemplo n.º 13
0
def socket(running_agent):
    socket = CoreAgentSocketThread.ensure_started()
    # Wait for socket to connect and register:
    time.sleep(0.01)

    yield socket