Ejemplo n.º 1
0
    def test_send_request_body_is_logged_raw_uncompressed_long_body_is_truncated(
            self):
        # Verify that very large bodies are truncated to avoid increased memory usage issues under
        # Python 2.7
        session = ScalyrClientSession("https://dummserver.com",
                                      "DUMMY API KEY", SCALYR_VERSION)

        session._ScalyrClientSession__connection = mock.Mock()
        session._ScalyrClientSession__receive_response = mock.Mock()
        session._ScalyrClientSession__compress = mock.Mock(
            return_value="compressed")

        add_events_request = AddEventsRequest({"bar": "baz"})
        event1 = Event(thread_id="foo4", attrs={
            "parser": "bar2"
        }).set_message("a" * (MAX_REQUEST_BODY_SIZE_LOG_MSG_LIMIT + 1))

        add_events_request.add_event(event=event1, timestamp=1)

        session.send(add_events_request=add_events_request)

        # Should log raw (uncompressed) request body / payload
        expected_body = (
            r'Sending POST /addEvents with body "{"bar":"baz".*\.\.\. \[body truncated to %s chars\] \.\.\.'
            % (MAX_REQUEST_BODY_SIZE_LOG_MSG_LIMIT))
        self.assertLogFileContainsRegex(expected_body,
                                        file_path=self.agent_debug_log_path)
Ejemplo n.º 2
0
    def test_send_request_body_is_logged_raw_uncompressed(self):
        """
        When sending a request with compression available / enabled, raw (uncompressed) request
        body (payload) should be logged under DEBUG log level.
        """
        session = ScalyrClientSession("https://dummserver.com",
                                      "DUMMY API KEY", SCALYR_VERSION)

        session._ScalyrClientSession__connection = mock.Mock()
        session._ScalyrClientSession__receive_response = mock.Mock()
        session._ScalyrClientSession__compress = mock.Mock(
            return_value="compressed")

        add_events_request = AddEventsRequest({"foo": "bar"})
        event1 = Event(thread_id="foo1", attrs={
            "parser": "bar1"
        }).set_message("eventOne")
        event2 = Event(thread_id="foo2", attrs={
            "parser": "bar2"
        }).set_message("eventTwo")
        add_events_request.add_event(event=event1, timestamp=1)
        add_events_request.add_event(event=event2, timestamp=2)

        session.send(add_events_request=add_events_request)

        # Should log raw (uncompressed) request body / payload
        expected_body = r'{"foo":"bar", events: \[{thread:"foo1", .*'
        self.assertLogFileContainsRegex(expected_body,
                                        file_path=self.agent_debug_log_path)
        expected_body = r'.*,{thread:"foo2", log:"foo2", attrs:{"parser":"bar2",.*'
        self.assertLogFileContainsRegex(expected_body,
                                        file_path=self.agent_debug_log_path)

        # Verify that the compression was indeed enabled since that's the scenario we are testing
        call_kwargs = session._ScalyrClientSession__connection.post.call_args_list[
            0][1]
        self.assertEqual(call_kwargs["body"], "compressed")