Spaces:
Sleeping
Sleeping
Srini P commited on
Commit ·
11d48fe
1
Parent(s): 799e82f
Diag: Add /api/diag endpoint and improved Azure Monitor initialization logic
Browse files- app/backend/main.py +45 -12
app/backend/main.py
CHANGED
|
@@ -13,20 +13,27 @@ 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 |
|
| 17 |
# Initialize Azure Monitor Tracing FIRST (Before project imports)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
from pipeline.rag_pipeline import get_rag_pipeline
|
| 32 |
from retrieval.user_auth import get_user_manager
|
|
@@ -149,6 +156,32 @@ if os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"):
|
|
| 149 |
logger.error(f"Failed to instrument FastAPI app: {str(e)}")
|
| 150 |
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
# ====================
|
| 153 |
# CHAT ENDPOINT
|
| 154 |
# ====================
|
|
|
|
| 13 |
from pydantic import BaseModel
|
| 14 |
from azure.monitor.opentelemetry import configure_azure_monitor
|
| 15 |
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
|
| 16 |
+
import sys
|
| 17 |
|
| 18 |
# Initialize Azure Monitor Tracing FIRST (Before project imports)
|
| 19 |
+
def init_azure_monitor():
|
| 20 |
+
# Check both standard and common variants
|
| 21 |
+
connection_string = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING") or os.getenv("APPLICATION_INSIGHTS_CONNECTION_STRING")
|
| 22 |
+
|
| 23 |
+
if connection_string:
|
| 24 |
+
connection_string = connection_string.strip("'\"")
|
| 25 |
+
try:
|
| 26 |
+
configure_azure_monitor(connection_string=connection_string)
|
| 27 |
+
print("INFO: Azure Monitor tracing initialized successfully.")
|
| 28 |
+
return True, connection_string
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"ERROR: Failed to initialize Azure Monitor Tracing: {str(e)}")
|
| 31 |
+
import traceback
|
| 32 |
+
traceback.print_exc()
|
| 33 |
+
return False, str(e)
|
| 34 |
+
return False, "CONNECTION_STRING_NOT_FOUND"
|
| 35 |
+
|
| 36 |
+
AZURE_MONITOR_OK, AZURE_MONITOR_STATUS = init_azure_monitor()
|
| 37 |
|
| 38 |
from pipeline.rag_pipeline import get_rag_pipeline
|
| 39 |
from retrieval.user_auth import get_user_manager
|
|
|
|
| 156 |
logger.error(f"Failed to instrument FastAPI app: {str(e)}")
|
| 157 |
|
| 158 |
|
| 159 |
+
# ====================
|
| 160 |
+
# DIAGNOSTIC ENDPOINT
|
| 161 |
+
# ====================
|
| 162 |
+
|
| 163 |
+
@app.get("/api/diag")
|
| 164 |
+
async def diagnostic():
|
| 165 |
+
"""Diagnostic endpoint to check environment and tracing status."""
|
| 166 |
+
conn_str = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING") or os.getenv("APPLICATION_INSIGHTS_CONNECTION_STRING")
|
| 167 |
+
|
| 168 |
+
# Mask connection string
|
| 169 |
+
masked = None
|
| 170 |
+
if conn_str:
|
| 171 |
+
conn_str_clean = conn_str.strip("'\"")
|
| 172 |
+
masked = f"{conn_str_clean[:20]}...{conn_str_clean[-5:]}" if len(conn_str_clean) > 25 else "Too short"
|
| 173 |
+
|
| 174 |
+
return {
|
| 175 |
+
"azure_monitor_status": AZURE_MONITOR_STATUS,
|
| 176 |
+
"azure_monitor_ok": AZURE_MONITOR_OK,
|
| 177 |
+
"connection_string_found": conn_str is not None,
|
| 178 |
+
"connection_string_masked": masked,
|
| 179 |
+
"env_vars_keys": list(os.environ.keys()),
|
| 180 |
+
"python_version": sys.version,
|
| 181 |
+
"otel_libs_loaded": "opentelemetry" in sys.modules,
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
|
| 185 |
# ====================
|
| 186 |
# CHAT ENDPOINT
|
| 187 |
# ====================
|