Skip to content

Commit 391fe4c

Browse files
committed
feat(o11y): refactor resolve_feature_flags to be generic and strictly follow precedence
1 parent c6fe8a6 commit 391fe4c

1 file changed

Lines changed: 61 additions & 44 deletions

File tree

  • packages/google-api-core/google/api_core/observability

packages/google-api-core/google/api_core/observability/options.py

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -72,62 +72,79 @@ def _get_env_bool(name: str) -> Optional[bool]:
7272
return None
7373

7474

75-
def _get_env_bool_with_dev_fallback(name: str) -> Optional[bool]:
76-
"""Retrieve the boolean value of an environment variable, checking experimental fallbacks first."""
77-
if name.startswith("GOOGLE_SDK_"):
78-
exp_name = name.replace("GOOGLE_SDK_", "GOOGLE_SDK_EXPERIMENTAL_", 1)
79-
val = _get_env_bool(exp_name)
80-
if val is not None:
81-
return val
82-
return _get_env_bool(name)
75+
def _has_provider(
76+
client_options: Optional[Union[Dict[str, Any], Any]], provider_key: str
77+
) -> bool:
78+
"""Checks if a specific provider key is present and not None in client_options."""
79+
if client_options is None:
80+
return False
81+
82+
options_dict = (
83+
client_options
84+
if isinstance(client_options, dict)
85+
else getattr(client_options, "__dict__", {})
86+
)
87+
return options_dict.get(provider_key) is not None
8388

8489

8590
def resolve_feature_flags(
86-
feature_name: str,
91+
env_var: str,
92+
provider_key: str,
8793
client_options: Optional[Union[Dict[str, Any], Any]] = None,
88-
default: bool = False,
8994
) -> bool:
90-
"""Determines if a telemetry signal is enabled.
95+
"""Determines if a feature is enabled based on environment variables and client options.
96+
97+
Behavior depends on whether the `env_var` name contains "EXPERIMENTAL":
9198
92-
Resolves settings in the following order of precedence:
93-
1. Programmatic overrides in client_options (checks tracer_provider)
94-
2. Language-wide Environment Variable: GOOGLE_SDK_PYTHON_TRACING_ENABLED
95-
(natively checks for a variant with an "EXPERIMENTAL" token first)
96-
3. Default fallback
99+
- **Experimental Path** (env_var contains "EXPERIMENTAL"):
100+
Strict gating. Requires the environment variable to be explicitly 'true'.
101+
If a programmatic provider is passed but the environment variable is not 'true',
102+
raises ValueError (Fail Fast).
103+
104+
- **GA Path** (env_var does not contain "EXPERIMENTAL"):
105+
Standard precedence. Enabled if a programmatic provider is passed,
106+
otherwise falls back to the environment variable value.
97107
98108
Args:
99-
feature_name: The feature name: must be 'tracing'.
109+
env_var: The name of the environment variable controlling this feature.
110+
provider_key: The key in client_options/attributes for the programmatic provider.
100111
client_options: A dictionary or object containing client configuration.
101-
default: Fallback boolean if no options or env variables match.
102112
103113
Returns:
104-
bool: True if the signal is resolved to enabled, False otherwise.
114+
bool: True if the feature is resolved to enabled, False otherwise.
115+
116+
Raises:
117+
ValueError: If a provider is passed for an experimental feature without opening the gate.
105118
"""
106-
if feature_name != "tracing":
107-
raise ValueError(
108-
f"Invalid feature_name: {feature_name!r}. Only 'tracing' is supported at this time."
109-
)
110-
111-
# 1. Experimental Gate
112-
exp_var = "GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED"
113-
exp_val = _get_env_bool(exp_var)
114-
115-
has_provider = False
116-
if client_options is not None:
117-
options_dict = (
118-
client_options
119-
if isinstance(client_options, dict)
120-
else getattr(client_options, "__dict__", {})
121-
)
122-
if options_dict.get("tracer_provider") is not None:
123-
has_provider = True
124-
125-
if exp_val is not True:
126-
if has_provider:
119+
120+
121+
# Check for programmatic feature provider
122+
has_provider = _has_provider(client_options, provider_key)
123+
124+
# Read environment variable
125+
env_var_setting = _get_env_bool(env_var)
126+
127+
# EXPERIMENTAL PATH:
128+
# Resolution Hierarchy:
129+
# 1. EXPERIMENTAL Gate
130+
# 2. Fail Fast if Provider present but EXPERIMENTAL Gate is closed
131+
if "EXPERIMENTAL" in env_var:
132+
# Fail Fast if provider present but gate is closed
133+
if env_var_setting is not True and has_provider:
127134
raise ValueError(
128-
f"Experimental feature {feature_name!r} requires {exp_var} to be set to 'true' to use programmatic providers."
135+
f"Experimental feature requires {env_var} to be set to 'true' to use programmatic providers."
129136
)
130-
return False # Blocked
131137

132-
# If we are here, exp_val IS True.
133-
return True
138+
return bool(env_var_setting)
139+
140+
# GENERAL AVAILABILITY PATH:
141+
# Resolution Hierarchy:
142+
# 1. Programmatic Provider
143+
# 2. Environment Variable
144+
145+
# Check Programmatic Provider
146+
if has_provider:
147+
return True
148+
149+
# Check Environment Variable
150+
return bool(env_var_setting)

0 commit comments

Comments
 (0)