1+ # -*- coding: utf-8 -*-
2+ # Copyright 2026 Google LLC
3+ #
4+ # Licensed under the Apache License, Version 2.0 (the "License");
5+ # you may not use this file except in compliance with the License.
6+ # You may obtain a copy of the License at
7+ #
8+ # http://www.apache.org/licenses/LICENSE-2.0
9+ #
10+ # Unless required by applicable law or agreed to in writing, software
11+ # distributed under the License is distributed on an "AS IS" BASIS,
12+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ # See the License for the specific language governing permissions and
14+ # limitations under the License.
15+ #
16+
117"""Observability environment variable and client options resolution helpers."""
218
319import os
723_TRUTHY_VALUES = ("y" , "yes" , "t" , "true" , "on" , "1" )
824_FALSY_VALUES = ("n" , "no" , "f" , "false" , "off" , "0" )
925
26+ # Test-only overrides for environment variables.
27+ # This is intended ONLY for unit/integration testing to prevent mutating
28+ # os.environ.
29+ _TEST_ENV_OVERRIDES : Dict [str , bool ] = {}
30+
1031
1132def _strtobool (val : str ) -> Optional [bool ]:
1233 """Convert a string representation of truth to a boolean."""
@@ -20,9 +41,6 @@ def _strtobool(val: str) -> Optional[bool]:
2041 raise ValueError (f"Invalid truth value: { val !r} " )
2142
2243
23- _TEST_ENV_OVERRIDES : Dict [str , bool ] = {}
24-
25-
2644def set_test_env_override (name : str , value : Optional [bool ]) -> None :
2745 """Sets a test-only override for a specific environment variable.
2846
@@ -55,7 +73,7 @@ def _get_env_bool(name: str) -> Optional[bool]:
5573
5674
5775def _get_env_bool_with_dev_fallback (name : str ) -> Optional [bool ]:
58- """Retrieve the boolean value of an environment variable, checking dev/exp fallbacks first."""
76+ """Retrieve the boolean value of an environment variable, checking experimental fallbacks first."""
5977 if name .startswith ("GOOGLE_CLOUD_" ):
6078 exp_name = name .replace ("GOOGLE_CLOUD_" , "GOOGLE_CLOUD_EXPERIMENTAL_" , 1 )
6179 val = _get_env_bool (exp_name )
@@ -74,20 +92,20 @@ def is_signal_enabled(
7492 Resolves settings in the following order of precedence:
7593 1. Programmatic overrides in client_options (checks tracer_provider)
7694 2. Language-wide Environment Variable: GOOGLE_CLOUD_PYTHON_TRACING_ENABLED
77- (natively checks for an EXPERIMENTAL prefix variant first)
95+ (natively checks for a variant with an " EXPERIMENTAL" token first)
7896 3. Default fallback
7997
8098 Args:
8199 signal_type: The signal type: must be 'tracing'.
82- client_options: A dictionary or object representing client configuration.
100+ client_options: A dictionary or object containing client configuration.
83101 default: Fallback boolean if no options or env variables match.
84102
85103 Returns:
86104 bool: True if the signal is resolved to enabled, False otherwise.
87105 """
88106 if signal_type != "tracing" :
89107 raise ValueError (
90- f"Invalid signal_type: { signal_type !r} . Only 'tracing' is supported."
108+ f"Invalid signal_type: { signal_type !r} . Only 'tracing' is supported at this time ."
91109 )
92110
93111 # 1. Resolve Programmatic Options First
0 commit comments