Skip to content

Commit 0fb9a74

Browse files
authored
Merge pull request #256 from lzchen/urllib
2 parents 3791001 + 703f869 commit 0fb9a74

File tree

13 files changed

+190
-5
lines changed

13 files changed

+190
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
([#254](https://github.com/microsoft/ApplicationInsights-Python/pull/254))
99
- Add support for FastAPI instrumentation
1010
([#255](https://github.com/microsoft/ApplicationInsights-Python/pull/255))
11+
- Add support for Urllib3/Urllib instrumentation
12+
([#256](https://github.com/microsoft/ApplicationInsights-Python/pull/256))
1113

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

azure-monitor-opentelemetry/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ This distro automatically installs the following libraries:
88

99
## Officially supported instrumentations
1010

11-
The following OpenTelemetry instrumentations come bundled in with the Azure monitor distro. If you would like to add support for another OpenTelemetry instrumentation, please submit a feature [request][distro_feature_request]. In the meantime, you can use the OpenTelemetry instrumentation manually via it's own APIs (i.e. `instrument()`) in your code.
11+
The following OpenTelemetry instrumentations come bundled in with the Azure monitor distro. If you would like to add support for another OpenTelemetry instrumentation, please submit a feature [request][distro_feature_request]. In the meantime, you can use the OpenTelemetry instrumentation manually via it's own APIs (i.e. `instrument()`) in your code. See [this][samples_manual] for an example.
1212

13-
* [OpenTelemetry Requests Instrumentation][opentelemetry_instrumentation_requests]
1413
* [OpenTelemetry Django Instrumentation][opentelemetry_instrumentation_django]
1514
* [OpenTelemetry FastApi Instrumentation][opentelemetry_instrumentation_fastapi]
1615
* [OpenTelemetry Flask Instrumentation][opentelemetry_instrumentation_flask]
1716
* [OpenTelemetry Psycopg2 Instrumentation][opentelemetry_instrumentation_psycopg2]
17+
* [OpenTelemetry Requests Instrumentation][opentelemetry_instrumentation_requests]
18+
* [OpenTelemetry UrlLib Instrumentation][opentelemetry_instrumentation_urllib]
19+
* [OpenTelemetry UrlLib3 Instrumentation][opentelemetry_instrumentation_urllib3]
1820

1921
## Getting started
2022

@@ -117,13 +119,16 @@ Samples are available [here][samples] to demonstrate how to utilize the above co
117119
[ot_sdk_python_metric_reader]: https://opentelemetry-python.readthedocs.io/en/stable/sdk/metrics.export.html#opentelemetry.sdk.metrics.export.MetricReader
118120
[ot_sdk_python_resource]: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py#L153
119121
[ot_sdk_python_view_examples]: https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples/metrics/views
120-
[opentelemetry_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests
121122
[opentelemetry_instrumentation_django]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-django
122123
[opentelemetry_instrumentation_fastapi]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-fastapi
123124
[opentelemetry_instrumentation_flask]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask
124125
[opentelemetry_instrumentation_psycopg2]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-psycopg2
126+
[opentelemetry_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests
127+
[opentelemetry_instrumentation_urllib]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-urllib
128+
[opentelemetry_instrumentation_urllib3]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-urllib3
125129
[opentelemetry_spec_resource]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#resource-sdk
126130
[opentelemetry_spec_view]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view
127131
[python]: https://www.python.org/downloads/
128132
[pip]: https://pypi.org/project/pip/
129133
[samples]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry/samples
134+
[samples_manual]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry/samples/tracing/manual.py

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
"flask",
4141
"psycopg2",
4242
"requests",
43+
"urllib",
44+
"urllib3",
4345
)
4446

4547

azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
tracing_export_interval_millis=15000,
1515
)
1616

17+
# Database calls using the psycopg2 library will be automatically captured
1718
cnx = psycopg2.connect(database="test", user="<user>", password="<password>")
1819
cursor = cnx.cursor()
1920
cursor.execute("INSERT INTO test_tables (test_field) VALUES (123)")

azure-monitor-opentelemetry/samples/tracing/http_fastapi.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@
2020

2121
# Requests made to fastapi endpoints will be automatically captured
2222
@app.get("/")
23-
async def root():
23+
async def test():
2424
return {"message": "Hello World"}
2525

2626

27+
# Exceptions that are raised within the request are automatically captured
28+
@app.get("/exception")
29+
async def exception():
30+
raise Exception("Hit an exception")
31+
32+
2733
# Telemetry from this endpoint will not be captured due to excluded_urls config above
2834
@app.get("/exclude")
29-
async def root():
35+
async def exclude():
3036
return {"message": "Telemetry was not captured"}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 logging
7+
from urllib import request
8+
9+
from azure.monitor.opentelemetry import configure_azure_monitor
10+
from opentelemetry import trace
11+
12+
logger = logging.getLogger(__name__)
13+
14+
# Configure Azure monitor collection telemetry pipeline
15+
configure_azure_monitor(
16+
connection_string="<your-connection-string>",
17+
disable_logging=True,
18+
disable_metrics=True,
19+
tracing_export_interval_millis=15000,
20+
)
21+
22+
tracer = trace.get_tracer(__name__)
23+
with tracer.start_as_current_span("Request parent span") as span:
24+
try:
25+
# Requests made using the urllib library will be automatically captured
26+
req = request.Request("https://www.example.org/", method="GET")
27+
r = request.urlopen(req)
28+
logger.warning("Request sent")
29+
except Exception as ex:
30+
# If an exception occurs, this can be manually recorded on the parent span
31+
span.set_attribute("status", "exception")
32+
span.record_exception(ex)
33+
34+
input()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 logging
7+
8+
import urllib3
9+
from azure.monitor.opentelemetry import configure_azure_monitor
10+
from opentelemetry import trace
11+
12+
logger = logging.getLogger(__name__)
13+
14+
# Configure Azure monitor collection telemetry pipeline
15+
configure_azure_monitor(
16+
connection_string="<your-connection-string>",
17+
disable_logging=True,
18+
disable_metrics=True,
19+
tracing_export_interval_millis=15000,
20+
)
21+
22+
http = urllib3.PoolManager()
23+
24+
tracer = trace.get_tracer(__name__)
25+
with tracer.start_as_current_span("Request parent span") as span:
26+
try:
27+
# Requests made using the urllib3 library will be automatically captured
28+
response = http.request("GET", "https://www.example.org/")
29+
logger.warning("Request sent")
30+
except Exception as ex:
31+
# If an exception occurs, this can be manually recorded on the parent span
32+
span.set_attribute("status", "exception")
33+
span.record_exception(ex)
34+
35+
input()
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+
from azure.monitor.opentelemetry import configure_azure_monitor
7+
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
8+
from sqlalchemy import create_engine, text
9+
10+
configure_azure_monitor(
11+
connection_string="<your-connection-string>",
12+
tracing_export_interval_millis=15000,
13+
disable_logging=True,
14+
disable_metrics=True,
15+
)
16+
17+
engine = create_engine("sqlite:///:memory:")
18+
# SQLAlchemy instrumentation is not officially supported by this package
19+
# However, you can use the OpenTelemetry instument method manually in
20+
# conjunction with configure_azure_monitor
21+
SQLAlchemyInstrumentor().instrument(
22+
engine=engine,
23+
)
24+
25+
# Database calls using the SqlAlchemy library will be automatically captured
26+
with engine.connect() as conn:
27+
result = conn.execute(text("select 'hello world'"))
28+
print(result.all())
29+
30+
input()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
from azure.monitor.opentelemetry import configure_azure_monitor
8+
from opentelemetry import trace
9+
10+
configure_azure_monitor(
11+
connection_string="<your-connection-string>",
12+
# Sampling ratio of between 0 and 1 inclusive
13+
# 0.1 means approximately 10% of your traces are sent
14+
sampling_ratio=0.1,
15+
tracing_export_interval_millis=15000,
16+
disable_logging=True,
17+
disable_metrics=True,
18+
)
19+
20+
tracer = trace.get_tracer(__name__)
21+
22+
for i in range(100):
23+
# Approximately 90% of these spans should be sampled out
24+
with tracer.start_as_current_span("hello"):
25+
print("Hello, World!")
26+
27+
input()

azure-monitor-opentelemetry/setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
"opentelemetry-instrumentation-flask~=0.36b0",
9292
"opentelemetry-instrumentation-psycopg2~=0.36b0",
9393
"opentelemetry-instrumentation-requests~=0.36b0",
94+
"opentelemetry-instrumentation-urllib~=0.36b0",
95+
"opentelemetry-instrumentation-urllib3~=0.36b0",
9496
"opentelemetry-api==1.15.0",
9597
"opentelemetry-sdk==1.15.0",
9698
],
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.urllib import URLLibInstrumentor
10+
11+
12+
class TestUrllibInstrumentation(unittest.TestCase):
13+
def test_instrument(self):
14+
try:
15+
URLLibInstrumentor().instrument()
16+
except Exception as ex: # pylint: disable=broad-except
17+
print(ex)
18+
self.fail(
19+
f"Unexpected exception raised when instrumenting {URLLibInstrumentor.__name__}"
20+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.urllib3 import URLLib3Instrumentor
10+
11+
12+
class TestUrllib3Instrumentation(unittest.TestCase):
13+
def test_instrument(self):
14+
try:
15+
URLLib3Instrumentor().instrument()
16+
except Exception as ex: # pylint: disable=broad-except
17+
print(ex)
18+
self.fail(
19+
f"Unexpected exception raised when instrumenting {URLLib3Instrumentor.__name__}"
20+
)

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ fastapi
44
flask
55
psycopg2
66
requests
7+
urllib3

0 commit comments

Comments
 (0)