예제 #1
0
    def submit_and_assess_dist_payload(self, serie, attach_host_name=True):
        """
        Helper to assess the metric payload format.
        """
        now = time()

        if isinstance(serie, dict):
            Distribution.send(attach_host_name=attach_host_name,
                              **deepcopy(serie))
            serie = [serie]
        else:
            Distribution.send(deepcopy(serie),
                              attach_host_name=attach_host_name)

        payload = self.get_request_data()

        for i, metric in enumerate(payload['series']):
            if attach_host_name:
                self.assertEqual(set(metric.keys()),
                                 set(['metric', 'points', 'host']))
                self.assertEqual(metric['host'], api._host_name)
            else:
                self.assertEqual(set(metric.keys()), set(['metric', 'points']))

            self.assertEqual(metric['metric'], serie[i]['metric'])

            # points is a list of 1 point
            self.assertTrue(isinstance(metric['points'], list))
            self.assertEqual(len(metric['points']), 1)
            # it consists of a [time, value] pair
            self.assertEqual(len(metric['points'][0]), 2)
            # its value == value we sent
            self.assertEqual(metric['points'][0][1], serie[i]['points'][0][1])
            # it's time not so far from current time
            assert now - 1 < metric['points'][0][0] < now + 1
예제 #2
0
    def test_compression(self):
        """
        Metric and Distribution support zlib compression
        """

        # By default, there is no compression
        # Metrics
        series = dict(metric="metric.1", points=[(time(), 13.)])
        Metric.send(attach_host_name=False, **series)
        _, kwargs = self.request_mock.call_args()
        req_data = kwargs["data"]
        headers = kwargs["headers"]
        assert "Content-Encoding" not in headers
        assert req_data == json.dumps({"series": [series]})
        # Same result when explicitely False
        Metric.send(compress_payload=False, attach_host_name=False, **series)
        _, kwargs = self.request_mock.call_args()
        req_data = kwargs["data"]
        headers = kwargs["headers"]
        assert "Content-Encoding" not in headers
        assert req_data == json.dumps({"series": [series]})
        # Distributions
        series = dict(metric="metric.1", points=[(time(), 13.)])
        Distribution.send(attach_host_name=False, **series)
        _, kwargs = self.request_mock.call_args()
        req_data = kwargs["data"]
        headers = kwargs["headers"]
        assert "Content-Encoding" not in headers
        assert req_data == json.dumps({"series": [series]})
        # Same result when explicitely False
        Distribution.send(compress_payload=False, attach_host_name=False, **series)
        _, kwargs = self.request_mock.call_args()
        req_data = kwargs["data"]
        headers = kwargs["headers"]
        assert "Content-Encoding" not in headers
        assert req_data == json.dumps({"series": [series]})

        # Enabling compression
        # Metrics
        series = dict(metric="metric.1", points=[(time(), 13.)])
        compressed_series = zlib.compress(json.dumps({"series": [series]}).encode("utf-8"))
        Metric.send(compress_payload=True, attach_host_name=False, **series)
        _, kwargs = self.request_mock.call_args()
        req_data = kwargs["data"]
        headers = kwargs["headers"]
        assert "Content-Encoding" in headers
        assert headers["Content-Encoding"] == "deflate"
        assert req_data == compressed_series
        # Distributions
        series = dict(metric='metric.1', points=[(time(), 13.)])
        compressed_series = zlib.compress(json.dumps({"series": [series]}).encode("utf-8"))
        Distribution.send(compress_payload=True, attach_host_name=False, **series)
        _, kwargs = self.request_mock.call_args()
        req_data = kwargs["data"]
        headers = kwargs["headers"]
        assert "Content-Encoding" in headers
        assert headers["Content-Encoding"] == "deflate"
        assert req_data == compressed_series