-
Notifications
You must be signed in to change notification settings - Fork 919
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
Merged
aabmass
merged 10 commits into
open-telemetry:main
from
herin049:feat/http-transport-create
Jun 24, 2026
Merged
opentelemetry-exporter-http-transport: enable entry-point loading of transport implementations #5320
Changes from all commits
Commits
Show all changes
10 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 f1e6fa6
Change kwargs type hint to Any in _requests.py
herin049 fa5c8aa
Refactor kwargs type hint to Any for urllib3
herin049 05001e6
Merge branch 'main' into feat/http-transport-create
herin049 a8ceda2
address comments
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
74 changes: 74 additions & 0 deletions
74
...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,76 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, cast | ||
|
|
||
| # pylint: disable-next=import-error | ||
| from opentelemetry.exporter.http.transport._requests import ( | ||
| RequestsHTTPTransport as _RequestsHTTPTransport, | ||
| ) | ||
|
|
||
| # pylint: disable-next=import-error | ||
| from opentelemetry.exporter.http.transport._urllib3 import ( | ||
| Urllib3HTTPTransport as _Urllib3HTTPTransport, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Protocol | ||
|
|
||
| from opentelemetry.exporter.http.transport._base import BaseHTTPTransport | ||
|
|
||
| class BaseHTTPTransportFactory(Protocol): | ||
| def __call__( | ||
| self, | ||
| *, | ||
| verify: bool | str, | ||
| cert: str | tuple[str, str] | None, | ||
| **kwargs: Any, | ||
| ) -> BaseHTTPTransport: ... | ||
|
|
||
|
|
||
| _KNOWN_TRANSPORTS: dict[str, BaseHTTPTransportFactory] = { | ||
| "requests": _RequestsHTTPTransport, | ||
| "urllib3": _Urllib3HTTPTransport, | ||
| } | ||
|
|
||
|
|
||
| def _load_http_transport_factory(name: str) -> BaseHTTPTransportFactory: | ||
| """Return the transport factory 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: A callable with signature | ||
| ``(*, verify, cert, **kwargs) -> BaseHTTPTransport``. | ||
| :raises ValueError: If no transport is registered under *name*. | ||
| :raises TypeError: If the loaded entry point is not callable. | ||
| """ | ||
| 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." | ||
| ) | ||
| factory = ep.load() | ||
| if not callable(factory): | ||
| raise TypeError( | ||
| f"Transport {name!r} loaded from entry point is not callable " | ||
| f"(got {factory!r})." | ||
| ) | ||
| return cast("BaseHTTPTransportFactory", factory) |
9 changes: 7 additions & 2 deletions
9
.../opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_base.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
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
61 changes: 61 additions & 0 deletions
61
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,61 @@ | ||
| # 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_factory | ||
| 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 TestLoadHTTPTransportFactory(unittest.TestCase): | ||
| def test_returns_requests_transport(self): | ||
| self.assertIs( | ||
| _load_http_transport_factory("requests"), RequestsHTTPTransport | ||
| ) | ||
|
|
||
| def test_returns_urllib3_transport(self): | ||
| self.assertIs( | ||
| _load_http_transport_factory("urllib3"), Urllib3HTTPTransport | ||
| ) | ||
|
|
||
| def test_known_transport_does_not_call_entry_points(self): | ||
| with patch(_ENTRY_POINTS_TARGET) as mock_ep: | ||
| _load_http_transport_factory("requests") | ||
| _load_http_transport_factory("urllib3") | ||
| self.assertFalse(mock_ep.called) | ||
|
|
||
| def test_unknown_transport_calls_entry_points(self): | ||
| def _custom_factory(*, verify, cert, **kwargs): | ||
| pass | ||
|
|
||
| mock_ep = MagicMock() | ||
| mock_ep.load.return_value = _custom_factory | ||
| with patch(_ENTRY_POINTS_TARGET, return_value=[mock_ep]) as mock_fn: | ||
| result = _load_http_transport_factory("custom") | ||
| self.assertEqual( | ||
| mock_fn.call_args.kwargs, | ||
| {"group": "opentelemetry_http_transport", "name": "custom"}, | ||
| ) | ||
| self.assertIs(result, _custom_factory) | ||
|
|
||
| def test_entry_point_non_callable_raises_type_error(self): | ||
| mock_ep = MagicMock() | ||
| mock_ep.load.return_value = "not_callable" | ||
| with patch(_ENTRY_POINTS_TARGET, return_value=[mock_ep]): | ||
| self.assertRaises(TypeError, _load_http_transport_factory, "bad") | ||
|
|
||
| def test_unknown_transport_raises_value_error(self): | ||
| with patch(_ENTRY_POINTS_TARGET, return_value=[]): | ||
| self.assertRaises( | ||
| ValueError, _load_http_transport_factory, "nonexistent" | ||
| ) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Uh oh!
There was an error while loading. Please reload this page.