-
Notifications
You must be signed in to change notification settings - Fork 7k
Refactor OpenTelemetry environment variable handling to use ray_constants #57910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… constants Signed-off-by: justwph <2732352+wph95@users.noreply.github.com>
|
@can-anyscale PTAL |
There was a problem hiding this 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.
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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) |
python/ray/util/metrics.py
Outdated
| import warnings | ||
| from typing import Any, Dict, List, Optional, Tuple, Union | ||
|
|
||
| from ray._private.ray_constants import RAY_ENABLE_OPEN_TELEMETRY |
There was a problem hiding this comment.
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()
Signed-off-by: justwph <2732352+wph95@users.noreply.github.com>
Signed-off-by: justwph <2732352+wph95@users.noreply.github.com>
Bug: OpenTelemetry Configuration Breaks Backward CompatibilityThe |
Signed-off-by: justwph <2732352+wph95@users.noreply.github.com>
Bug: Ray Metrics Module Breaks Backward CompatibilityThe |
Bug: Test Configuration Issue with Environment VariableReplacing Additional Locations (1) |
Bug: Open Telemetry Configuration Not DynamicThe Additional Locations (4) |
can-anyscale
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fb7fc5d to
27b0aa7
Compare
| self._metric = None | ||
| else: | ||
| if os.environ.get("RAY_enable_open_telemetry") == "1": | ||
| if env_bool("RAY_enable_open_telemetry", False): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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
|
Thank you for your contribution |
…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>
…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>
…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>
…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>
…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>

Remove direct environment variable access patterns
os.getenv("RAY_enable_open_telemetry") == "1"ray_constants.RAY_ENABLE_OPEN_TELEMETRYconsistently throughout the codebaseUnify default value format for RAY_enable_open_telemetry
"true"|"false""1"and"true", which is now unifiedBackward compatibility maintained
RAY_ENABLE_OPEN_TELEMETRYconstant properly handles both"1"and"true"valuesenv_boolhelper function already supports both formats:Most of the current code uses:
RAY_enable_open_telemetry: "1"A smaller portion (not zero) uses:
RAY_enable_open_telemetry: "true"ray/python/ray/tests/test_metrics_agent.py
Line 497 in fe7ad00
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".