From d938f8107bf8b570fd05decbb8ff5a062fc80e60 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 7 Mar 2023 10:28:14 -0800 Subject: [PATCH 01/10] urllib3 --- CHANGELOG.md | 2 ++ .../azure/monitor/opentelemetry/_configure.py | 1 + .../samples/tracing/http_urllib3.py | 35 +++++++++++++++++++ azure-monitor-opentelemetry/setup.py | 1 + .../tests/instrumentation/test_urllib3.py | 20 +++++++++++ test-requirements.txt | 1 + 6 files changed, 60 insertions(+) create mode 100644 azure-monitor-opentelemetry/samples/tracing/http_urllib3.py create mode 100644 azure-monitor-opentelemetry/tests/instrumentation/test_urllib3.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 55e0100a..45f90db4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ([#254](https://github.com/microsoft/ApplicationInsights-Python/pull/254)) - Add support for FastAPI instrumentation ([#255](https://github.com/microsoft/ApplicationInsights-Python/pull/255)) +- Add support for Urllib3 instrumentation + ([#256](https://github.com/microsoft/ApplicationInsights-Python/pull/256)) ## [1.0.0b10](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b10) - 2023-02-23 diff --git a/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py b/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py index 1c50ce58..b2082ebe 100644 --- a/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py +++ b/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py @@ -40,6 +40,7 @@ "flask", "psycopg2", "requests", + "urllib3", ) diff --git a/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py b/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py new file mode 100644 index 00000000..b78144c5 --- /dev/null +++ b/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py @@ -0,0 +1,35 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- +import logging + +import urllib3 +from azure.monitor.opentelemetry import configure_azure_monitor +from opentelemetry import trace + +logger = logging.getLogger(__name__) + +# Configure Azure monitor collection telemetry pipeline +configure_azure_monitor( + # connection_string="", + disable_logging=True, + disable_metrics=True, + tracing_export_interval_millis=15000, +) + +http = urllib3.PoolManager() + +tracer = trace.get_tracer(__name__) +with tracer.start_as_current_span("Request parent span") as span: + try: + # Requests made using the urllib3 library will be automatically captured + response = http.request("GET", "https://www.example.org/") + logger.warning("Request sent") + except Exception as ex: + # If an exception occurs, this can be manually recorded on the parent span + span.set_attribute("status", "exception") + span.record_exception(ex) + +input() diff --git a/azure-monitor-opentelemetry/setup.py b/azure-monitor-opentelemetry/setup.py index 36b84f77..c791ea16 100644 --- a/azure-monitor-opentelemetry/setup.py +++ b/azure-monitor-opentelemetry/setup.py @@ -91,6 +91,7 @@ "opentelemetry-instrumentation-flask~=0.36b0", "opentelemetry-instrumentation-psycopg2~=0.36b0", "opentelemetry-instrumentation-requests~=0.36b0", + "opentelemetry-instrumentation-urllib3~=0.36b0", "opentelemetry-api==1.15.0", "opentelemetry-sdk==1.15.0", ], diff --git a/azure-monitor-opentelemetry/tests/instrumentation/test_urllib3.py b/azure-monitor-opentelemetry/tests/instrumentation/test_urllib3.py new file mode 100644 index 00000000..55511ab1 --- /dev/null +++ b/azure-monitor-opentelemetry/tests/instrumentation/test_urllib3.py @@ -0,0 +1,20 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +import unittest + +from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor + + +class TestUrllib3Instrumentation(unittest.TestCase): + def test_instrument(self): + try: + URLLib3Instrumentor().instrument() + except Exception as ex: # pylint: disable=broad-except + print(ex) + self.fail( + f"Unexpected exception raised when instrumenting {URLLib3Instrumentor.__name__}" + ) diff --git a/test-requirements.txt b/test-requirements.txt index b7208bdc..42423dfc 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,3 +4,4 @@ fastapi flask psycopg2 requests +urllib3 From 6f4cc2780c278d9f0d848d311d25328e4cfbfdcc Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 7 Mar 2023 13:02:38 -0800 Subject: [PATCH 02/10] Update README.md --- azure-monitor-opentelemetry/README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/azure-monitor-opentelemetry/README.md b/azure-monitor-opentelemetry/README.md index 85b5be03..2b0e1235 100644 --- a/azure-monitor-opentelemetry/README.md +++ b/azure-monitor-opentelemetry/README.md @@ -10,11 +10,12 @@ This distro automatically installs the following libraries: 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. -* [OpenTelemetry Requests Instrumentation][opentelemetry_instrumentation_requests] * [OpenTelemetry Django Instrumentation][opentelemetry_instrumentation_django] * [OpenTelemetry FastApi Instrumentation][opentelemetry_instrumentation_fastapi] * [OpenTelemetry Flask Instrumentation][opentelemetry_instrumentation_flask] * [OpenTelemetry Psycopg2 Instrumentation][opentelemetry_instrumentation_psycopg2] +* [OpenTelemetry Requests Instrumentation][opentelemetry_instrumentation_requests] +* [OpenTelemetry UrlLib3 Instrumentation][opentelemetry_instrumentation_urllib3] ## Getting started @@ -117,11 +118,12 @@ Samples are available [here][samples] to demonstrate how to utilize the above co [ot_sdk_python_metric_reader]: https://opentelemetry-python.readthedocs.io/en/stable/sdk/metrics.export.html#opentelemetry.sdk.metrics.export.MetricReader [ot_sdk_python_resource]: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py#L153 [ot_sdk_python_view_examples]: https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples/metrics/views -[opentelemetry_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests [opentelemetry_instrumentation_django]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-django [opentelemetry_instrumentation_fastapi]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-fastapi [opentelemetry_instrumentation_flask]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask [opentelemetry_instrumentation_psycopg2]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-psycopg2 +[opentelemetry_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests +[opentelemetry_instrumentation_urllib3]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-urllib3 [opentelemetry_spec_resource]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#resource-sdk [opentelemetry_spec_view]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view [python]: https://www.python.org/downloads/ From c891014039538ef480e7d75c61de3b7fe41b57a3 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 7 Mar 2023 13:33:41 -0800 Subject: [PATCH 03/10] Update http_fastapi.py --- .../samples/tracing/http_fastapi.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py b/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py index 112f1acb..c53b6269 100644 --- a/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py +++ b/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py @@ -20,11 +20,17 @@ # Requests made to fastapi endpoints will be automatically captured @app.get("/") -async def root(): +async def test(): 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") + + # Telemetry from this endpoint will not be captured due to excluded_urls config above @app.get("/exclude") -async def root(): +async def exclude(): return {"message": "Telemetry was not captured"} From 3f17d5c5c443c7fbfa51da6906d8c49a78e85956 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 7 Mar 2023 14:27:27 -0800 Subject: [PATCH 04/10] cs --- azure-monitor-opentelemetry/samples/tracing/http_urllib3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py b/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py index b78144c5..6b2d8e8e 100644 --- a/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py +++ b/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py @@ -13,7 +13,7 @@ # Configure Azure monitor collection telemetry pipeline configure_azure_monitor( - # connection_string="", + connection_string="", disable_logging=True, disable_metrics=True, tracing_export_interval_millis=15000, From c5f5dc64dbcf122eee62060191b7ef42951a8148 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 7 Mar 2023 14:52:43 -0800 Subject: [PATCH 05/10] urllib --- CHANGELOG.md | 2 +- azure-monitor-opentelemetry/README.md | 2 ++ .../azure/monitor/opentelemetry/_configure.py | 1 + .../samples/tracing/http_urllib.py | 34 +++++++++++++++++++ azure-monitor-opentelemetry/setup.py | 1 + .../tests/instrumentation/test_urllib.py | 20 +++++++++++ test-requirements.txt | 1 + 7 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 azure-monitor-opentelemetry/samples/tracing/http_urllib.py create mode 100644 azure-monitor-opentelemetry/tests/instrumentation/test_urllib.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 45f90db4..3373959c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ([#254](https://github.com/microsoft/ApplicationInsights-Python/pull/254)) - Add support for FastAPI instrumentation ([#255](https://github.com/microsoft/ApplicationInsights-Python/pull/255)) -- Add support for Urllib3 instrumentation +- Add support for Urllib3/Urllib instrumentation ([#256](https://github.com/microsoft/ApplicationInsights-Python/pull/256)) ## [1.0.0b10](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b10) - 2023-02-23 diff --git a/azure-monitor-opentelemetry/README.md b/azure-monitor-opentelemetry/README.md index 2b0e1235..f6f1cfa0 100644 --- a/azure-monitor-opentelemetry/README.md +++ b/azure-monitor-opentelemetry/README.md @@ -15,6 +15,7 @@ The following OpenTelemetry instrumentations come bundled in with the Azure moni * [OpenTelemetry Flask Instrumentation][opentelemetry_instrumentation_flask] * [OpenTelemetry Psycopg2 Instrumentation][opentelemetry_instrumentation_psycopg2] * [OpenTelemetry Requests Instrumentation][opentelemetry_instrumentation_requests] +* [OpenTelemetry UrlLib Instrumentation][opentelemetry_instrumentation_urllib] * [OpenTelemetry UrlLib3 Instrumentation][opentelemetry_instrumentation_urllib3] ## Getting started @@ -123,6 +124,7 @@ Samples are available [here][samples] to demonstrate how to utilize the above co [opentelemetry_instrumentation_flask]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask [opentelemetry_instrumentation_psycopg2]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-psycopg2 [opentelemetry_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests +[opentelemetry_instrumentation_urllib]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-urllib [opentelemetry_instrumentation_urllib3]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-urllib3 [opentelemetry_spec_resource]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#resource-sdk [opentelemetry_spec_view]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view diff --git a/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py b/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py index b2082ebe..f08a80af 100644 --- a/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py +++ b/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py @@ -40,6 +40,7 @@ "flask", "psycopg2", "requests", + "urllib", "urllib3", ) diff --git a/azure-monitor-opentelemetry/samples/tracing/http_urllib.py b/azure-monitor-opentelemetry/samples/tracing/http_urllib.py new file mode 100644 index 00000000..8fcf7161 --- /dev/null +++ b/azure-monitor-opentelemetry/samples/tracing/http_urllib.py @@ -0,0 +1,34 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- +import logging + +from urllib import request +from azure.monitor.opentelemetry import configure_azure_monitor +from opentelemetry import trace + +logger = logging.getLogger(__name__) + +# Configure Azure monitor collection telemetry pipeline +configure_azure_monitor( + connection_string="", + disable_logging=True, + disable_metrics=True, + tracing_export_interval_millis=15000, +) + +tracer = trace.get_tracer(__name__) +with tracer.start_as_current_span("Request parent span") as span: + try: + # Requests made using the urllib library will be automatically captured + req = request.Request('https://www.example.org/', method="GET") + r = request.urlopen(req) + logger.warning("Request sent") + except Exception as ex: + # If an exception occurs, this can be manually recorded on the parent span + span.set_attribute("status", "exception") + span.record_exception(ex) + +input() diff --git a/azure-monitor-opentelemetry/setup.py b/azure-monitor-opentelemetry/setup.py index c791ea16..e7f114c7 100644 --- a/azure-monitor-opentelemetry/setup.py +++ b/azure-monitor-opentelemetry/setup.py @@ -91,6 +91,7 @@ "opentelemetry-instrumentation-flask~=0.36b0", "opentelemetry-instrumentation-psycopg2~=0.36b0", "opentelemetry-instrumentation-requests~=0.36b0", + "opentelemetry-instrumentation-urllib~=0.36b0", "opentelemetry-instrumentation-urllib3~=0.36b0", "opentelemetry-api==1.15.0", "opentelemetry-sdk==1.15.0", diff --git a/azure-monitor-opentelemetry/tests/instrumentation/test_urllib.py b/azure-monitor-opentelemetry/tests/instrumentation/test_urllib.py new file mode 100644 index 00000000..bcd40384 --- /dev/null +++ b/azure-monitor-opentelemetry/tests/instrumentation/test_urllib.py @@ -0,0 +1,20 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +import unittest + +from opentelemetry.instrumentation.urllib import URLLibInstrumentor + + +class TestUrllibInstrumentation(unittest.TestCase): + def test_instrument(self): + try: + URLLibInstrumentor().instrument() + except Exception as ex: # pylint: disable=broad-except + print(ex) + self.fail( + f"Unexpected exception raised when instrumenting {URLLibInstrumentor.__name__}" + ) diff --git a/test-requirements.txt b/test-requirements.txt index 42423dfc..ddcb54a9 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,4 +4,5 @@ fastapi flask psycopg2 requests +urllib urllib3 From 11a41bf1212668847af30218d2d2361d989eb3e3 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 7 Mar 2023 15:06:01 -0800 Subject: [PATCH 06/10] Update test-requirements.txt --- test-requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/test-requirements.txt b/test-requirements.txt index ddcb54a9..42423dfc 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,5 +4,4 @@ fastapi flask psycopg2 requests -urllib urllib3 From 79c74ff38750bf21a3024a77a13cdb5ac2f8dfa5 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 7 Mar 2023 15:12:44 -0800 Subject: [PATCH 07/10] Update http_urllib.py --- azure-monitor-opentelemetry/samples/tracing/http_urllib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-monitor-opentelemetry/samples/tracing/http_urllib.py b/azure-monitor-opentelemetry/samples/tracing/http_urllib.py index 8fcf7161..19ba2a90 100644 --- a/azure-monitor-opentelemetry/samples/tracing/http_urllib.py +++ b/azure-monitor-opentelemetry/samples/tracing/http_urllib.py @@ -23,7 +23,7 @@ with tracer.start_as_current_span("Request parent span") as span: try: # Requests made using the urllib library will be automatically captured - req = request.Request('https://www.example.org/', method="GET") + req = request.Request("https://www.example.org/", method="GET") r = request.urlopen(req) logger.warning("Request sent") except Exception as ex: From 95cf300cb192bccb9d75753c4be94f18c63789b9 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 7 Mar 2023 15:26:45 -0800 Subject: [PATCH 08/10] Update http_urllib.py --- azure-monitor-opentelemetry/samples/tracing/http_urllib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-monitor-opentelemetry/samples/tracing/http_urllib.py b/azure-monitor-opentelemetry/samples/tracing/http_urllib.py index 19ba2a90..cf2eb453 100644 --- a/azure-monitor-opentelemetry/samples/tracing/http_urllib.py +++ b/azure-monitor-opentelemetry/samples/tracing/http_urllib.py @@ -4,8 +4,8 @@ # license information. # -------------------------------------------------------------------------- import logging - from urllib import request + from azure.monitor.opentelemetry import configure_azure_monitor from opentelemetry import trace From d49268f783c358101be7c2f4876889d5af989099 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 8 Mar 2023 10:51:17 -0800 Subject: [PATCH 09/10] samplES --- azure-monitor-opentelemetry/README.md | 3 +- .../samples/tracing/db_psycopg2.py | 1 + .../samples/tracing/manual.py | 31 +++++++++++++++++++ .../samples/tracing/sampling.py | 27 ++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 azure-monitor-opentelemetry/samples/tracing/manual.py create mode 100644 azure-monitor-opentelemetry/samples/tracing/sampling.py diff --git a/azure-monitor-opentelemetry/README.md b/azure-monitor-opentelemetry/README.md index f6f1cfa0..1c621150 100644 --- a/azure-monitor-opentelemetry/README.md +++ b/azure-monitor-opentelemetry/README.md @@ -8,7 +8,7 @@ This distro automatically installs the following libraries: ## Officially supported instrumentations -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. +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. * [OpenTelemetry Django Instrumentation][opentelemetry_instrumentation_django] * [OpenTelemetry FastApi Instrumentation][opentelemetry_instrumentation_fastapi] @@ -131,3 +131,4 @@ Samples are available [here][samples] to demonstrate how to utilize the above co [python]: https://www.python.org/downloads/ [pip]: https://pypi.org/project/pip/ [samples]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry/samples +[samples_manual]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry/samples/tracing/manual.py diff --git a/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py b/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py index e6aa9a44..72bfab97 100644 --- a/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py +++ b/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py @@ -14,6 +14,7 @@ tracing_export_interval_millis=15000, ) +# Database calls using the psycopg2 library will be automatically captured cnx = psycopg2.connect(database="test", user="", password="") cursor = cnx.cursor() cursor.execute("INSERT INTO test_tables (test_field) VALUES (123)") diff --git a/azure-monitor-opentelemetry/samples/tracing/manual.py b/azure-monitor-opentelemetry/samples/tracing/manual.py new file mode 100644 index 00000000..285c1fc9 --- /dev/null +++ b/azure-monitor-opentelemetry/samples/tracing/manual.py @@ -0,0 +1,31 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- +from sqlalchemy import create_engine, text + +from azure.monitor.opentelemetry import configure_azure_monitor +from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor + +configure_azure_monitor( + connection_string="", + tracing_export_interval_millis=15000, + disable_logging=True, + disable_metrics=True, +) + +engine = create_engine("sqlite:///:memory:") +# SQLAlchemy instrumentation is not officially supported by this package +# However, you can use the OpenTelemetry instument method manually in +# conjunction with configure_azure_monitor +SQLAlchemyInstrumentor().instrument( + engine=engine, +) + +# Database calls using the SqlAlchemy library will be automatically captured +with engine.connect() as conn: + result = conn.execute(text("select 'hello world'")) + print(result.all()) + +input() diff --git a/azure-monitor-opentelemetry/samples/tracing/sampling.py b/azure-monitor-opentelemetry/samples/tracing/sampling.py new file mode 100644 index 00000000..ad0c8a77 --- /dev/null +++ b/azure-monitor-opentelemetry/samples/tracing/sampling.py @@ -0,0 +1,27 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +from azure.monitor.opentelemetry import configure_azure_monitor +from opentelemetry import trace + +configure_azure_monitor( + connection_string="", + # Sampling ratio of between 0 and 1 inclusive + # 0.1 means approximately 10% of your traces are sent + sampling_ratio=0.1, + tracing_export_interval_millis=15000, + disable_logging=True, + disable_metrics=True, +) + +tracer = trace.get_tracer(__name__) + +for i in range(100): + # Approximately 90% of these spans should be sampled out + with tracer.start_as_current_span("hello"): + print("Hello, World!") + +input() From 703f8692d08ea142e040f1751b4f33ab35bdff70 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 8 Mar 2023 10:56:03 -0800 Subject: [PATCH 10/10] Update manual.py --- azure-monitor-opentelemetry/samples/tracing/manual.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/azure-monitor-opentelemetry/samples/tracing/manual.py b/azure-monitor-opentelemetry/samples/tracing/manual.py index 285c1fc9..122ca429 100644 --- a/azure-monitor-opentelemetry/samples/tracing/manual.py +++ b/azure-monitor-opentelemetry/samples/tracing/manual.py @@ -3,10 +3,9 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- -from sqlalchemy import create_engine, text - from azure.monitor.opentelemetry import configure_azure_monitor from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor +from sqlalchemy import create_engine, text configure_azure_monitor( connection_string="",