def test_invalid_sampling_rate(self): with self.assertRaises(ValueError): log_exporter.AzureLogHandler( enable_stats_metrics=False, instrumentation_key='12345678-1234-5678-abcd-12345678abcd', logging_sampling_rate=4.0, )
def test_log_with_invalid_custom_properties(self, requests_mock): logger = logging.getLogger(self.id()) handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', storage_path=os.path.join(TEST_FOLDER, self.id()), ) logger.addHandler(handler) logger.warning('action_1_%s', None) logger.warning('action_2_%s', 'arg', extra={'custom_dimensions': 'not_a_dict'}) logger.warning('action_3_%s', 'arg', extra={'notcustom_dimensions': { 'key_1': 'value_1' }}) handler.close() self.assertEqual(len(os.listdir(handler.storage.path)), 0) post_body = requests_mock.call_args_list[0][1]['data'] self.assertTrue('action_1_' in post_body) self.assertTrue('action_2_arg' in post_body) self.assertTrue('action_3_arg' in post_body) self.assertFalse('not_a_dict' in post_body) self.assertFalse('key_1' in post_body)
def test_export_empty(self, request_mock): handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', storage_path=os.path.join(TEST_FOLDER, self.id()), ) handler._export([]) self.assertEqual(len(os.listdir(handler.storage.path)), 0) handler.close()
def test_init_handler_with_queue_capacity(self): handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', queue_capacity=500, ) self.assertEqual(handler.options.queue_capacity, 500) self.assertEqual(handler._worker._src._queue.maxsize, 500)
def test_init_handler_with_proxies(self): handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', proxies='{"https":"https://test-proxy.com"}', ) self.assertEqual( handler.options.proxies, '{"https":"https://test-proxy.com"}', )
def test_log_record_to_envelope(self): handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', storage_path=os.path.join(TEST_FOLDER, self.id()), ) envelope = handler.log_record_to_envelope( mock.MagicMock( exc_info=None, levelno=10, )) self.assertEqual(envelope.iKey, '12345678-1234-5678-abcd-12345678abcd') handler.close()
def test_log_record_not_sampled(self, requests_mock): logger = logging.getLogger(self.id()) handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', logging_sampling_rate=0.0, ) logger.addHandler(handler) logger.warning('Hello_World') logger.warning('Hello_World2') logger.warning('Hello_World3') logger.warning('Hello_World4') handler.close() self.assertFalse(requests_mock.called)
def test_export_failure(self, log_record_to_envelope_mock): log_record_to_envelope_mock.return_value = ['bar'] handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', storage_path=os.path.join(TEST_FOLDER, self.id()), ) with mock.patch('opencensus.ext.azure.log_exporter' '.AzureLogHandler._transmit') as transmit: transmit.return_value = 10 handler._export(['foo']) self.assertEqual(len(os.listdir(handler.storage.path)), 1) self.assertIsNone(handler.storage.get()) handler.close()
def test_log_record_with_custom_properties(self, requests_mock): logger = logging.getLogger(self.id()) handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', storage_path=os.path.join(TEST_FOLDER, self.id()), ) logger.addHandler(handler) logger.warning('action', {'key-1': 'value-1', 'key-2': 'value-2'}) handler.close() post_body = requests_mock.call_args_list[0][1]['data'] self.assertTrue('action' in post_body) self.assertTrue('key-1' in post_body) self.assertTrue('key-2' in post_body)
def test_exception(self, requests_mock): logger = logging.getLogger(self.id()) handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', storage_path=os.path.join(TEST_FOLDER, self.id()), ) logger.addHandler(handler) try: return 1 / 0 # generate a ZeroDivisionError except Exception: logger.exception('Captured an exception.') handler.close() self.assertEqual(len(requests_mock.call_args_list), 1) post_body = requests_mock.call_args_list[0][1]['data'] self.assertTrue('ZeroDivisionError' in post_body)
def test_log_record_sampled(self, requests_mock): logger = logging.getLogger(self.id()) handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', logging_sampling_rate=1.0, ) logger.addHandler(handler) logger.warning('Hello_World') logger.warning('Hello_World2') logger.warning('Hello_World3') logger.warning('Hello_World4') handler.close() post_body = requests_mock.call_args_list[0][1]['data'] self.assertTrue('Hello_World' in post_body) self.assertTrue('Hello_World2' in post_body) self.assertTrue('Hello_World3' in post_body) self.assertTrue('Hello_World4' in post_body)
def test_exception_with_custom_properties(self, requests_mock): logger = logging.getLogger(self.id()) handler = log_exporter.AzureLogHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', storage_path=os.path.join(TEST_FOLDER, self.id()), ) logger.addHandler(handler) try: return 1 / 0 # generate a ZeroDivisionError except Exception: properties = { 'custom_dimensions': { 'key_1': 'value_1', 'key_2': 'value_2' } } logger.exception('Captured an exception.', extra=properties) handler.close() self.assertEqual(len(requests_mock.call_args_list), 1) post_body = requests_mock.call_args_list[0][1]['data'] self.assertTrue('ZeroDivisionError' in post_body) self.assertTrue('key_1' in post_body) self.assertTrue('key_2' in post_body)
def test_ctor(self): self.assertRaises(ValueError, lambda: log_exporter.AzureLogHandler( connection_string="", instrumentation_key="")) # noqa: E501
def test_ctor(self): from opencensus.ext.azure.common import Options instrumentation_key = Options._default.instrumentation_key Options._default.instrumentation_key = None self.assertRaises(ValueError, lambda: log_exporter.AzureLogHandler()) Options._default.instrumentation_key = instrumentation_key