|
| 1 | +"""Observability environment variable and client options resolution helpers.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import warnings |
| 5 | +from typing import Any, Dict, List, Optional, Union |
| 6 | + |
| 7 | +# Allowed truthy and falsy patterns for environment variables |
| 8 | +_TRUTHY_VALUES = ("y", "yes", "t", "true", "on", "1") |
| 9 | +_FALSY_VALUES = ("n", "no", "f", "false", "off", "0") |
| 10 | + |
| 11 | + |
| 12 | +def _strtobool(val: str) -> Optional[bool]: |
| 13 | + """Convert a string representation of truth to a boolean.""" |
| 14 | + clean_val = val.lower().strip() |
| 15 | + if not clean_val: |
| 16 | + return None |
| 17 | + if clean_val in _TRUTHY_VALUES: |
| 18 | + return True |
| 19 | + if clean_val in _FALSY_VALUES: |
| 20 | + return False |
| 21 | + raise ValueError(f"Invalid truth value: {val!r}") |
| 22 | + |
| 23 | + |
| 24 | +def _get_env_bool(name: str) -> Optional[bool]: |
| 25 | + """Retrieve the boolean value of an environment variable.""" |
| 26 | + val = os.getenv(name) |
| 27 | + if val is None: |
| 28 | + return None |
| 29 | + try: |
| 30 | + return _strtobool(val) |
| 31 | + except ValueError: |
| 32 | + return None |
| 33 | + |
| 34 | + |
| 35 | +def _get_env_bool_with_dev_fallback(name: str) -> Optional[bool]: |
| 36 | + """Retrieve the boolean value of an environment variable, checking dev/exp fallbacks first.""" |
| 37 | + if name.startswith("GOOGLE_CLOUD_"): |
| 38 | + exp_name = name.replace("GOOGLE_CLOUD_", "GOOGLE_CLOUD_EXPERIMENTAL_", 1) |
| 39 | + val = _get_env_bool(exp_name) |
| 40 | + if val is not None: |
| 41 | + return val |
| 42 | + return _get_env_bool(name) |
| 43 | + |
| 44 | + |
| 45 | +def is_signal_enabled( |
| 46 | + service_name: str, |
| 47 | + signal_type: str, |
| 48 | + client_options: Optional[Union[Dict[str, Any], Any]] = None, |
| 49 | + default: bool = False, |
| 50 | + legacy_vars: Optional[List[str]] = None, |
| 51 | +) -> bool: |
| 52 | + """Determines if a telemetry signal is enabled.""" |
| 53 | + service_upper = service_name.upper().replace("-", "_") |
| 54 | + signal_upper = signal_type.upper() |
| 55 | + |
| 56 | + # 1. Resolve Programmatic Options First |
| 57 | + if client_options is not None: |
| 58 | + options_dict = ( |
| 59 | + client_options |
| 60 | + if isinstance(client_options, dict) |
| 61 | + else getattr(client_options, "__dict__", {}) |
| 62 | + ) |
| 63 | + option_key = f"enable_{signal_type.lower()}" |
| 64 | + provider_key = f"{signal_type.rstrip('s').lower()}_provider" |
| 65 | + |
| 66 | + if options_dict.get(option_key) is not None: |
| 67 | + return bool(options_dict.get(option_key)) |
| 68 | + if options_dict.get(provider_key) is not None: |
| 69 | + return True |
| 70 | + |
| 71 | + # 2. Language & Service-specific |
| 72 | + val = _get_env_bool_with_dev_fallback( |
| 73 | + f"GOOGLE_CLOUD_PYTHON_{service_upper}_{signal_upper}_ENABLED" |
| 74 | + ) |
| 75 | + if val is not None: |
| 76 | + return val |
| 77 | + |
| 78 | + # 3. Language-wide Global |
| 79 | + val = _get_env_bool_with_dev_fallback(f"GOOGLE_CLOUD_PYTHON_{signal_upper}_ENABLED") |
| 80 | + if val is not None: |
| 81 | + return val |
| 82 | + |
| 83 | + # 4. Cross-language Service-specific |
| 84 | + val = _get_env_bool_with_dev_fallback( |
| 85 | + f"GOOGLE_CLOUD_{service_upper}_{signal_upper}_ENABLED" |
| 86 | + ) |
| 87 | + if val is not None: |
| 88 | + return val |
| 89 | + |
| 90 | + # 5. Cross-language Global |
| 91 | + val = _get_env_bool_with_dev_fallback(f"GOOGLE_CLOUD_{signal_upper}_ENABLED") |
| 92 | + if val is not None: |
| 93 | + return val |
| 94 | + |
| 95 | + # 6. Legacy Variables |
| 96 | + if legacy_vars: |
| 97 | + for legacy_var in legacy_vars: |
| 98 | + val = _get_env_bool(legacy_var) |
| 99 | + if val is not None: |
| 100 | + warnings.warn( |
| 101 | + f"Environment variable {legacy_var!r} is deprecated and will be removed " |
| 102 | + "in a future release. Please migrate to the standardized " |
| 103 | + f"GOOGLE_CLOUD_PYTHON_{service_upper}_{signal_upper}_ENABLED instead.", |
| 104 | + DeprecationWarning, |
| 105 | + stacklevel=2, |
| 106 | + ) |
| 107 | + return val |
| 108 | + |
| 109 | + # 7. Default Fallback |
| 110 | + return default |
0 commit comments