-
Notifications
You must be signed in to change notification settings - Fork 916
opentelemetry-exporter-http-transport: enable entry-point loading of transport implementations #5320
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
Open
herin049
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
herin049:feat/http-transport-create
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
opentelemetry-exporter-http-transport: enable entry-point loading of transport implementations #5320
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c15147
opentelemetry-exporter-http-transport: enable entry-point loading of …
herin049 71f6d7b
add changelog fragment
herin049 7e1051e
fix Python 3.10 typing import error
herin049 e581987
fix typecheck errors
herin049 dc75887
check that returned class is a subclass of BaseHTTPTransport
herin049 b64796e
Merge branch 'main' into feat/http-transport-create
herin049 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `opentelemetry-exporter-http-transport`: enable entry-point loading of transport implementations |
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
77 changes: 77 additions & 0 deletions
77
...entelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/__init__.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 |
|---|---|---|
| @@ -1,2 +1,79 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Callable | ||
|
|
||
| from opentelemetry.exporter.http.transport._base import BaseHTTPTransport | ||
|
|
||
|
|
||
| def _load_requests_transport() -> type[BaseHTTPTransport]: | ||
| # pylint: disable-next=import-outside-toplevel,import-error | ||
| from opentelemetry.exporter.http.transport._requests import ( # noqa: PLC0415 | ||
| RequestsHTTPTransport, | ||
| ) | ||
|
|
||
| return RequestsHTTPTransport | ||
|
|
||
|
|
||
| def _load_urllib3_transport() -> type[BaseHTTPTransport]: | ||
| # pylint: disable-next=import-outside-toplevel,import-error | ||
| from opentelemetry.exporter.http.transport._urllib3 import ( # noqa: PLC0415 | ||
| Urllib3HTTPTransport, | ||
| ) | ||
|
|
||
| return Urllib3HTTPTransport | ||
|
|
||
|
|
||
| _KNOWN_TRANSPORTS: dict[str, Callable[[], type[BaseHTTPTransport]]] = { | ||
| "requests": _load_requests_transport, | ||
| "urllib3": _load_urllib3_transport, | ||
| } | ||
|
|
||
|
|
||
| def _load_http_transport_class(name: str) -> type[BaseHTTPTransport]: | ||
| """Return the transport class registered under *name*. | ||
|
|
||
| Checks the built-in transport registry first to avoid the overhead of an | ||
| entry-point scan for built-in transports. Falls back to standard entry-point | ||
| discovery for user supplied transports registered under the | ||
| ``opentelemetry_http_transport`` group. | ||
|
|
||
| :param name: Entry point name, e.g. ``"requests"`` or ``"urllib3"``. | ||
| :returns: The :class:`~opentelemetry.exporter.http.transport._base.BaseHTTPTransport` | ||
| subclass registered under *name*. | ||
| :raises ValueError: If no transport is registered under *name*. | ||
| """ | ||
| if name in _KNOWN_TRANSPORTS: | ||
| return _KNOWN_TRANSPORTS[name]() | ||
| # pylint: disable-next=import-outside-toplevel,import-error | ||
| from opentelemetry.util._importlib_metadata import ( # noqa: PLC0415 | ||
| entry_points, | ||
| ) | ||
|
|
||
| ep = next( | ||
| iter(entry_points(group="opentelemetry_http_transport", name=name)), | ||
| None, | ||
| ) | ||
| if not ep: | ||
| raise ValueError( | ||
| f"No HTTP transport registered under name {name!r}. " | ||
| "Install the corresponding extra or register an entry point " | ||
| "under the 'opentelemetry_http_transport' group." | ||
| ) | ||
| cls = ep.load() | ||
| # pylint: disable-next=import-outside-toplevel,import-error | ||
| from opentelemetry.exporter.http.transport._base import ( # noqa: PLC0415 | ||
| BaseHTTPTransport, | ||
| ) | ||
|
|
||
| if not isinstance(cls, type) or not issubclass(cls, BaseHTTPTransport): | ||
| raise TypeError( | ||
| f"Transport {name!r} loaded from entry point does not subclass " | ||
| f"BaseHTTPTransport (got {cls!r})." | ||
| ) | ||
| return cls |
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
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
72 changes: 72 additions & 0 deletions
72
exporter/opentelemetry-exporter-http-transport/tests/test_load_transport.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,72 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # pylint: disable=import-error | ||
|
|
||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from opentelemetry.exporter.http.transport import _load_http_transport_class | ||
| from opentelemetry.exporter.http.transport._base import BaseHTTPTransport | ||
| from opentelemetry.exporter.http.transport._requests import ( | ||
| RequestsHTTPTransport, | ||
| ) | ||
| from opentelemetry.exporter.http.transport._urllib3 import ( | ||
| Urllib3HTTPTransport, | ||
| ) | ||
|
|
||
| _ENTRY_POINTS_TARGET = "opentelemetry.util._importlib_metadata.entry_points" | ||
|
|
||
|
|
||
| # pylint: disable=no-self-use | ||
| class TestLoadHTTPTransportClass(unittest.TestCase): | ||
| def test_returns_requests_transport(self): | ||
| self.assertIs( | ||
| _load_http_transport_class("requests"), RequestsHTTPTransport | ||
| ) | ||
|
|
||
| def test_returns_urllib3_transport(self): | ||
| self.assertIs( | ||
| _load_http_transport_class("urllib3"), Urllib3HTTPTransport | ||
| ) | ||
|
|
||
| def test_known_transport_does_not_call_entry_points(self): | ||
| with patch(_ENTRY_POINTS_TARGET) as mock_ep: | ||
| _load_http_transport_class("requests") | ||
| _load_http_transport_class("urllib3") | ||
| self.assertFalse(mock_ep.called) | ||
|
|
||
| def test_unknown_transport_calls_entry_points(self): | ||
| class _CustomTransport: | ||
| pass | ||
|
|
||
| BaseHTTPTransport.register(_CustomTransport) | ||
| mock_ep = MagicMock() | ||
| mock_ep.load.return_value = _CustomTransport | ||
| with patch(_ENTRY_POINTS_TARGET, return_value=[mock_ep]) as mock_fn: | ||
| result = _load_http_transport_class("custom") | ||
| self.assertEqual( | ||
| mock_fn.call_args.kwargs, | ||
| {"group": "opentelemetry_http_transport", "name": "custom"}, | ||
| ) | ||
| self.assertIs(result, _CustomTransport) | ||
|
|
||
| def test_entry_point_non_subclass_raises_type_error(self): | ||
| class _NotATransport: | ||
| pass | ||
|
|
||
| mock_ep = MagicMock() | ||
| mock_ep.load.return_value = _NotATransport | ||
| with patch(_ENTRY_POINTS_TARGET, return_value=[mock_ep]): | ||
| self.assertRaises(TypeError, _load_http_transport_class, "bad") | ||
|
|
||
| def test_entry_point_non_class_raises_type_error(self): | ||
| mock_ep = MagicMock() | ||
| mock_ep.load.return_value = object() | ||
| with patch(_ENTRY_POINTS_TARGET, return_value=[mock_ep]): | ||
| self.assertRaises(TypeError, _load_http_transport_class, "bad") | ||
|
|
||
| def test_unknown_transport_raises_value_error(self): | ||
| with patch(_ENTRY_POINTS_TARGET, return_value=[]): | ||
| self.assertRaises( | ||
| ValueError, _load_http_transport_class, "nonexistent" | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to register these all the time? Won't that cause them to always use their entrypoints?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically these aren't needed, but I've added them for consistency (easy to search for all transport implementations) and to provide an example for users to provide their own entry-points (we follow the same pattern in the SDK). We do the check for known entry-points before we attempt to load anything, so these are still optimized away.