Mirrowel commited on
Commit
a140a0d
Β·
1 Parent(s): eb3864b

refactor(logging): πŸ”¨ remove debug print statements and add concise deployment logs

Browse files

Removes verbose DEBUG-REMOVE diagnostic print statements that were used for troubleshooting .env loading and credential discovery during development.

- Removes ~25 debug print statements from main.py and credential_manager.py
- Adds concise, production-friendly logging for deployment verification:
- .env file loading summary with file names
- Credential loading summary with provider:count format
- Preserves essential startup information for operational visibility
- Improves code readability by removing debugging clutter
- Maintains helpful deployment context without verbose diagnostic output

src/proxy_app/main.py CHANGED
@@ -42,31 +42,23 @@ _start_time = time.time()
42
  from dotenv import load_dotenv
43
  from glob import glob
44
 
45
- # [DEBUG-REMOVE] Diagnostic logging for .env loading
46
- print(f"[DEBUG-REMOVE] Current working directory: {Path.cwd()}")
47
- print(f"[DEBUG-REMOVE] __file__ location: {Path(__file__).resolve().parent}")
48
-
49
  # Load main .env first
50
- _main_env_path = Path.cwd() / ".env"
51
- print(f"[DEBUG-REMOVE] Looking for main .env at: {_main_env_path}")
52
- print(f"[DEBUG-REMOVE] Main .env exists: {_main_env_path.exists()}")
53
  load_dotenv()
54
 
55
  # Load any additional .env files (e.g., antigravity_all_combined.env, gemini_cli_all_combined.env)
56
  _root_dir = Path.cwd()
57
  _env_files_found = list(_root_dir.glob("*.env"))
58
- print(f"[DEBUG-REMOVE] Found {len(_env_files_found)} .env files in {_root_dir}:")
59
- for _ef in _env_files_found:
60
- print(f"[DEBUG-REMOVE] - {_ef.name}")
61
-
62
  for _env_file in sorted(_root_dir.glob("*.env")):
63
  if _env_file.name != ".env": # Skip main .env (already loaded)
64
- print(f"[DEBUG-REMOVE] Loading additional .env file: {_env_file}")
65
  load_dotenv(_env_file, override=False) # Don't override existing values
66
 
 
 
 
 
 
67
  # Get proxy API key for display
68
  proxy_api_key = os.getenv("PROXY_API_KEY")
69
- print(f"[DEBUG-REMOVE] PROXY_API_KEY from environment: {'SET' if proxy_api_key else 'NOT SET'}")
70
  if proxy_api_key:
71
  key_display = f"βœ“ {proxy_api_key}"
72
  else:
@@ -302,16 +294,12 @@ PROXY_API_KEY = os.getenv("PROXY_API_KEY")
302
 
303
  # Discover API keys from environment variables
304
  api_keys = {}
305
- print("[DEBUG-REMOVE] === Discovering API keys from environment ===")
306
  for key, value in os.environ.items():
307
  if "_API_KEY" in key and key != "PROXY_API_KEY":
308
  provider = key.split("_API_KEY")[0].lower()
309
  if provider not in api_keys:
310
  api_keys[provider] = []
311
  api_keys[provider].append(value)
312
- print(f"[DEBUG-REMOVE] Found API key: {key} for provider '{provider}'")
313
-
314
- print(f"[DEBUG-REMOVE] Total providers with API keys: {list(api_keys.keys())}")
315
 
316
  # Load model ignore lists from environment variables
317
  ignore_models = {}
@@ -355,15 +343,8 @@ async def lifespan(app: FastAPI):
355
 
356
  # The CredentialManager now handles all discovery, including .env overrides.
357
  # We pass all environment variables to it for this purpose.
358
- print("[DEBUG-REMOVE] === Creating CredentialManager ===")
359
- print(f"[DEBUG-REMOVE] Total environment variables: {len(os.environ)}")
360
  cred_manager = CredentialManager(os.environ)
361
  oauth_credentials = cred_manager.discover_and_prepare()
362
-
363
- print(f"[DEBUG-REMOVE] === OAuth credentials discovered ===")
364
- print(f"[DEBUG-REMOVE] Providers with OAuth credentials: {list(oauth_credentials.keys())}")
365
- for provider, paths in oauth_credentials.items():
366
- print(f"[DEBUG-REMOVE] {provider}: {len(paths)} credential(s) - {paths}")
367
 
368
  if not skip_oauth_init and oauth_credentials:
369
  logging.info("Starting OAuth credential validation and deduplication...")
@@ -507,9 +488,6 @@ async def lifespan(app: FastAPI):
507
  }
508
 
509
  # The client now uses the root logger configuration
