def test_create_database(self): r1 = InfluxReporter(registry=self.registry, autocreate_database=True) with mock.patch("pyformance.reporters.influx.urlopen") as patch: r1.report_now() if patch.call_count != 2: raise AssertionError( "Expected 2 calls to 'urlopen'. Received: {}".format(patch.call_count))
def test_report_now(self): influx_reporter = InfluxReporter(registry=self.registry) with mock.patch( "pyformance.reporters.influx.urllib.request.urlopen") as patch: influx_reporter.report_now() patch.assert_called()
def test_combined_events_with_counter(self): tags = {"host": "server1"} self.registry._clock = self.clock event = self.registry.event(key="event", tags=tags) event.add({"field": 1}) counter = self.registry.counter("event", tags=tags) counter.inc(5) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = [ "event,host=server1 count=5 " + self.clock.time_string(), "event,host=server1 field=1 " + self.clock.time_string() ] send_mock.assert_called_once_with(expected_url, "\n".join(expected_data))
def test_create_database(self): r1 = InfluxReporter(registry=self.registry, autocreate_database=True) with mock.patch("pyformance.reporters.influx.urlopen") as patch: r1.report_now() if patch.call_count != 2: raise AssertionError( "Expected 2 calls to 'urlopen'. Received: {}".format( patch.call_count))
def test_gauge_without_tags(self): self.registry.gauge("cpu").set_value(65) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "cpu value=65 " + self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data)
def test_gauge_with_tags(self): tags = {"region": "us - west"} self.registry.gauge(key="cpu", tags=tags).set_value(65) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send", wraps=influx_reporter._try_send) as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "cpu,region=us\\ -\\ west value=65 " + \ self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data)
def test_counter_with_tags(self): tags = {"host": "server1"} counter = self.registry.counter(key="cpu", tags=tags) for i in range(5): counter.inc(1) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "cpu,host=server1 count=5 " + \ self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data)
def test_gauge_with_global_tags(self): tags = {"region": "us-west-2"} self.registry.gauge(key="cpu", tags=tags).set_value(65) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False, global_tags={ "stage": "dev", "region": "override" }) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "cpu,stage=dev,region=us-west-2 value=65 " + \ self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data)
def test_events_with_tags(self): tags = {"host": "server1"} self.registry._clock = self.clock event = self.registry.event(key="event", tags=tags) event.add({"field": 1, "float": 0.12, "int": MarkInt(1)}) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "event,host=server1 field=1,float=0.12,int=1i " + \ self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data)
def fetch_docs(base_path, es_host, es_index, es_query=None, limit=-1): exp_name = "fetch_docs" exp_path = f"{base_path}/{exp_name}" os.makedirs(exp_path, exist_ok=True) run = mlflow.start_run(experiment_id=get_or_create_experiment_id(exp_name)) docs_path = f"{exp_path}/{run.run_info.run_uuid}" registry = MetricsRegistry() mlflow_reporter = MlflowReporter(registry=registry, active_run=run, reporting_interval=10) mlflow_reporter.start() influx_reporter = InfluxReporter(registry=registry, reporting_interval=10, autocreate_database=True) influx_reporter.start() try: mlflow.log_param("docs_path", docs_path) mlflow.log_param("es_host", es_host) mlflow.log_param("es_index", es_index) mlflow.log_param("es_query", es_query) _write_docs( _get_docs_scrolled(registry, es_host, es_index, es_query, limit), docs_path) influx_reporter.report_now() mlflow_reporter.report_now() mlflow.end_run() except Exception as e: mlflow.end_run("FAILED") raise e finally: influx_reporter.stop() mlflow_reporter.stop() return run
def test_create_database(self): r1 = InfluxReporter(registry=self.registry, autocreate_database=True) with mock.patch("influxdb.InfluxDBClient.write_points") as patch: r1.report_now() patch.assert_called()
def test_report_now(self): influx_reporter = InfluxReporter(registry=self.registry) with mock.patch("influxdb.InfluxDBClient.write_points") as patch: influx_reporter.report_now() patch.assert_called()
def test_report_now(self): influx_reporter = InfluxReporter(registry=self.registry) with mock.patch("pyformance.reporters.influx.urlopen") as patch: influx_reporter.report_now() patch.assert_called()