umer6016 commited on
Commit
49462f7
·
1 Parent(s): 1785876

Fix Supabase logging: require ENABLE_METRICS_LOGGING env var

Browse files
backend/app/services/metrics_logger.py CHANGED
@@ -45,6 +45,12 @@ def get_supabase_client() -> Optional[Client]:
45
  print("⚠️ Metrics Supabase not configured: missing SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY/ANON.")
46
  _warned_no_supabase = True
47
  return None
 
 
 
 
 
 
48
  try:
49
  _supabase_client = create_client(SUPABASE_URL, SUPABASE_KEY)
50
  except Exception as exc:
 
45
  print("⚠️ Metrics Supabase not configured: missing SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY/ANON.")
46
  _warned_no_supabase = True
47
  return None
48
+
49
+ if not _metrics_enabled():
50
+ if not _warned_no_supabase:
51
+ print("ℹ️ Metrics logging is disabled (ENABLE_METRICS_LOGGING is not 'true').")
52
+ _warned_no_supabase = True
53
+ return None
54
  try:
55
  _supabase_client = create_client(SUPABASE_URL, SUPABASE_KEY)
56
  except Exception as exc:
deployment_guide.md CHANGED
@@ -24,6 +24,7 @@ Your app needs OpenAI and Supabase keys.
24
  - `OPENAI_API_KEY`
25
  - `SUPABASE_URL`
26
  - `SUPABASE_ANON_KEY`
 
27
 
28
  ### 3. Upload Code
29
  You can upload files via the web interface or use Git. Since you have the code locally:
 
24
  - `OPENAI_API_KEY`
25
  - `SUPABASE_URL`
26
  - `SUPABASE_ANON_KEY`
27
+ - `ENABLE_METRICS_LOGGING` (Set to `true` to enable Supabase logging)
28
 
29
  ### 3. Upload Code
30
  You can upload files via the web interface or use Git. Since you have the code locally:
scripts/test_supabase_connection.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ from datetime import datetime, timezone
4
+ from supabase import create_client, Client
5
+
6
+ def main():
7
+ print("--- Supabase Connection Test ---")
8
+
9
+ # 1. Check Env Vars
10
+ url = os.getenv("SUPABASE_URL")
11
+ key = os.getenv("SUPABASE_ANON_KEY") or os.getenv("SUPABASE_SERVICE_ROLE_KEY")
12
+ enable_metrics = os.getenv("ENABLE_METRICS_LOGGING", "false").lower() == "true"
13
+
14
+ print(f"ENABLE_METRICS_LOGGING: {enable_metrics}")
15
+ print(f"SUPABASE_URL: {'Set' if url else 'Missing'}")
16
+ print(f"SUPABASE_KEY: {'Set' if key else 'Missing'}")
17
+
18
+ if not url or not key:
19
+ print("❌ Missing Supabase credentials.")
20
+ sys.exit(1)
21
+
22
+ if not enable_metrics:
23
+ print("⚠️ ENABLE_METRICS_LOGGING is not true. The backend would skip logging.")
24
+ print(" Proceeding with test anyway to verify connection...")
25
+
26
+ # 2. Connect
27
+ try:
28
+ supabase: Client = create_client(url, key)
29
+ print("✅ Supabase client initialized.")
30
+ except Exception as e:
31
+ print(f"❌ Failed to initialize client: {e}")
32
+ sys.exit(1)
33
+
34
+ # 3. Test Insert
35
+ payload = {
36
+ "question": "TEST_CONNECTION_PROBE",
37
+ "answer": "This is a test row from scripts/test_supabase_connection.py",
38
+ "provenance": "test_script",
39
+ "user_id": "test_user"
40
+ }
41
+
42
+ print(f"Attempting to insert into 'metrics_chat_answers': {payload}")
43
+
44
+ try:
45
+ response = supabase.table("metrics_chat_answers").insert(payload).execute()
46
+ print("✅ Insert successful!")
47
+ print("Response data:", response.data)
48
+ except Exception as e:
49
+ print(f"❌ Insert failed: {e}")
50
+ print("Possible causes:")
51
+ print(" - Table 'metrics_chat_answers' does not exist.")
52
+ print(" - RLS (Row Level Security) blocks insertion for anon key.")
53
+ print(" - Triggers or constraints failed.")
54
+
55
+ if __name__ == "__main__":
56
+ main()