Skip to content

Commit 7a93c25

Browse files
authored
Refactor OpenTelemetry environment variable handling to use ray_constants (#57910)
1. **Remove direct environment variable access patterns** - Replace all instances of `os.getenv("RAY_enable_open_telemetry") == "1"` - Standardize to use `ray_constants.RAY_ENABLE_OPEN_TELEMETRY` consistently throughout the codebase 2. **Unify default value format for RAY_enable_open_telemetry** - Standardize the default value to `"true"` | `"false"` - Previously, the codebase had mixed usage of `"1"` and `"true"`, which is now unified 3. **Backward compatibility maintained** - Carefully verified that the existing `RAY_ENABLE_OPEN_TELEMETRY` constant properly handles both `"1"` and `"true"` values - This change will not introduce any breaking behavior - The `env_bool` helper function already supports both formats: ```python RAY_ENABLE_OPEN_TELEMETRY = env_bool("RAY_enable_open_telemetry", False) def env_bool(key, default): if key in os.environ: return ( True if os.environ[key].lower() == "true" or os.environ[key] == "1" else False ) return default ``` --- Most of the current code uses: `RAY_enable_open_telemetry: "1"` A smaller portion (not zero) uses: `RAY_enable_open_telemetry: "true"` https://github.com/ray-project/ray/blob/fe7ad00f9720a722fde5fecba5bb681234bcdb63/python/ray/tests/test_metrics_agent.py#L497 My personal preference is "true"—it’s concise and unambiguous. If it’s "1", I have to think/guess whether it means "true" or "false". --------- Signed-off-by: justwph <[email protected]>
1 parent 33ba50e commit 7a93c25

File tree

6 files changed

+16
-12
lines changed

6 files changed

+16
-12
lines changed

python/ray/dashboard/modules/reporter/tests/test_reporter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ def enable_open_telemetry(request):
204204
Fixture to enable OpenTelemetry for the test.
205205
"""
206206
if request.param:
207-
os.environ["RAY_enable_open_telemetry"] = "1"
207+
os.environ["RAY_enable_open_telemetry"] = "true"
208208
else:
209-
os.environ["RAY_enable_open_telemetry"] = "0"
209+
os.environ["RAY_enable_open_telemetry"] = "false"
210210
yield
211211
os.environ.pop("RAY_enable_open_telemetry", None)
212212

python/ray/tests/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ py_test_module_list(
8585
py_test_module_list(
8686
size = "medium",
8787
env = {
88-
"RAY_enable_open_telemetry": "1",
88+
"RAY_enable_open_telemetry": "true",
8989
},
9090
files = [
9191
"test_metric_cardinality.py",

python/ray/tests/test_metric_cardinality.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
TASK_OR_ACTOR_NAME_TAG_KEY,
1919
)
2020

21+
from ray._private.ray_constants import RAY_ENABLE_OPEN_TELEMETRY
2122

2223
try:
2324
import prometheus_client
@@ -41,7 +42,7 @@ def _setup_cluster_for_test(request, ray_start_cluster):
4142
"metrics_report_interval_ms": 1000,
4243
"enable_metrics_collection": True,
4344
"metric_cardinality_level": core_metric_cardinality_level,
44-
"enable_open_telemetry": os.getenv("RAY_enable_open_telemetry") == "1",
45+
"enable_open_telemetry": RAY_ENABLE_OPEN_TELEMETRY,
4546
}
4647
)
4748
cluster.wait_for_nodes()
@@ -151,7 +152,7 @@ def test_cardinality_recommended_and_legacy_levels(
151152
# implementation doesn't support low cardinality.
152153
@pytest.mark.skipif(prometheus_client is None, reason="Prometheus not installed")
153154
@pytest.mark.skipif(
154-
os.getenv("RAY_enable_open_telemetry") != "1",
155+
not RAY_ENABLE_OPEN_TELEMETRY,
155156
reason="OpenTelemetry is not enabled",
156157
)
157158
@pytest.mark.parametrize(

python/ray/tests/test_metrics_agent.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
Gauge as MetricsAgentGauge,
2323
PrometheusServiceDiscoveryWriter,
2424
)
25-
from ray._private.ray_constants import PROMETHEUS_SERVICE_DISCOVERY_FILE
25+
from ray._private.ray_constants import (
26+
PROMETHEUS_SERVICE_DISCOVERY_FILE,
27+
RAY_ENABLE_OPEN_TELEMETRY,
28+
)
2629
from ray._private.test_utils import (
2730
PrometheusTimeseries,
2831
fetch_prometheus_metric_timeseries,
@@ -204,7 +207,7 @@ def _setup_cluster_for_test(request, ray_start_cluster):
204207
"event_stats_print_interval_ms": 500,
205208
"event_stats": True,
206209
"enable_metrics_collection": enable_metrics_collection,
207-
"enable_open_telemetry": os.getenv("RAY_enable_open_telemetry") == "1",
210+
"enable_open_telemetry": RAY_ENABLE_OPEN_TELEMETRY,
208211
}
209212
)
210213
# Add worker nodes.
@@ -320,7 +323,7 @@ def test_cases():
320323
"test_driver_counter_total",
321324
"test_gauge",
322325
]
323-
if os.environ.get("RAY_enable_open_telemetry") != "1"
326+
if not RAY_ENABLE_OPEN_TELEMETRY
324327
else [
325328
"test_counter_total",
326329
"test_driver_counter_total",
@@ -744,7 +747,7 @@ def wrap_test_case_for_retry():
744747

745748
@pytest.mark.skipif(sys.platform == "win32", reason="Not working in Windows.")
746749
@pytest.mark.skipif(
747-
os.environ.get("RAY_enable_open_telemetry") == "1",
750+
RAY_ENABLE_OPEN_TELEMETRY,
748751
reason="OpenTelemetry backend does not support Counter exported as gauge.",
749752
)
750753
def test_counter_exported_as_gauge(shutdown_only):

python/ray/util/metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import logging
2-
import os
32
import re
43
import warnings
54
from typing import Any, Dict, List, Optional, Tuple, Union
65

6+
from ray._private.ray_constants import env_bool
77
from ray._raylet import (
88
Count as CythonCount,
99
Gauge as CythonGauge,
@@ -198,7 +198,7 @@ def __init__(
198198
if self._discard_metric:
199199
self._metric = None
200200
else:
201-
if os.environ.get("RAY_enable_open_telemetry") == "1":
201+
if env_bool("RAY_enable_open_telemetry", False):
202202
"""
203203
For the new opentelemetry implementation, we'll correctly use Counter
204204
rather than Sum.

src/ray/stats/tests/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ray_cc_test(
55
size = "small",
66
srcs = ["metric_with_open_telemetry_test.cc"],
77
env = {
8-
"RAY_enable_open_telemetry": "1",
8+
"RAY_enable_open_telemetry": "true",
99
},
1010
tags = ["team:core"],
1111
deps = [

0 commit comments

Comments
 (0)