From 52dcd97df5225fa872da988328fe2ec66d50c2ec Mon Sep 17 00:00:00 2001 From: justwph <2732352+wph95@users.noreply.github.com> Date: Mon, 20 Oct 2025 22:49:44 +0800 Subject: [PATCH 1/5] [metrics] Refactor OpenTelemetry environment variable handling to use constants Signed-off-by: justwph <2732352+wph95@users.noreply.github.com> --- .../dashboard/modules/reporter/tests/test_reporter.py | 4 ++-- python/ray/tests/BUILD.bazel | 2 +- python/ray/tests/test_metric_cardinality.py | 5 +++-- python/ray/tests/test_metrics_agent.py | 11 +++++++---- python/ray/util/metrics.py | 4 ++-- src/ray/stats/tests/BUILD.bazel | 2 +- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/python/ray/dashboard/modules/reporter/tests/test_reporter.py b/python/ray/dashboard/modules/reporter/tests/test_reporter.py index 8a7704358ded..a06ea4d96a40 100644 --- a/python/ray/dashboard/modules/reporter/tests/test_reporter.py +++ b/python/ray/dashboard/modules/reporter/tests/test_reporter.py @@ -204,9 +204,9 @@ def enable_open_telemetry(request): Fixture to enable OpenTelemetry for the test. """ if request.param: - os.environ["RAY_enable_open_telemetry"] = "1" + os.environ["RAY_enable_open_telemetry"] = "true" else: - os.environ["RAY_enable_open_telemetry"] = "0" + os.environ["RAY_enable_open_telemetry"] = "false" yield os.environ.pop("RAY_enable_open_telemetry", None) diff --git a/python/ray/tests/BUILD.bazel b/python/ray/tests/BUILD.bazel index 98018df64c37..22d2e9a6f8b4 100644 --- a/python/ray/tests/BUILD.bazel +++ b/python/ray/tests/BUILD.bazel @@ -83,7 +83,7 @@ py_test_module_list( py_test_module_list( size = "medium", env = { - "RAY_enable_open_telemetry": "1", + "RAY_enable_open_telemetry": "true", }, files = [ "test_metric_cardinality.py", diff --git a/python/ray/tests/test_metric_cardinality.py b/python/ray/tests/test_metric_cardinality.py index 450345e76fcc..df25a6327911 100644 --- a/python/ray/tests/test_metric_cardinality.py +++ b/python/ray/tests/test_metric_cardinality.py @@ -18,6 +18,7 @@ TASK_OR_ACTOR_NAME_TAG_KEY, ) +from ray._private.ray_constants import RAY_ENABLE_OPEN_TELEMETRY try: import prometheus_client @@ -41,7 +42,7 @@ def _setup_cluster_for_test(request, ray_start_cluster): "metrics_report_interval_ms": 1000, "enable_metrics_collection": True, "metric_cardinality_level": core_metric_cardinality_level, - "enable_open_telemetry": os.getenv("RAY_enable_open_telemetry") == "1", + "enable_open_telemetry": RAY_ENABLE_OPEN_TELEMETRY, } ) cluster.wait_for_nodes() @@ -151,7 +152,7 @@ def test_cardinality_recommended_and_legacy_levels( # implementation doesn't support low cardinality. @pytest.mark.skipif(prometheus_client is None, reason="Prometheus not installed") @pytest.mark.skipif( - os.getenv("RAY_enable_open_telemetry") != "1", + not RAY_ENABLE_OPEN_TELEMETRY, reason="OpenTelemetry is not enabled", ) @pytest.mark.parametrize( diff --git a/python/ray/tests/test_metrics_agent.py b/python/ray/tests/test_metrics_agent.py index 4f3814a44a01..7857bc24d3ec 100644 --- a/python/ray/tests/test_metrics_agent.py +++ b/python/ray/tests/test_metrics_agent.py @@ -22,7 +22,10 @@ Gauge as MetricsAgentGauge, PrometheusServiceDiscoveryWriter, ) -from ray._private.ray_constants import PROMETHEUS_SERVICE_DISCOVERY_FILE +from ray._private.ray_constants import ( + PROMETHEUS_SERVICE_DISCOVERY_FILE, + RAY_ENABLE_OPEN_TELEMETRY, +) from ray._private.test_utils import ( PrometheusTimeseries, fetch_prometheus_metric_timeseries, @@ -205,7 +208,7 @@ def _setup_cluster_for_test(request, ray_start_cluster): "event_stats_print_interval_ms": 500, "event_stats": True, "enable_metrics_collection": enable_metrics_collection, - "enable_open_telemetry": os.getenv("RAY_enable_open_telemetry") == "1", + "enable_open_telemetry": RAY_ENABLE_OPEN_TELEMETRY, } ) # Add worker nodes. @@ -321,7 +324,7 @@ def test_cases(): "test_driver_counter_total", "test_gauge", ] - if os.environ.get("RAY_enable_open_telemetry") != "1" + if not RAY_ENABLE_OPEN_TELEMETRY else [ "test_counter_total", "test_driver_counter_total", @@ -745,7 +748,7 @@ def wrap_test_case_for_retry(): @pytest.mark.skipif(sys.platform == "win32", reason="Not working in Windows.") @pytest.mark.skipif( - os.environ.get("RAY_enable_open_telemetry") == "1", + RAY_ENABLE_OPEN_TELEMETRY, reason="OpenTelemetry backend does not support Counter exported as gauge.", ) def test_counter_exported_as_gauge(shutdown_only): diff --git a/python/ray/util/metrics.py b/python/ray/util/metrics.py index d3e3e6da8def..c390162cc35c 100644 --- a/python/ray/util/metrics.py +++ b/python/ray/util/metrics.py @@ -1,9 +1,9 @@ import logging -import os import re import warnings from typing import Any, Dict, List, Optional, Tuple, Union +from ray._private.ray_constants import RAY_ENABLE_OPEN_TELEMETRY from ray._raylet import ( Count as CythonCount, Gauge as CythonGauge, @@ -198,7 +198,7 @@ def __init__( if self._discard_metric: self._metric = None else: - if os.environ.get("RAY_enable_open_telemetry") == "1": + if RAY_ENABLE_OPEN_TELEMETRY: """ For the new opentelemetry implementation, we'll correctly use Counter rather than Sum. diff --git a/src/ray/stats/tests/BUILD.bazel b/src/ray/stats/tests/BUILD.bazel index ac78d6de4d00..eee623ee0ca2 100644 --- a/src/ray/stats/tests/BUILD.bazel +++ b/src/ray/stats/tests/BUILD.bazel @@ -5,7 +5,7 @@ ray_cc_test( size = "small", srcs = ["metric_with_open_telemetry_test.cc"], env = { - "RAY_enable_open_telemetry": "1", + "RAY_enable_open_telemetry": "true", }, tags = ["team:core"], deps = [ From f3165897cea178e3553a0a842d7c5b56f97c8465 Mon Sep 17 00:00:00 2001 From: justwph <2732352+wph95@users.noreply.github.com> Date: Thu, 23 Oct 2025 15:57:11 +0800 Subject: [PATCH 2/5] revert metrics.py change Signed-off-by: justwph <2732352+wph95@users.noreply.github.com> --- python/ray/util/metrics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/ray/util/metrics.py b/python/ray/util/metrics.py index c390162cc35c..d3e3e6da8def 100644 --- a/python/ray/util/metrics.py +++ b/python/ray/util/metrics.py @@ -1,9 +1,9 @@ import logging +import os import re import warnings from typing import Any, Dict, List, Optional, Tuple, Union -from ray._private.ray_constants import RAY_ENABLE_OPEN_TELEMETRY from ray._raylet import ( Count as CythonCount, Gauge as CythonGauge, @@ -198,7 +198,7 @@ def __init__( if self._discard_metric: self._metric = None else: - if RAY_ENABLE_OPEN_TELEMETRY: + if os.environ.get("RAY_enable_open_telemetry") == "1": """ For the new opentelemetry implementation, we'll correctly use Counter rather than Sum. From a5edb599f7c8d68df6994fe5e2bdd7604da5c89b Mon Sep 17 00:00:00 2001 From: justwph <2732352+wph95@users.noreply.github.com> Date: Thu, 23 Oct 2025 16:01:18 +0800 Subject: [PATCH 3/5] improve metrics.py Signed-off-by: justwph <2732352+wph95@users.noreply.github.com> --- python/ray/util/metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ray/util/metrics.py b/python/ray/util/metrics.py index d3e3e6da8def..f39cb17c153c 100644 --- a/python/ray/util/metrics.py +++ b/python/ray/util/metrics.py @@ -198,7 +198,7 @@ def __init__( if self._discard_metric: self._metric = None else: - if os.environ.get("RAY_enable_open_telemetry") == "1": + if os.environ.get("RAY_enable_open_telemetry") == "true": """ For the new opentelemetry implementation, we'll correctly use Counter rather than Sum. From b37c1823c715cd409ae0823fe7f22a90aba5d357 Mon Sep 17 00:00:00 2001 From: justwph <2732352+wph95@users.noreply.github.com> Date: Thu, 23 Oct 2025 21:00:03 +0800 Subject: [PATCH 4/5] improve metrics.py Signed-off-by: justwph <2732352+wph95@users.noreply.github.com> --- python/ray/util/metrics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/ray/util/metrics.py b/python/ray/util/metrics.py index f39cb17c153c..7718ca6a71d0 100644 --- a/python/ray/util/metrics.py +++ b/python/ray/util/metrics.py @@ -1,9 +1,9 @@ import logging -import os import re import warnings from typing import Any, Dict, List, Optional, Tuple, Union +from ray._private.ray_constants import env_bool from ray._raylet import ( Count as CythonCount, Gauge as CythonGauge, @@ -198,7 +198,7 @@ def __init__( if self._discard_metric: self._metric = None else: - if os.environ.get("RAY_enable_open_telemetry") == "true": + if env_bool("RAY_enable_open_telemetry", False): """ For the new opentelemetry implementation, we'll correctly use Counter rather than Sum. From 27b0aa72ecc0fa885f1a33978ea1c8c70ef8d25d Mon Sep 17 00:00:00 2001 From: justwph <2732352+wph95@users.noreply.github.com> Date: Wed, 5 Nov 2025 21:09:57 +0800 Subject: [PATCH 5/5] fix typo Signed-off-by: justwph <2732352+wph95@users.noreply.github.com> --- python/ray/util/metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ray/util/metrics.py b/python/ray/util/metrics.py index 7718ca6a71d0..75f0aa5237c5 100644 --- a/python/ray/util/metrics.py +++ b/python/ray/util/metrics.py @@ -198,7 +198,7 @@ def __init__( if self._discard_metric: self._metric = None else: - if env_bool("RAY_enable_open_telemetry", False): + if env_bool("RAY_enable_open_telemetry", False): """ For the new opentelemetry implementation, we'll correctly use Counter rather than Sum.