예제 #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)
예제 #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")
예제 #3
0
    def test_send_request_body_compression(self):
        add_events_request = AddEventsRequest({"bar": "baz"})
        event1 = Event(thread_id="foo4", attrs={
            "parser": "bar2"
        }).set_message("test message 1")
        event2 = Event(thread_id="foo5", attrs={
            "parser": "bar2"
        }).set_message("test message 2")
        add_events_request.add_event(event=event1, timestamp=1)
        add_events_request.add_event(event=event2, timestamp=2)

        serialized_data = add_events_request.get_payload()

        if sys.version_info < (2, 7, 0):
            # lz4 and zstandard Python package is not available for Python 2.6
            compression_types = scalyr_util.COMPRESSION_TYPE_TO_DEFAULT_LEVEL.copy(
            )
            del compression_types["zstandard"]
            del compression_types["lz4"]
        else:
            compression_types = scalyr_util.COMPRESSION_TYPE_TO_DEFAULT_LEVEL

        for compression_type in compression_types:
            session = ScalyrClientSession(
                "https://dummserver.com",
                "DUMMY API KEY",
                SCALYR_VERSION,
                compression_type=compression_type,
            )

            session._ScalyrClientSession__connection = mock.Mock()
            session._ScalyrClientSession__receive_response = mock.Mock()

            session.send(add_events_request=add_events_request)

            (
                path,
                request,
            ) = session._ScalyrClientSession__connection.post.call_args_list[0]

            _, decompress_func = scalyr_util.get_compress_and_decompress_func(
                compression_type)

            self.assertEqual(path[0], "/addEvents")

            if compression_type == "none":
                self.assertTrue("Content-Encoding" not in
                                session._ScalyrClientSession__standard_headers)
                self.assertTrue(b"test message 1" in request["body"])
                self.assertTrue(b"test message 2" in request["body"])
            else:
                self.assertEqual(
                    session.
                    _ScalyrClientSession__standard_headers["Content-Encoding"],
                    compression_type,
                )

                # Verify decompressed data matches the raw body
                self.assertTrue(b"test message 1" not in request["body"])
                self.assertTrue(b"test message 2" not in request["body"])
                self.assertFalse(serialized_data == request["body"])
                self.assertEqual(serialized_data,
                                 decompress_func(request["body"]))

        # Compression is disabled
        session = ScalyrClientSession(
            "https://dummserver.com",
            "DUMMY API KEY",
            SCALYR_VERSION,
            compression_type=None,
        )

        session._ScalyrClientSession__connection = mock.Mock()
        session._ScalyrClientSession__receive_response = mock.Mock()

        session.send(add_events_request=add_events_request)

        serialized_data = add_events_request.get_payload()

        (
            path,
            request,
        ) = session._ScalyrClientSession__connection.post.call_args_list[0]

        _, decompress_func = scalyr_util.get_compress_and_decompress_func(
            compression_type)

        self.assertEqual(path[0], "/addEvents")

        self.assertTrue(b"test message 1" in request["body"])
        self.assertTrue(b"test message 2" in request["body"])
        self.assertEqual(serialized_data, request["body"])
        self.assertTrue("Content-Encoding" not in
                        session._ScalyrClientSession__standard_headers)