510
- print(f"[DEBUG-REMOVE] === Initializing RotatingClient ===")
511
- print(f"[DEBUG-REMOVE] API keys providers: {list(api_keys.keys())}")
512
- print(f"[DEBUG-REMOVE] OAuth providers: {list(oauth_credentials.keys())}")
513
  client = RotatingClient(
514
  api_keys=api_keys,
515
  oauth_credentials=oauth_credentials, # Pass OAuth config
@@ -520,9 +498,12 @@ async def lifespan(app: FastAPI):
520
  enable_request_logging=ENABLE_REQUEST_LOGGING,
521
  max_concurrent_requests_per_key=max_concurrent_requests_per_key
522
  )
523
- print(f"[DEBUG-REMOVE] RotatingClient.all_credentials keys: {list(client.all_credentials.keys())}")
524
- for provider, creds in client.all_credentials.items():
525
- print(f"[DEBUG-REMOVE] {provider}: {len(creds)} credential(s)")
 
 
 
526
  client.background_refresher.start() # Start the background task
527
  app.state.rotating_client = client
528
 
 
42
  from dotenv import load_dotenv
43
  from glob import glob
44
 
 
 
 
 
45
  # Load main .env first
 
 
 
46
  load_dotenv()
47
 
48
  # Load any additional .env files (e.g., antigravity_all_combined.env, gemini_cli_all_combined.env)
49
  _root_dir = Path.cwd()
50
  _env_files_found = list(_root_dir.glob("*.env"))
 
 
 
 
51
  for _env_file in sorted(_root_dir.glob("*.env")):
52
  if _env_file.name != ".env": # Skip main .env (already loaded)
 
53
  load_dotenv(_env_file, override=False) # Don't override existing values
54
 
55
+ # Log discovered .env files for deployment verification
56
+ if _env_files_found:
57
+ _env_names = [_ef.name for _ef in _env_files_found]
58
+ print(f"πŸ“ Loaded {len(_env_files_found)} .env file(s): {', '.join(_env_names)}")
59
+
60
  # Get proxy API key for display
61
  proxy_api_key = os.getenv("PROXY_API_KEY")
 
62
  if proxy_api_key:
63
  key_display = f"βœ“ {proxy_api_key}"
64
  else:
 
294
 
295
  # Discover API keys from environment variables
296
  api_keys = {}
 
297
  for key, value in os.environ.items():
298
  if "_API_KEY" in key and key != "PROXY_API_KEY":
299
  provider = key.split("_API_KEY")[0].lower()
300
  if provider not in api_keys:
301
  api_keys[provider] = []
302
  api_keys[provider].append(value)
 
 
 
303
 
304
  # Load model ignore lists from environment variables
305
  ignore_models = {}
 
343
 
344
  # The CredentialManager now handles all discovery, including .env overrides.
345
  # We pass all environment variables to it for this purpose.
 
 
346
  cred_manager = CredentialManager(os.environ)
347
  oauth_credentials = cred_manager.discover_and_prepare()
 
 
 
 
 
348
 
349
  if not skip_oauth_init and oauth_credentials:
350
  logging.info("Starting OAuth credential validation and deduplication...")
 
488
  }
489
 
490
  # The client now uses the root logger configuration
 
 
 
491
  client = RotatingClient(
492
  api_keys=api_keys,
493
  oauth_credentials=oauth_credentials, # Pass OAuth config
 
498
  enable_request_logging=ENABLE_REQUEST_LOGGING,
499
  max_concurrent_requests_per_key=max_concurrent_requests_per_key
500
  )
501
+
502
+ # Log loaded credentials summary (compact, always visible for deployment verification)
503
+ _api_summary = ', '.join([f"{p}:{len(c)}" for p, c in api_keys.items()]) if api_keys else "none"
504
+ _oauth_summary = ', '.join([f"{p}:{len(c)}" for p, c in oauth_credentials.items()]) if oauth_credentials else "none"
505
+ _total_summary = ', '.join([f"{p}:{len(c)}" for p, c in client.all_credentials.items()])
506
+ print(f"πŸ”‘ Credentials loaded: {_total_summary} (API: {_api_summary} | OAuth: {_oauth_summary})")
507
  client.background_refresher.start() # Start the background task
508
  app.state.rotating_client = client
509
 
