3
3
import os
4
4
5
5
import azure .identity .aio
6
+ import fastapi
7
+ from azure .monitor .opentelemetry import configure_azure_monitor
6
8
from dotenv import load_dotenv
7
9
from environs import Env
8
- from fastapi import FastAPI
10
+ from opentelemetry . instrumentation . sqlalchemy import SQLAlchemyInstrumentor
9
11
10
12
from .globals import global_storage
11
13
from .openai_clients import create_openai_chat_client
15
17
16
18
17
19
@contextlib .asynccontextmanager
18
- async def lifespan (app : FastAPI ):
20
+ async def lifespan (app : fastapi . FastAPI ):
19
21
load_dotenv (override = True )
20
22
21
23
azure_credential = None
@@ -40,6 +42,8 @@ async def lifespan(app: FastAPI):
40
42
global_storage .openai_chat_client = openai_chat_client
41
43
global_storage .openai_chat_model = openai_chat_model
42
44
45
+ if os .getenv ("APPLICATIONINSIGHTS_CONNECTION_STRING" ):
46
+ SQLAlchemyInstrumentor ().instrument (engine = engine .sync_engine )
43
47
yield
44
48
45
49
await engine .dispose ()
@@ -54,7 +58,14 @@ def create_app():
54
58
else :
55
59
logging .basicConfig (level = logging .WARNING )
56
60
57
- app = FastAPI (docs_url = "/docs" , lifespan = lifespan )
61
+ # Turn off particularly noisy INFO level logs from Azure Core SDK:
62
+ logging .getLogger ("azure.core.pipeline.policies.http_logging_policy" ).setLevel (logging .WARNING )
63
+
64
+ if os .getenv ("APPLICATIONINSIGHTS_CONNECTION_STRING" ):
65
+ logger .info ("Configuring Azure Monitor" )
66
+ configure_azure_monitor (logger_name = "ragapp" )
67
+
68
+ app = fastapi .FastAPI (docs_url = "/docs" , lifespan = lifespan )
58
69
59
70
from . import api_routes # noqa
60
71
from . import frontend_routes # noqa
0 commit comments