def _setup_cluster_for_test(ray_start_cluster): NUM_NODES = 2 cluster = ray_start_cluster # Add a head node. cluster.add_node(_system_config={"metrics_report_interval_ms": 1000}) # Add worker nodes. [cluster.add_node() for _ in range(NUM_NODES - 1)] cluster.wait_for_nodes() ray.init(address=cluster.address) worker_should_exit = SignalActor.remote() # Generate a metric in the driver. counter = Count("test_driver_counter", description="desc") counter.inc() # Generate some metrics from actor & tasks. @ray.remote def f(): counter = Count("test_counter", description="desc") counter.inc() counter = ray.get(ray.put(counter)) # Test serialization. counter.inc() ray.get(worker_should_exit.wait.remote()) @ray.remote class A: async def ping(self): histogram = Histogram("test_histogram", description="desc", boundaries=[0.1, 1.6]) histogram = ray.get(ray.put(histogram)) # Test serialization. histogram.record(1.5) ray.get(worker_should_exit.wait.remote()) a = A.remote() obj_refs = [f.remote(), a.ping.remote()] node_info_list = ray.nodes() prom_addresses = [] for node_info in node_info_list: metrics_export_port = node_info["MetricsExportPort"] addr = node_info["NodeManagerAddress"] prom_addresses.append(f"{addr}:{metrics_export_port}") yield prom_addresses ray.get(worker_should_exit.send.remote()) ray.get(obj_refs) ray.shutdown() cluster.shutdown()
def test_basic_custom_metrics(metric_mock): # Make sure each of metric works as expected. # -- Count -- count = Count("count", tag_keys=("a", )) with pytest.raises(TypeError): count.inc("hi") with pytest.raises(ValueError): count.inc(0) count.inc(-1) count._metric = metric_mock count.record(1, {"a": "1"}) metric_mock.record.assert_called_with(1, tags={"a": "1"}) # -- Gauge -- gauge = Gauge("gauge", description="gauge") gauge._metric = metric_mock gauge.record(4) metric_mock.record.assert_called_with(4, tags={}) # -- Histogram histogram = Histogram( "hist", description="hist", boundaries=[1.0, 3.0], tag_keys=("a", "b")) histogram._metric = metric_mock tags = {"a": "10", "b": "b"} histogram.record(8, tags=tags) metric_mock.record.assert_called_with(8, tags=tags)
def f(): counter = Count("test_counter", description="desc") counter.inc() counter = ray.get(ray.put(counter)) # Test serialization. counter.inc() ray.get(worker_should_exit.wait.remote())