Skip to content

Commit 3791001

Browse files
authored
Merge pull request #255 from lzchen/instr
2 parents 9a3a8b6 + aa01eee commit 3791001

File tree

7 files changed

+62
-4
lines changed

7 files changed

+62
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
([#253](https://github.com/microsoft/ApplicationInsights-Python/pull/253))
77
- Use entrypoints instead of importlib to load instrumentations
88
([#254](https://github.com/microsoft/ApplicationInsights-Python/pull/254))
9+
- Add support for FastAPI instrumentation
10+
([#255](https://github.com/microsoft/ApplicationInsights-Python/pull/255))
911

1012
## [1.0.0b10](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b10) - 2023-02-23
1113

azure-monitor-opentelemetry/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The following OpenTelemetry instrumentations come bundled in with the Azure moni
1212

1313
* [OpenTelemetry Requests Instrumentation][opentelemetry_instrumentation_requests]
1414
* [OpenTelemetry Django Instrumentation][opentelemetry_instrumentation_django]
15+
* [OpenTelemetry FastApi Instrumentation][opentelemetry_instrumentation_fastapi]
1516
* [OpenTelemetry Flask Instrumentation][opentelemetry_instrumentation_flask]
1617
* [OpenTelemetry Psycopg2 Instrumentation][opentelemetry_instrumentation_psycopg2]
1718

@@ -118,6 +119,7 @@ Samples are available [here][samples] to demonstrate how to utilize the above co
118119
[ot_sdk_python_view_examples]: https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples/metrics/views
119120
[opentelemetry_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests
120121
[opentelemetry_instrumentation_django]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-django
122+
[opentelemetry_instrumentation_fastapi]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-fastapi
121123
[opentelemetry_instrumentation_flask]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask
122124
[opentelemetry_instrumentation_psycopg2]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-psycopg2
123125
[opentelemetry_spec_resource]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#resource-sdk

azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
_INSTRUMENTATION_CONFIG_SUFFIX = "_config"
3737
_SUPPORTED_INSTRUMENTED_LIBRARIES = (
3838
"django",
39+
"fastapi",
3940
"flask",
4041
"psycopg2",
4142
"requests",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
import fastapi
7+
from azure.monitor.opentelemetry import configure_azure_monitor
8+
9+
# Configure Azure monitor collection telemetry pipeline
10+
configure_azure_monitor(
11+
connection_string="<your-connection-string>",
12+
disable_logging=True,
13+
disable_metrics=True,
14+
fastapi_config={"excluded_urls": "http://127.0.0.1:8000/exclude"},
15+
tracing_export_interval_millis=15000,
16+
)
17+
18+
app = fastapi.FastAPI()
19+
20+
21+
# Requests made to fastapi endpoints will be automatically captured
22+
@app.get("/")
23+
async def root():
24+
return {"message": "Hello World"}
25+
26+
27+
# Telemetry from this endpoint will not be captured due to excluded_urls config above
28+
@app.get("/exclude")
29+
async def root():
30+
return {"message": "Telemetry was not captured"}

azure-monitor-opentelemetry/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@
8787
"azure-monitor-opentelemetry-exporter>=1.0.0b12",
8888
"opentelemetry-instrumentation~=0.36b0",
8989
"opentelemetry-instrumentation-django~=0.36b0",
90-
"opentelemetry-instrumentation-requests~=0.36b0",
90+
"opentelemetry-instrumentation-fastapi~=0.36b0",
9191
"opentelemetry-instrumentation-flask~=0.36b0",
9292
"opentelemetry-instrumentation-psycopg2~=0.36b0",
93+
"opentelemetry-instrumentation-requests~=0.36b0",
9394
"opentelemetry-api==1.15.0",
9495
"opentelemetry-sdk==1.15.0",
9596
],
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
import unittest
8+
9+
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
10+
11+
12+
class TestFastApiInstrumentation(unittest.TestCase):
13+
def test_instrument(self):
14+
excluded_urls = "client/.*/info,healthcheck"
15+
try:
16+
FastAPIInstrumentor().instrument(excluded_urls=excluded_urls)
17+
except Exception as ex: # pylint: disable=broad-except
18+
print(ex)
19+
self.fail(
20+
f"Unexpected exception raised when instrumenting {FastAPIInstrumentor.__name__}"
21+
)

test-requirements.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pytest
2-
psycopg2
3-
flask
42
django
5-
requests
3+
fastapi
4+
flask
5+
psycopg2
6+
requests

0 commit comments

Comments
 (0)