def test_log_record_sampled(self, requests_mock): logger = logging.getLogger(self.id()) handler = log_exporter.AzureEventHandler( 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_log_record_with_custom_properties(self, requests_mock): logger = logging.getLogger(self.id()) handler = log_exporter.AzureEventHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', storage_path=os.path.join(TEST_FOLDER, self.id()), ) logger.addHandler(handler) logger.warning('action', extra={ 'custom_dimensions': { '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_with_custom_properties(self, requests_mock): logger = logging.getLogger(self.id()) handler = log_exporter.AzureEventHandler( 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_invalid_sampling_rate(self): with self.assertRaises(ValueError): log_exporter.AzureEventHandler( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', logging_sampling_rate=4.0, )
def test_ctor(self): self.assertRaises(ValueError, lambda: log_exporter.AzureEventHandler( 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.AzureEventHandler()) Options._default.instrumentation_key = instrumentation_key