-
Notifications
You must be signed in to change notification settings - Fork 78
Add support for urllib3 + urllib instrumentation #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d938f81
urllib3
lzchen f62a38c
Merge branch 'main' of https://github.com/microsoft/ApplicationInsigh…
lzchen 6f4cc27
Update README.md
lzchen c891014
Update http_fastapi.py
lzchen 3f17d5c
cs
lzchen c5f5dc6
urllib
lzchen 11a41bf
Update test-requirements.txt
lzchen 79c74ff
Update http_urllib.py
lzchen 95cf300
Update http_urllib.py
lzchen d49268f
samplES
lzchen 703f869
Update manual.py
lzchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,8 @@ | |
| "flask", | ||
| "psycopg2", | ||
| "requests", | ||
| "urllib", | ||
| "urllib3", | ||
| ) | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
azure-monitor-opentelemetry/samples/tracing/http_urllib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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="<your-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() |
35 changes: 35 additions & 0 deletions
35
azure-monitor-opentelemetry/samples/tracing/http_urllib3.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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="<your-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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # 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.instrumentation.sqlalchemy import SQLAlchemyInstrumentor | ||
| from sqlalchemy import create_engine, text | ||
|
|
||
| configure_azure_monitor( | ||
| connection_string="<your-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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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="<your-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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
azure-monitor-opentelemetry/tests/instrumentation/test_urllib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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__}" | ||
| ) |
20 changes: 20 additions & 0 deletions
20
azure-monitor-opentelemetry/tests/instrumentation/test_urllib3.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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__}" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ fastapi | |
| flask | ||
| psycopg2 | ||
| requests | ||
| urllib3 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.