def test_transmit_no_response(self, requests_mock, logger_mock):
        envelope = create_envelope()
        options = Options(
            instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
        exporter = metrics_exporter.MetricsExporter(options)
        exporter._transmit_without_retry([envelope])

        self.assertEqual(len(requests_mock.call_args_list), 1)
        self.assertEqual(len(logger_mock.call_args_list), 1)
 def test_shutdown(self):
     mock_thread = mock.Mock()
     mock_storage = mock.Mock()
     exporter = metrics_exporter.MetricsExporter(
         instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
     exporter.exporter_thread = mock_thread
     exporter.storage = mock_storage
     exporter.shutdown()
     mock_thread.close.assert_called_once()
     mock_storage.close.assert_called_once()
    def test_create_properties_none(self):
        metric = create_metric()
        exporter = metrics_exporter.MetricsExporter(
            instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
        metric.time_series[0].label_values[0]._value = None
        properties = exporter._create_properties(metric.time_series[0],
                                                 metric.descriptor)

        self.assertEqual(len(properties), 1)
        self.assertEqual(properties['key'], 'null')
Пример #4
0
    def test_create_properties(self):
        metric = create_metric()
        options = Options(
            instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
        exporter = metrics_exporter.MetricsExporter(options)
        properties = exporter.create_properties(metric.time_series[0],
                                                metric.descriptor)

        self.assertEqual(len(properties), 1)
        self.assertEqual(properties['key'], 'val')
Пример #5
0
    def test_transmit_invalid_response_body(self, logger_mock):
        with mock.patch('requests.post') as requests_mock:
            type(requests_mock.return_value).text = mock.PropertyMock(
                return_value='invalid')
            envelope = create_envelope()
            options = Options(
                instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
            exporter = metrics_exporter.MetricsExporter(options)
            exporter._transmit_without_retry([envelope])

            self.assertEqual(len(requests_mock.call_args_list), 1)
            self.assertEqual(len(logger_mock.call_args_list), 1)
Пример #6
0
    def test_transmit_no_status_code(self, logger_mock):
        with mock.patch('requests.post') as requests_mock:
            type(requests_mock.return_value).status_code = mock.PropertyMock(
                side_effect=Exception())
            envelope = create_envelope()
            options = Options(
                instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
            exporter = metrics_exporter.MetricsExporter(options)
            exporter._transmit_without_retry([envelope])

            self.assertEqual(len(requests_mock.call_args_list), 1)
            self.assertEqual(len(logger_mock.call_args_list), 1)
    def test_create_data_points(self):
        metric = create_metric()
        exporter = metrics_exporter.MetricsExporter(
            instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
        data_points = exporter._create_data_points(metric.time_series[0],
                                                   metric.descriptor)

        self.assertEqual(len(data_points), 1)
        data_point = data_points[0]
        self.assertEqual(data_point.ns, metric.descriptor.name)
        self.assertEqual(data_point.name, metric.descriptor.name)
        self.assertEqual(data_point.value,
                         metric.time_series[0].points[0].value.value)
    def test_export_metrics(self, requests_mock):
        metric = create_metric()
        exporter = metrics_exporter.MetricsExporter(
            instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
        requests_mock.return_value.text = '{"itemsReceived":1,'\
                                          '"itemsAccepted":1,'\
                                          '"errors":[]}'
        requests_mock.return_value.status_code = 200
        exporter.export_metrics([metric])

        self.assertEqual(len(requests_mock.call_args_list), 1)
        post_body = requests_mock.call_args_list[0][1]['data']
        self.assertTrue('metrics' in post_body)
        self.assertTrue('properties' in post_body)
Пример #9
0
    def test_transmit_partial_exception(self, logger_mock):
        with mock.patch('requests.post') as requests_mock:
            text = '{"itemsReceived":1,'\
                   '"itemsAccepted":1}'
            type(requests_mock.return_value).text = mock.PropertyMock(
                return_value=text)
            type(requests_mock.return_value).status_code = mock.PropertyMock(
                return_value=206)
            envelope = create_envelope()
            options = Options(
                instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
            exporter = metrics_exporter.MetricsExporter(options)
            exporter._transmit_without_retry([envelope])

            self.assertEqual(len(requests_mock.call_args_list), 1)
            self.assertEqual(len(logger_mock.call_args_list), 1)
Пример #10
0
    def test_transmit_none_data_non_retryable(self, logger_mock, json_mock):
        with mock.patch('requests.post') as requests_mock:
            text = '{"itemsReceived":1,'\
                   '"itemsAccepted":1,'\
                   '"errors":[{"statusCode":500, "index":0}]}'
            type(requests_mock.return_value).text = mock.PropertyMock(
                return_value=text)
            type(requests_mock.return_value).status_code = mock.PropertyMock(
                return_value=402)
            envelope = create_envelope()
            options = Options(
                instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
            exporter = metrics_exporter.MetricsExporter(options)
            exporter._transmit_without_retry([envelope])

            self.assertEqual(len(requests_mock.call_args_list), 1)
            self.assertEqual(len(logger_mock.call_args_list), 1)
    def test_export_metrics_empty(self, requests_mock):
        exporter = metrics_exporter.MetricsExporter(
            instrumentation_key='12345678-1234-5678-abcd-12345678abcd')
        exporter.export_metrics([])

        self.assertEqual(len(requests_mock.call_args_list), 0)
 def test_constructor_invalid_batch_size(self):
     self.assertRaises(
         ValueError, lambda: metrics_exporter.MetricsExporter(
             instrumentation_key='12345678-1234-5678-abcd-12345678abcd',
             max_batch_size=-1))
 def test_constructor_missing_key(self):
     instrumentation_key = Options._default.instrumentation_key
     Options._default.instrumentation_key = None
     self.assertRaises(ValueError,
                       lambda: metrics_exporter.MetricsExporter())
     Options._default.instrumentation_key = instrumentation_key