Skip to content

Conversation

@wph95
Copy link
Contributor

@wph95 wph95 commented Oct 20, 2025

  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:
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"

"RAY_enable_open_telemetry": "true",

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".

… constants

Signed-off-by: justwph <2732352+wph95@users.noreply.github.com>
@wph95 wph95 requested a review from a team as a code owner October 20, 2025 14:52
@edoakes
Copy link
Collaborator

edoakes commented Oct 20, 2025

@can-anyscale PTAL

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the handling of the RAY_enable_open_telemetry environment variable to consistently use the ray_constants.RAY_ENABLE_OPEN_TELEMETRY boolean constant. This is a great improvement as it centralizes the logic, ensures consistent behavior for both "1" and "true" values, and fixes latent bugs where the string "true" was not correctly handled. The changes are applied across test files and build configurations, standardizing on "true"/"false" values. The code is cleaner and more robust. I have one minor suggestion for further improvement.

Comment on lines 206 to 211
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and avoid magic strings, you can define the environment variable name as a local variable. This also allows for making the code more concise.

Suggested change
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)
env_var = "RAY_enable_open_telemetry"
os.environ[env_var] = "true" if request.param else "false"
yield
os.environ.pop(env_var, None)

cursor[bot]

This comment was marked as outdated.

@ray-gardener ray-gardener bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Oct 20, 2025
import warnings
from typing import Any, Dict, List, Optional, Tuple, Union

from ray._private.ray_constants import RAY_ENABLE_OPEN_TELEMETRY
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one of the bot's comments might be valid, this import is likely safe only for internal Ray components. Since ray.util.metrics is a public API, its behavior could be undefined if a program sets different values for RAY_ENABLE_OPEN_TELEMETRY.

import ray
from ray.util import metrics

os.environ["RAY_enable_open_telemetry"] = "true"
ray.init()

wph95 added 3 commits October 23, 2025 15:57
Signed-off-by: justwph <2732352+wph95@users.noreply.github.com>
Signed-off-by: justwph <2732352+wph95@users.noreply.github.com>
@wph95 wph95 requested a review from can-anyscale October 23, 2025 08:01
@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: OpenTelemetry Configuration Breaks Backward Compatibility

The ray.util.metrics classes (e.g., Counter) now require RAY_enable_open_telemetry="true" to enable OpenTelemetry, breaking backward compatibility for users who set it to "1". This bypasses the RAY_ENABLE_OPEN_TELEMETRY constant, which is designed to handle both "1" and "true" values, contradicting the PR's backward compatibility claim.

Fix in Cursor Fix in Web

@wph95 wph95 closed this Oct 23, 2025
@wph95 wph95 reopened this Oct 23, 2025
Signed-off-by: justwph <2732352+wph95@users.noreply.github.com>
@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Ray Metrics Module Breaks Backward Compatibility

The ray.util.metrics module now checks for RAY_enable_open_telemetry == "true", breaking backward compatibility for users who previously set it to "1". This causes the Counter class to incorrectly use CythonSum instead of CythonCount, which contradicts the PR's claim of maintaining backward compatibility.

Fix in Cursor Fix in Web

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Test Configuration Issue with Environment Variable

Replacing os.getenv("RAY_enable_open_telemetry") with the RAY_ENABLE_OPEN_TELEMETRY constant means test fixtures, like _setup_cluster_for_test, now use an import-time value. This can lead to stale enable_open_telemetry configurations if the environment variable changes after module import but before the fixture runs, affecting tests in test_metric_cardinality.py and test_metrics_agent.py.

Additional Locations (1)

Fix in Cursor Fix in Web

@cursor
Copy link

cursor bot commented Oct 23, 2025

Bug: Open Telemetry Configuration Not Dynamic

The RAY_ENABLE_OPEN_TELEMETRY constant is evaluated at module import time. This means if the RAY_enable_open_telemetry environment variable changes after module import but before a fixture or test uses it, the code will use a stale value. This regresses from previous dynamic environment checks.

Additional Locations (4)

Fix in Cursor Fix in Web

Copy link
Contributor

@can-anyscale can-anyscale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@can-anyscale can-anyscale added the go add ONLY when ready to merge, run all tests label Nov 3, 2025
Signed-off-by: justwph <2732352+wph95@users.noreply.github.com>
@wph95 wph95 force-pushed the fix-hard-code-env branch from fb7fc5d to 27b0aa7 Compare November 5, 2025 13:10
self._metric = None
else:
if os.environ.get("RAY_enable_open_telemetry") == "1":
if env_bool("RAY_enable_open_telemetry", False):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Inconsistent Env Evaluation Between Metrics and Ray Internal Measures

Inconsistent environment variable evaluation timing. The change uses env_bool("RAY_enable_open_telemetry", False) which evaluates the environment variable at runtime (when Counter.init is called), while other parts of the codebase use the RAY_ENABLE_OPEN_TELEMETRY constant which is evaluated at module import time. This creates undefined behavior when the environment variable is set after importing ray.util.metrics but before creating a Counter instance. Since ray.util.metrics is a public API, users could set the environment variable at any time, leading to inconsistent behavior between the metrics module and internal Ray components. The fix should import and use RAY_ENABLE_OPEN_TELEMETRY constant instead of calling env_bool() at runtime.

Fix in Cursor Fix in Web

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it makes sense for test to evaluate at import time; in non-test code, it is consistently that the value will be evaluated at runtime

self._metric = None
else:
if os.environ.get("RAY_enable_open_telemetry") == "1":
if env_bool("RAY_enable_open_telemetry", False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it makes sense for test to evaluate at import time; in non-test code, it is consistently that the value will be evaluated at runtime

@can-anyscale
Copy link
Contributor

Thank you for your contribution

@can-anyscale can-anyscale merged commit 7a93c25 into ray-project:master Nov 7, 2025
6 checks passed
YoussefEssDS pushed a commit to YoussefEssDS/ray that referenced this pull request Nov 8, 2025
…ants (ray-project#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 <2732352+wph95@users.noreply.github.com>
landscapepainter pushed a commit to landscapepainter/ray that referenced this pull request Nov 17, 2025
…ants (ray-project#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 <2732352+wph95@users.noreply.github.com>
Aydin-ab pushed a commit to Aydin-ab/ray-aydin that referenced this pull request Nov 19, 2025
…ants (ray-project#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 <2732352+wph95@users.noreply.github.com>
Signed-off-by: Aydin Abiar <aydin@anyscale.com>
ykdojo pushed a commit to ykdojo/ray that referenced this pull request Nov 27, 2025
…ants (ray-project#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 <2732352+wph95@users.noreply.github.com>
Signed-off-by: YK <1811651+ykdojo@users.noreply.github.com>
SheldonTsen pushed a commit to SheldonTsen/ray that referenced this pull request Dec 1, 2025
…ants (ray-project#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 <2732352+wph95@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants