def test_get_exporter_cls(self): res1 = get_exporter_cls("Stackdriver") res2 = get_exporter_cls("Pubsub") res3 = get_exporter_cls("Bigquery") self.assertEqual(res1.__name__, "StackdriverExporter") self.assertEqual(res2.__name__, "PubsubExporter") self.assertEqual(res3.__name__, "BigqueryExporter") with self.assertRaises(SystemExit): get_exporter_cls("UndefinedExporter")
def test_get_exporter_cls(self): res1 = get_exporter_cls("Stackdriver") res2 = get_exporter_cls("Pubsub") res3 = get_exporter_cls("Bigquery") self.assertEqual(res1.__name__, "StackdriverExporter") self.assertEqual(res1.__module__, "slo_generator.exporters.stackdriver") self.assertEqual(res2.__name__, "PubsubExporter") self.assertEqual(res2.__module__, "slo_generator.exporters.pubsub") self.assertEqual(res3.__name__, "BigqueryExporter") self.assertEqual(res3.__module__, "slo_generator.exporters.bigquery") with self.assertRaises(ModuleNotFoundError): get_exporter_cls("UndefinedExporter")
def export(data, exporters): """Export data using selected exporters. Args: data (dict): Data to export. exporters (list): List of exporter configurations. Returns: obj: Return values from exporters output. """ LOGGER.debug(f'Exporters: {pprint.pformat(exporters)}') LOGGER.debug(f'Data: {pprint.pformat(data)}') results = [] # Passing one exporter as a dict will work for convenience if isinstance(exporters, dict): exporters = [exporters] for config in exporters: LOGGER.debug(f'Exporter config: {pprint.pformat(config)}') exporter_class = config.get('class') LOGGER.info(f'Exporting results to {exporter_class}') exporter = utils.get_exporter_cls(exporter_class)() ret = exporter.export(data, **config) results.append(ret) LOGGER.debug(f'Exporter return: {pprint.pformat(ret)}')
def export(data, exporters, raise_on_error=False): """Export data using selected exporters. Args: data (dict): Data to export. exporters (list): List of exporter configurations. Returns: obj: Return values from exporters output. """ LOGGER.debug(f'Exporters: {pprint.pformat(exporters)}') LOGGER.debug(f'Data: {pprint.pformat(data)}') responses = [] # Passing one exporter as a dict will work for convenience if isinstance(exporters, dict): exporters = [exporters] for config in exporters: try: exporter_class = config.get('class') LOGGER.info(f'Exporting results to {exporter_class}') LOGGER.debug(f'Exporter config: {pprint.pformat(config)}') exporter = utils.get_exporter_cls(exporter_class)() response = exporter.export(data, **config) if isinstance(response, list): for elem in response: elem['exporter'] = exporter_class responses.append(response) except Exception as exc: # pylint: disable=broad-except LOGGER.critical(exc, exc_info=True) LOGGER.error(f'{exporter_class}Exporter failed. Passing.') if raise_on_error: raise exc responses.append(exc) return responses
def test_get_exporter_dynamic_cls(self): res1 = get_exporter_cls("pathlib.Path") self.assertEqual(res1.__name__, "Path") self.assertEqual(res1.__module__, "pathlib") with self.assertRaises(ModuleNotFoundError): get_exporter_cls("foo.bar.DoesNotExist")
def test_get_backend_dynamic_cls(self): res1 = get_backend_cls("pathlib.Path") self.assertEqual(res1.__name__, "Path") self.assertEqual(res1.__module__, "pathlib") with self.assertRaises(SystemExit): get_exporter_cls("foo.bar.DoesNotExist")