Skip to content

Commit ed6f269

Browse files
author
ks6088ts
committed
install application insights to FastAPI server
1 parent e74b801 commit ed6f269

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

docs/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@
1515
- [FastAPI で作成した API エンドポイントをモデル コンテキスト プロトコル (MCP) ツールとして公開してみる](https://dev.classmethod.jp/articles/fastapi-api-mcp/)
1616
- [MCP Inspector > docs](https://modelcontextprotocol.io/docs/tools/inspector)
1717
- [MCP Inspector > codes](https://github.com/modelcontextprotocol/inspector)
18+
19+
## Application Insights
20+
21+
- [Application Insights の概要 - OpenTelemetry の可観測性](https://learn.microsoft.com/ja-jp/azure/azure-monitor/app/app-insights-overview)
22+
- [FastAPI のテレメトリデータを Azure Application Insights に送る](https://qiita.com/hoto17296/items/2f366dfabdbe3d1d4e97)

template_fastapi/app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
ref. https://github.com/tadata-org/fastapi_mcp/blob/v0.3.4/examples/shared/apps/items.py
44
"""
55

6+
from os import getenv
7+
8+
from azure.monitor.opentelemetry import configure_azure_monitor
69
from fastapi import FastAPI, HTTPException, Query
10+
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
711
from pydantic import BaseModel
812

913
app = FastAPI()
1014

15+
# If AZURE_APPLICATIONINSIGHTS_CONNECTION_STRING exists, configure Azure Monitor
16+
AZURE_CONNECTION_STRING = getenv("AZURE_APPLICATIONINSIGHTS_CONNECTION_STRING")
17+
if AZURE_CONNECTION_STRING:
18+
configure_azure_monitor(
19+
connection_string=AZURE_CONNECTION_STRING,
20+
)
21+
FastAPIInstrumentor.instrument_app(app)
22+
1123

1224
class Item(BaseModel):
1325
id: int

0 commit comments

Comments
 (0)