Srini P commited on
Commit
799e82f
·
1 Parent(s): ba8f1ce

Fix: Improve Azure Monitor initialization and robust connection string parsing

Browse files
Files changed (1) hide show
  1. app/backend/main.py +16 -7
app/backend/main.py CHANGED
@@ -13,6 +13,21 @@ from fastapi.middleware.cors import CORSMiddleware
13
  from pydantic import BaseModel
14
  from azure.monitor.opentelemetry import configure_azure_monitor
15
  from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  from pipeline.rag_pipeline import get_rag_pipeline
17
  from retrieval.user_auth import get_user_manager
18
  from vector_store import get_vector_store
@@ -26,13 +41,7 @@ logging.basicConfig(
26
  )
27
  logger = logging.getLogger(__name__)
28
 
29
- # Initialize Azure Monitor Tracing (Must be done before app creation)
30
- if os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"):
31
- logger.info("Initializing Azure Monitor Tracing...")
32
- try:
33
- configure_azure_monitor()
34
- except Exception as e:
35
- logger.error(f"Failed to initialize Azure Monitor Tracing: {str(e)}")
36
 
37
 
38
  # ====================
 
13
  from pydantic import BaseModel
14
  from azure.monitor.opentelemetry import configure_azure_monitor
15
  from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
16
+
17
+ # Initialize Azure Monitor Tracing FIRST (Before project imports)
18
+ connection_string = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
19
+ if connection_string:
20
+ # Remove potential quotes (HF secrets sometimes include them)
21
+ connection_string = connection_string.strip("'\"")
22
+ try:
23
+ configure_azure_monitor(connection_string=connection_string)
24
+ # We'll instrument the app later after it's created
25
+ except Exception as e:
26
+ # Don't use logger yet as logging isn't fully set up with basicConfig
27
+ print(f"ERROR: Failed to initialize Azure Monitor Tracing: {str(e)}")
28
+ import traceback
29
+ traceback.print_exc()
30
+
31
  from pipeline.rag_pipeline import get_rag_pipeline
32
  from retrieval.user_auth import get_user_manager
33
  from vector_store import get_vector_store
 
41
  )
42
  logger = logging.getLogger(__name__)
43
 
44
+ # Logging setup remains as is
 
 
 
 
 
 
45
 
46
 
47
  # ====================