Skip to content

Commit 2839bb0

Browse files
Merge pull request #125 from microsoft/PSL-US-35959
fix: fixed app insights logging issue
2 parents 56e6e0f + b1b710a commit 2839bb0

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/api/app/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44

55
import uvicorn
6+
from azure.monitor.opentelemetry import configure_azure_monitor
67
from fastapi import FastAPI, HTTPException, Request
78
from fastapi.middleware.cors import CORSMiddleware
89
from fastapi.responses import JSONResponse
@@ -34,6 +35,16 @@
3435
# Get logger for this module (logging already configured above)
3536
logger = logging.getLogger(__name__)
3637

38+
# Check if the Application Insights Instrumentation Key is set in the environment variables
39+
instrumentation_key = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
40+
if instrumentation_key:
41+
# Configure Application Insights if the Instrumentation Key is found
42+
configure_azure_monitor(connection_string=instrumentation_key)
43+
logging.info("Application Insights configured with the provided Instrumentation Key")
44+
else:
45+
# Log a warning if the Instrumentation Key is not found
46+
logging.warning("No Application Insights Instrumentation Key found. Skipping configuration")
47+
3748
# Create FastAPI app
3849
app = FastAPI(
3950
title=settings.app_name,

0 commit comments

Comments
 (0)