src/rotator_library/credential_manager.py CHANGED
@@ -58,25 +58,13 @@ class CredentialManager:
58
  """
59
  env_credentials: Dict[str, Set[str]] = {}
60
 
61
- # [DEBUG-REMOVE] Log all environment variable keys for OAuth providers
62
- print(f"[DEBUG-REMOVE] === Scanning environment for OAuth credentials ===")
63
- print(f"[DEBUG-REMOVE] ENV_OAUTH_PROVIDERS: {list(ENV_OAUTH_PROVIDERS.keys())}")
64
-
65
  for provider, env_prefix in ENV_OAUTH_PROVIDERS.items():
66
  found_indices: Set[str] = set()
67
- print(f"[DEBUG-REMOVE] Scanning for provider '{provider}' with prefix '{env_prefix}'")
68
 
69
  # Check for numbered credentials (PROVIDER_N_ACCESS_TOKEN pattern)
70
  # Pattern: ANTIGRAVITY_1_ACCESS_TOKEN, ANTIGRAVITY_2_ACCESS_TOKEN, etc.
71
  numbered_pattern = re.compile(rf"^{env_prefix}_(\d+)_ACCESS_TOKEN$")
72
 
73
- # [DEBUG-REMOVE] Show all matching environment variable keys
74
- matching_keys = [k for k in self.env_vars.keys() if env_prefix in k]
75
- if matching_keys:
76
- print(f"[DEBUG-REMOVE] Found {len(matching_keys)} keys with '{env_prefix}': {matching_keys}")
77
- else:
78
- print(f"[DEBUG-REMOVE] No keys found with '{env_prefix}' prefix")
79
-
80
  for key in self.env_vars.keys():
81
  match = numbered_pattern.match(key)
82
  if match:
@@ -85,30 +73,20 @@ class CredentialManager:
85
  refresh_key = f"{env_prefix}_{index}_REFRESH_TOKEN"
86
  if refresh_key in self.env_vars and self.env_vars[refresh_key]:
87
  found_indices.add(index)
88
- print(f"[DEBUG-REMOVE] βœ“ Found numbered credential {index} for {provider}")
89
- else:
90
- print(f"[DEBUG-REMOVE] βœ— Missing REFRESH_TOKEN for {provider} credential {index}")
91
 
92
  # Check for legacy single credential (PROVIDER_ACCESS_TOKEN pattern)
93
  # Only use this if no numbered credentials exist
94
  if not found_indices:
95
  access_key = f"{env_prefix}_ACCESS_TOKEN"
96
  refresh_key = f"{env_prefix}_REFRESH_TOKEN"
97
- print(f"[DEBUG-REMOVE] Checking legacy format: {access_key}, {refresh_key}")
98
  if (access_key in self.env_vars and self.env_vars[access_key] and
99
  refresh_key in self.env_vars and self.env_vars[refresh_key]):
100
  # Use "0" as the index for legacy single credential
101
  found_indices.add("0")
102
- print(f"[DEBUG-REMOVE] βœ“ Found legacy single credential for {provider}")
103
- else:
104
- print(f"[DEBUG-REMOVE] βœ— No legacy credential found for {provider}")
105
 
106
  if found_indices:
107
  env_credentials[provider] = found_indices
108
  lib_logger.info(f"Found {len(found_indices)} env-based credential(s) for {provider}")
109
- print(f"[DEBUG-REMOVE] RESULT: {len(found_indices)} credential(s) registered for {provider}")
110
- else:
111
- print(f"[DEBUG-REMOVE] RESULT: No credentials found for {provider}")
112
 
113
  # Convert to virtual paths
114
  result: Dict[str, List[str]] = {}
 
58
  """
59
  env_credentials: Dict[str, Set[str]] = {}
60
 
 
 
 
 
61
  for provider, env_prefix in ENV_OAUTH_PROVIDERS.items():
62
  found_indices: Set[str] = set()
 
63
 
64
  # Check for numbered credentials (PROVIDER_N_ACCESS_TOKEN pattern)
65
  # Pattern: ANTIGRAVITY_1_ACCESS_TOKEN, ANTIGRAVITY_2_ACCESS_TOKEN, etc.
66
  numbered_pattern = re.compile(rf"^{env_prefix}_(\d+)_ACCESS_TOKEN$")
67
 
 
 
 
 
 
 
 
68
  for key in self.env_vars.keys():
69
  match = numbered_pattern.match(key)
70
  if match:
 
73
  refresh_key = f"{env_prefix}_{index}_REFRESH_TOKEN"
74
  if refresh_key in self.env_vars and self.env_vars[refresh_key]:
75
  found_indices.add(index)
 
 
 
76
 
77
  # Check for legacy single credential (PROVIDER_ACCESS_TOKEN pattern)
78
  # Only use this if no numbered credentials exist
79
  if not found_indices:
80
  access_key = f"{env_prefix}_ACCESS_TOKEN"
81
  refresh_key = f"{env_prefix}_REFRESH_TOKEN"
 
82
  if (access_key in self.env_vars and self.env_vars[access_key] and
83
  refresh_key in self.env_vars and self.env_vars[refresh_key]):
84
  # Use "0" as the index for legacy single credential
85
  found_indices.add("0")
 
 
 
86
 
87
  if found_indices:
88
  env_credentials[provider] = found_indices
89
  lib_logger.info(f"Found {len(found_indices)} env-based credential(s) for {provider}")
 
 
 
90
 
91
  # Convert to virtual paths
92
  result: Dict[str, List[str]] = {}