forked from microsoft/opentelemetry-distro-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastapi_app.py
More file actions
45 lines (32 loc) · 1.27 KB
/
Copy pathfastapi_app.py
File metadata and controls
45 lines (32 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import os
import uvicorn
from logging import getLogger, INFO
from microsoft.opentelemetry import use_microsoft_opentelemetry
# Connection string can also be passed directly:
# azure_monitor_connection_string="InstrumentationKey=..."
use_microsoft_opentelemetry(
enable_azure_monitor=True,
azure_monitor_connection_string=os.environ.get("APPLICATIONINSIGHTS_CONNECTION_STRING", ""),
logger_name=__name__,
)
logger = getLogger(__name__)
logger.setLevel(INFO)
from fastapi import FastAPI # pylint: disable=wrong-import-position
app = FastAPI()
@app.get("/")
async def root():
logger.info("Root endpoint called")
return {"message": "Hello World"}
# Exceptions that are raised within the request are automatically captured
@app.get("/exception")
async def exception():
raise Exception("Hit an exception") # pylint: disable=broad-exception-raised
# Set the OTEL_PYTHON_EXCLUDED_URLS environment variable to "http://127.0.0.1:8000/exclude"
# Telemetry from this endpoint will not be captured due to excluded_urls config above
@app.get("/exclude")
async def exclude():
return {"message": "Telemetry was not captured"}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)