Skip to content

Commit 37bcbd3

Browse files
committed
Address feedback from Copilot
1 parent 166d19b commit 37bcbd3

File tree

8 files changed

+40
-10
lines changed

8 files changed

+40
-10
lines changed

infra/aca.bicep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ module app 'core/host/container-app-upsert.bicep' = {
5959
name: 'AZURE_COSMOSDB_CONTAINER'
6060
value: cosmosDbContainer
6161
}
62+
// We typically store sensitive values in secrets, but App Insights connection strings are not considered highly sensitive
6263
{
6364
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
6465
value: applicationInsightsConnectionString

infra/main.bicep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,4 +754,5 @@ output AZURE_COSMOSDB_ENDPOINT string = cosmosDb.outputs.endpoint
754754
output AZURE_COSMOSDB_DATABASE string = cosmosDbDatabaseName
755755
output AZURE_COSMOSDB_CONTAINER string = cosmosDbContainerName
756756

757+
// We typically do not output sensitive values, but App Insights connection strings are not considered highly sensitive
757758
output APPLICATIONINSIGHTS_CONNECTION_STRING string = useMonitoring ? applicationInsights.outputs.connectionString : ''

infra/write_env.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ Add-Content -Path $ENV_FILE_PATH -Value "AZURE_TENANT_ID=$(azd env get-value AZU
1111
Add-Content -Path $ENV_FILE_PATH -Value "AZURE_COSMOSDB_ACCOUNT=$(azd env get-value AZURE_COSMOSDB_ACCOUNT)"
1212
Add-Content -Path $ENV_FILE_PATH -Value "AZURE_COSMOSDB_DATABASE=$(azd env get-value AZURE_COSMOSDB_DATABASE)"
1313
Add-Content -Path $ENV_FILE_PATH -Value "AZURE_COSMOSDB_CONTAINER=$(azd env get-value AZURE_COSMOSDB_CONTAINER)"
14+
Add-Content -Path $ENV_FILE_PATH -Value "APPLICATIONINSIGHTS_CONNECTION_STRING=$(azd env get-value APPLICATIONINSIGHTS_CONNECTION_STRING)"
1415
Add-Content -Path $ENV_FILE_PATH -Value "API_HOST=azure"

infra/write_env.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ echo "AZURE_TENANT_ID=$(azd env get-value AZURE_TENANT_ID)" >> "$ENV_FILE_PATH"
1515
echo "AZURE_COSMOSDB_ACCOUNT=$(azd env get-value AZURE_COSMOSDB_ACCOUNT)" >> "$ENV_FILE_PATH"
1616
echo "AZURE_COSMOSDB_DATABASE=$(azd env get-value AZURE_COSMOSDB_DATABASE)" >> "$ENV_FILE_PATH"
1717
echo "AZURE_COSMOSDB_CONTAINER=$(azd env get-value AZURE_COSMOSDB_CONTAINER)" >> "$ENV_FILE_PATH"
18-
echo "API_HOST=azure" >> "$ENV_FILE_PATH"
18+
echo "APPLICATIONINSIGHTS_CONNECTION_STRING=$(azd env get-value APPLICATIONINSIGHTS_CONNECTION_STRING)" >> "$ENV_FILE_PATH"
19+
echo "API_HOST=azure" >> "$ENV_FILE_PATH"

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ dependencies = [
1717
"agent-framework>=1.0.0b251016",
1818
"azure-cosmos>=4.9.0",
1919
"azure-monitor-opentelemetry>=1.6.4",
20-
"logfire>=3.11.0",
20+
"opentelemetry-instrumentation-starlette>=0.49b0",
21+
"logfire>=4.15.1",
2122
"azure-core-tracing-opentelemetry>=1.0.0b12"
2223
]
2324

servers/deployed_mcp.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Run with: cd servers && uvicorn deployed_mcp:app --host 0.0.0.0 --port 8000"""
2+
13
import logging
24
import os
35
import uuid
@@ -12,7 +14,12 @@
1214
from azure.monitor.opentelemetry import configure_azure_monitor
1315
from dotenv import load_dotenv
1416
from fastmcp import FastMCP
15-
from opentelemetry_middleware import OpenTelemetryMiddleware
17+
from opentelemetry.instrumentation.starlette import StarletteInstrumentor
18+
19+
try:
20+
from opentelemetry_middleware import OpenTelemetryMiddleware
21+
except ImportError:
22+
from servers.opentelemetry_middleware import OpenTelemetryMiddleware
1623

1724
RUNNING_IN_PRODUCTION = os.getenv("RUNNING_IN_PRODUCTION", "false").lower() == "true"
1825

@@ -23,13 +30,14 @@
2330
logger = logging.getLogger("ExpensesMCP")
2431
logger.setLevel(logging.INFO)
2532

26-
# Configure OpenTelemetry tracing
33+
# Configure OpenTelemetry tracing, either via Azure Monitor or Logfire
34+
# We don't support both at the same time due to potential conflicts with tracer providers
2735
if os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"):
2836
logger.info("Setting up Azure Monitor instrumentation")
2937
configure_azure_monitor()
3038
elif os.getenv("LOGFIRE_PROJECT_NAME"):
3139
logger.info("Setting up Logfire instrumentation")
32-
settings.tracing_implementation = "opentelemetry" # Send Azure Monitor traces via OpenTelemetry
40+
settings.tracing_implementation = "opentelemetry" # Configure Azure SDK to use OpenTelemetry tracing
3341
logfire.configure(service_name="expenses-mcp", send_to_logfire=True)
3442

3543
# Cosmos DB configuration from environment variables
@@ -169,7 +177,4 @@ def analyze_spending_prompt(
169177

170178
# ASGI application for uvicorn
171179
app = mcp.http_app()
172-
173-
if __name__ == "__main__":
174-
logger.info("MCP Expenses server starting (HTTP mode on port 8000)")
175-
mcp.run(transport="http", host="0.0.0.0", port=8000)
180+
StarletteInstrumentor.instrument_app(app)

servers/opentelemetry_middleware.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ async def on_call_tool(self, context: MiddlewareContext, call_next):
1919
"mcp.method": context.method,
2020
"mcp.source": context.source,
2121
"mcp.tool.name": tool_name,
22+
# If arguments are sensitive, consider omitting or sanitizing them
23+
# If arguments are long/nested, consider adding a size or depth limit
2224
"mcp.tool.arguments": str(context.message.arguments),
2325
},
2426
) as span:

uv.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)