Ejemplo n.º 1
0
    def send(self, data):
        response = None

        headers = self._headers.copy() if self._headers else {}
        headers.update(self.auth_headers)

        if compat.PY2 and isinstance(self._url, compat.text_type):
            url = self._url.encode("utf-8")
        else:
            url = self._url
        try:
            try:
                response = self.http.urlopen("POST",
                                             url,
                                             body=data,
                                             headers=headers,
                                             timeout=self._timeout,
                                             preload_content=False)
                logger.debug("Sent request, url=%s size=%.2fkb status=%s", url,
                             len(data) / 1024.0, response.status)
            except Exception as e:
                print_trace = True
                if isinstance(e, MaxRetryError) and isinstance(
                        e.reason, TimeoutError):
                    message = "Connection to APM Server timed out " "(url: %s, timeout: %s seconds)" % (
                        self._url,
                        self._timeout,
                    )
                    print_trace = False
                else:
                    message = "Unable to reach APM Server: %s (url: %s)" % (
                        e, self._url)
                raise TransportException(message,
                                         data,
                                         print_trace=print_trace)
            body = response.read()
            if response.status >= 400:
                if response.status == 429:  # rate-limited
                    message = "Temporarily rate limited: "
                    print_trace = False
                else:
                    message = "HTTP %s: " % response.status
                    print_trace = True
                message += body.decode("utf8", errors="replace")
                raise TransportException(message,
                                         data,
                                         print_trace=print_trace)
            return response.getheader("Location")
        finally:
            if response:
                response.close()
Ejemplo n.º 2
0
def test_sync_transport_fail_and_recover(mock_send, caplog):
    transport = Transport(client=None)
    transport.start_thread()
    try:
        mock_send.side_effect = TransportException("meh")
        transport.queue("x", {})
        transport.flush()
        assert transport.state.did_fail()
        # first retry should be allowed immediately
        assert transport.state.should_try()

        # recover
        mock_send.side_effect = None
        transport.queue("x", {})
        transport.flush()
        assert not transport.state.did_fail()
    finally:
        transport.close()