diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f3fe9675be..4b19e74504 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -195,6 +195,25 @@ jobs: - name: Run tests run: tox -e lint-opentelemetry-opencensus-shim + lint-opentelemetry-exporter-http-transport: + name: opentelemetry-exporter-http-transport + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.14 + uses: actions/setup-python@v5 + with: + python-version: "3.14" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e lint-opentelemetry-exporter-http-transport + lint-opentelemetry-exporter-opencensus: name: opentelemetry-exporter-opencensus runs-on: ubuntu-latest diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b481fbfa70..a1b6b75289 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1414,6 +1414,139 @@ jobs: - name: Run tests run: tox -e py314-test-opentelemetry-opencensus-shim -- -ra + py310-test-opentelemetry-exporter-http-transport_ubuntu-latest: + name: opentelemetry-exporter-http-transport 3.10 Ubuntu + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.10 + uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py310-test-opentelemetry-exporter-http-transport -- -ra + + py311-test-opentelemetry-exporter-http-transport_ubuntu-latest: + name: opentelemetry-exporter-http-transport 3.11 Ubuntu + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.11 + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py311-test-opentelemetry-exporter-http-transport -- -ra + + py312-test-opentelemetry-exporter-http-transport_ubuntu-latest: + name: opentelemetry-exporter-http-transport 3.12 Ubuntu + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.12 + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py312-test-opentelemetry-exporter-http-transport -- -ra + + py313-test-opentelemetry-exporter-http-transport_ubuntu-latest: + name: opentelemetry-exporter-http-transport 3.13 Ubuntu + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.13 + uses: actions/setup-python@v5 + with: + python-version: "3.13" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py313-test-opentelemetry-exporter-http-transport -- -ra + + py314-test-opentelemetry-exporter-http-transport_ubuntu-latest: + name: opentelemetry-exporter-http-transport 3.14 Ubuntu + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.14 + uses: actions/setup-python@v5 + with: + python-version: "3.14" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py314-test-opentelemetry-exporter-http-transport -- -ra + + py314t-test-opentelemetry-exporter-http-transport_ubuntu-latest: + name: opentelemetry-exporter-http-transport 3.14t Ubuntu + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.14t + uses: actions/setup-python@v5 + with: + python-version: "3.14t" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py314t-test-opentelemetry-exporter-http-transport -- -ra + + pypy3-test-opentelemetry-exporter-http-transport_ubuntu-latest: + name: opentelemetry-exporter-http-transport pypy-3.10 Ubuntu + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python pypy-3.10 + uses: actions/setup-python@v5 + with: + python-version: "pypy-3.10" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e pypy3-test-opentelemetry-exporter-http-transport -- -ra + py310-test-opentelemetry-exporter-opencensus_ubuntu-latest: name: opentelemetry-exporter-opencensus 3.10 Ubuntu runs-on: ubuntu-latest @@ -4667,6 +4800,153 @@ jobs: - name: Run tests run: tox -e py314-test-opentelemetry-opencensus-shim -- -ra + py310-test-opentelemetry-exporter-http-transport_windows-latest: + name: opentelemetry-exporter-http-transport 3.10 Windows + runs-on: windows-latest + timeout-minutes: 30 + steps: + - name: Configure git to support long filenames + run: git config --system core.longpaths true + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.10 + uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py310-test-opentelemetry-exporter-http-transport -- -ra + + py311-test-opentelemetry-exporter-http-transport_windows-latest: + name: opentelemetry-exporter-http-transport 3.11 Windows + runs-on: windows-latest + timeout-minutes: 30 + steps: + - name: Configure git to support long filenames + run: git config --system core.longpaths true + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.11 + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py311-test-opentelemetry-exporter-http-transport -- -ra + + py312-test-opentelemetry-exporter-http-transport_windows-latest: + name: opentelemetry-exporter-http-transport 3.12 Windows + runs-on: windows-latest + timeout-minutes: 30 + steps: + - name: Configure git to support long filenames + run: git config --system core.longpaths true + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.12 + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py312-test-opentelemetry-exporter-http-transport -- -ra + + py313-test-opentelemetry-exporter-http-transport_windows-latest: + name: opentelemetry-exporter-http-transport 3.13 Windows + runs-on: windows-latest + timeout-minutes: 30 + steps: + - name: Configure git to support long filenames + run: git config --system core.longpaths true + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.13 + uses: actions/setup-python@v5 + with: + python-version: "3.13" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py313-test-opentelemetry-exporter-http-transport -- -ra + + py314-test-opentelemetry-exporter-http-transport_windows-latest: + name: opentelemetry-exporter-http-transport 3.14 Windows + runs-on: windows-latest + timeout-minutes: 30 + steps: + - name: Configure git to support long filenames + run: git config --system core.longpaths true + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.14 + uses: actions/setup-python@v5 + with: + python-version: "3.14" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py314-test-opentelemetry-exporter-http-transport -- -ra + + py314t-test-opentelemetry-exporter-http-transport_windows-latest: + name: opentelemetry-exporter-http-transport 3.14t Windows + runs-on: windows-latest + timeout-minutes: 30 + steps: + - name: Configure git to support long filenames + run: git config --system core.longpaths true + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.14t + uses: actions/setup-python@v5 + with: + python-version: "3.14t" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py314t-test-opentelemetry-exporter-http-transport -- -ra + + pypy3-test-opentelemetry-exporter-http-transport_windows-latest: + name: opentelemetry-exporter-http-transport pypy-3.10 Windows + runs-on: windows-latest + timeout-minutes: 30 + steps: + - name: Configure git to support long filenames + run: git config --system core.longpaths true + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python pypy-3.10 + uses: actions/setup-python@v5 + with: + python-version: "pypy-3.10" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e pypy3-test-opentelemetry-exporter-http-transport -- -ra + py310-test-opentelemetry-exporter-opencensus_windows-latest: name: opentelemetry-exporter-opencensus 3.10 Windows runs-on: windows-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index 16a3c35150..e53bbbf189 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#5095](https://github.com/open-telemetry/opentelemetry-python/pull/5095)) - Add `registry` keyword argument to `PrometheusMetricReader` to allow passing a custom Prometheus registry ([#5055](https://github.com/open-telemetry/opentelemetry-python/pull/5055)) +- `opentelemetry-exporter-http-transport`: add 'opentelemetry-exporter-http-transport' package for HTTP exporters + ([#5194](https://github.com/open-telemetry/opentelemetry-python/pull/5194)) ## Version 1.41.0/0.62b0 (2026-04-09) diff --git a/eachdist.ini b/eachdist.ini index 09e62be3b2..67fe4ec870 100644 --- a/eachdist.ini +++ b/eachdist.ini @@ -32,6 +32,7 @@ version=0.63b0.dev packages= opentelemetry-opentracing-shim opentelemetry-opencensus-shim + opentelemetry-exporter-http-transport opentelemetry-exporter-opencensus opentelemetry-exporter-prometheus opentelemetry-exporter-otlp-json-common diff --git a/exporter/opentelemetry-exporter-http-transport/LICENSE b/exporter/opentelemetry-exporter-http-transport/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/exporter/opentelemetry-exporter-http-transport/README.rst b/exporter/opentelemetry-exporter-http-transport/README.rst new file mode 100644 index 0000000000..67dfec0a04 --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/README.rst @@ -0,0 +1,37 @@ +OpenTelemetry Exporters HTTP Transport +====================================== + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-exporter-http-transport.svg + :target: https://pypi.org/project/opentelemetry-exporter-http-transport/ + +This package provides shared HTTP transport abstractions and an OTLP HTTP +client used by OpenTelemetry exporters. + +The package has **no required dependencies**. The ``requests`` and ``urllib3`` +transports are available as optional extras. + +Installation +------------ + +Core package (no HTTP backend included):: + + pip install opentelemetry-exporter-http-transport + +With the ``requests`` backend:: + + pip install opentelemetry-exporter-http-transport[requests] + +With the ``urllib3`` backend:: + + pip install opentelemetry-exporter-http-transport[urllib3] + + +References +---------- + +* `OpenTelemetry `_ +* `OpenTelemetry Protocol Specification `_ +* `requests `_ +* `urllib3 `_ diff --git a/exporter/opentelemetry-exporter-http-transport/pyproject.toml b/exporter/opentelemetry-exporter-http-transport/pyproject.toml new file mode 100644 index 0000000000..39991aa33a --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/pyproject.toml @@ -0,0 +1,52 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "opentelemetry-exporter-http-transport" +dynamic = ["version"] +description = "OpenTelemetry Exporters HTTP transport" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "OpenTelemetry Authors", email = "cncf-opentelemetry-contributors@lists.cncf.io" }, +] +classifiers = [ + "Development Status :: 4 - Beta", + "Framework :: OpenTelemetry", + "Framework :: OpenTelemetry :: Exporters", + "Intended Audience :: Developers", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", +] +dependencies = [] + +[project.optional-dependencies] +urllib3 = [ + "urllib3 >= 1.11" +] +requests = [ + "requests ~= 2.7" +] + +[project.urls] +Homepage = "https://github.com/open-telemetry/opentelemetry-python/tree/main/exporter/opentelemetry-exporter-http-transport" +Repository = "https://github.com/open-telemetry/opentelemetry-python" + +[tool.hatch.version] +path = "src/opentelemetry/exporter/http/transport/version/__init__.py" + +[tool.hatch.build.targets.sdist] +include = [ + "/src", + "/tests", +] + +[tool.hatch.build.targets.wheel] +packages = ["src/opentelemetry"] diff --git a/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/__init__.py b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/__init__.py new file mode 100644 index 0000000000..e57cf4aba9 --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/__init__.py @@ -0,0 +1,2 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 diff --git a/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_base.py b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_base.py new file mode 100644 index 0000000000..a52de97a85 --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_base.py @@ -0,0 +1,50 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +from abc import ABC, abstractmethod +from dataclasses import dataclass + + +@dataclass(frozen=True, slots=True) +class BaseHTTPResult(ABC): + """Outcome of a single HTTP request made by a :class:`BaseHTTPTransport`. + + Either ``status_code`` and ``reason`` are populated (server responded), + or ``error`` is set (request failed before a response was received). + """ + + status_code: int | None = None + reason: str | None = None + error: Exception | None = None + + @abstractmethod + def is_connection_error(self) -> bool: + """Return ``True`` if the failure is a transport-level connection error.""" + + +class BaseHTTPTransport(ABC): + """Abstract HTTP transport interface used by OTLP HTTP exporters.""" + + @abstractmethod + def request( + self, + method: str, + url: str, + *, + headers: dict[str, str] | None = None, + timeout: float | None = None, + data: bytes | None = None, + ) -> BaseHTTPResult: + """Send an HTTP request and return the result. + + :param method: HTTP method (e.g. ``"POST"``). + :param url: Target URL. + :param headers: Optional HTTP headers to include in the request. + :param timeout: Optional request timeout in seconds. + :param data: Optional request body. + :returns: A :class:`BaseHTTPResult` describing the outcome. + """ + + @abstractmethod + def close(self) -> None: + """Release any resources held by the transport.""" diff --git a/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_otlp_client.py b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_otlp_client.py new file mode 100644 index 0000000000..a11b2f8829 --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_otlp_client.py @@ -0,0 +1,202 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +import enum +import gzip +import logging +import random +import threading +import time +import zlib +from collections.abc import Mapping +from dataclasses import dataclass +from http import HTTPStatus +from io import BytesIO +from typing import Final, Literal + +from opentelemetry.exporter.http.transport._base import ( + BaseHTTPResult, + BaseHTTPTransport, +) + +_logger = logging.getLogger(__name__) + +_MAX_RETRIES: Final[int] = 6 + + +def _is_retryable(status_code: int | None) -> bool: + if status_code is None: + return False + if status_code == HTTPStatus.REQUEST_TIMEOUT.value: + return True + if 500 <= status_code <= 599: + return True + return False + + +class Compression(enum.Enum): + NONE = "none" + DEFLATE = "deflate" + GZIP = "gzip" + + @staticmethod + def from_str(value: str) -> "Compression": + match value.strip().lower(): + case "none": + return Compression.NONE + case "deflate": + return Compression.DEFLATE + case "gzip": + return Compression.GZIP + case _: + _logger.warning("Unknown compression type: %s", value) + return Compression.NONE + + +@dataclass(slots=True, frozen=True) +class ExportResult: + """Outcome of an OTLP export attempt, including retry exhaustion.""" + + success: bool + status_code: int | None + reason: str | None + error: Exception | None + + +class OTLPHTTPClient: + """Sends serialized OTLP payloads over HTTP with retry logic. + + Compression, backoff, and connection-error recovery are handled internally. + Callers interact through the :meth:`export` and :meth:`close` methods. + """ + + def __init__( + self, + transport: BaseHTTPTransport, + endpoint: str, + timeout: float, + compression: Compression, + shutdown_event: threading.Event, + headers: Mapping[str, str], + kind: Literal["spans", "logs", "metrics"], + jitter: float = 0.2, + ) -> None: + self._transport = transport + self._endpoint = endpoint + self._timeout = timeout + self._compression = compression + self._shutdown_event = shutdown_event + self._headers = dict(headers) + self._kind = kind + self._jitter = min(max(jitter, 0.0), 1.0) + + def _compute_backoff(self, retry: int) -> float: + return 2**retry * random.uniform(1 - self._jitter, 1 + self._jitter) + + def _compress(self, serialized_data: bytes) -> bytes: + if self._compression is Compression.GZIP: + buf = BytesIO() + with gzip.GzipFile(fileobj=buf, mode="w") as gz: + gz.write(serialized_data) + return buf.getvalue() + if self._compression is Compression.DEFLATE: + return zlib.compress(serialized_data) + return serialized_data + + def _submit(self, data: bytes, timeout: float) -> BaseHTTPResult: + deadline = time.time() + timeout + result = self._transport.request( + "POST", + self._endpoint, + headers=self._headers, + data=data, + timeout=timeout, + ) + if ( + result.error is not None + and result.is_connection_error() + and (remaining := deadline - time.time()) > 0 + ): + # Immediately retry connection errors once without backoff. These + # usually indicate a stale pooled connection that the transport will + # reestablish on the next attempt. + result = self._transport.request( + "POST", + self._endpoint, + headers=self._headers, + data=data, + timeout=remaining, + ) + return result + + def export(self, data: bytes) -> ExportResult: + """Export a serialized payload, retrying on transient failures. + + :param data: Serialized bytes to send. + :returns: An :class:`ExportResult` indicating success or the reason for failure. + """ + data = self._compress(data) + deadline = time.time() + self._timeout + + for retry in range(_MAX_RETRIES): + backoff = self._compute_backoff(retry) + status_code: int | None = None + reason: str | None = None + export_error: Exception | None + retryable: bool + + try: + result = self._submit(data, max(deadline - time.time(), 0.0)) + # pylint: disable-next=broad-exception-caught + except Exception as error: + export_error = error + retryable = False + else: + status_code = result.status_code + reason = result.reason + if status_code is not None and 200 <= status_code < 400: + return ExportResult(True, status_code, reason, None) + export_error = result.error + retryable = ( + _is_retryable(status_code) + if status_code + else result.is_connection_error() + ) + + if not retryable: + _logger.error( + "Failed to export %s batch code: %s, reason: %s", + self._kind, + status_code, + reason or export_error or "unknown", + ) + return ExportResult(False, status_code, reason, export_error) + + if ( + retry + 1 == _MAX_RETRIES + or backoff > (deadline - time.time()) + or self._shutdown_event.is_set() + ): + _logger.error( + "Failed to export %s batch due to timeout, " + "max retries or shutdown.", + self._kind, + ) + return ExportResult(False, status_code, reason, export_error) + + _logger.warning( + "Transient error %s encountered while exporting %s batch, retrying in %.2fs.", + reason or export_error, + self._kind, + backoff, + ) + shutdown = self._shutdown_event.wait(backoff) + if shutdown: + _logger.warning("Shutdown in progress, aborting retry.") + break + + return ExportResult(False, None, None, None) + + def close(self) -> None: + """Close the underlying transport and release its resources.""" + self._transport.close() diff --git a/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_requests.py b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_requests.py new file mode 100644 index 0000000000..f673de316b --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_requests.py @@ -0,0 +1,95 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +from __future__ import annotations + +import functools +import warnings +from dataclasses import dataclass +from typing import TYPE_CHECKING + +from opentelemetry.exporter.http.transport._base import ( + BaseHTTPResult, + BaseHTTPTransport, +) + +if TYPE_CHECKING: + import requests + + +@functools.cache +def _get_connection_error_types() -> tuple[type[Exception], ...]: + # pylint: disable-next=import-outside-toplevel + import requests.exceptions # noqa: PLC0415 + + return ( + requests.exceptions.ConnectionError, + requests.exceptions.ConnectTimeout, + requests.exceptions.ReadTimeout, + requests.exceptions.Timeout, + requests.exceptions.SSLError, + requests.exceptions.ProxyError, + ) + + +@dataclass(frozen=True, slots=True) +class RequestsHTTPResult(BaseHTTPResult): + def is_connection_error(self) -> bool: + if self.error is None: + return False + return isinstance(self.error, _get_connection_error_types()) + + +class RequestsHTTPTransport(BaseHTTPTransport): + def __init__( + self, + *, + verify: bool | str = True, + cert: str | tuple[str, str] | None = None, + session: requests.Session | None = None, + ) -> None: + # pylint: disable-next=import-outside-toplevel + import requests # noqa: PLC0415 + + self._session = session if session is not None else requests.Session() + self._session.verify = verify + if cert is not None: + self._session.cert = cert + + if verify is False: + # pylint: disable-next=import-outside-toplevel + from urllib3.exceptions import ( # noqa: PLC0415 + InsecureRequestWarning, + ) + + warnings.filterwarnings("ignore", category=InsecureRequestWarning) + + def request( + self, + method: str, + url: str, + *, + headers: dict[str, str] | None = None, + timeout: float | None = None, + data: bytes | None = None, + ) -> BaseHTTPResult: + try: + response = self._session.request( + method=method, + url=url, + headers=headers, + data=data, + timeout=timeout, + allow_redirects=False, + ) + # pylint: disable-next=broad-exception-caught + except Exception as error: + return RequestsHTTPResult(error=error) + + return RequestsHTTPResult( + status_code=response.status_code, + reason=response.reason, + ) + + def close(self) -> None: + self._session.close() diff --git a/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_urllib3.py b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_urllib3.py new file mode 100644 index 0000000000..a7292124b0 --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/_urllib3.py @@ -0,0 +1,109 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +import functools +import warnings +from dataclasses import dataclass + +from opentelemetry.exporter.http.transport._base import ( + BaseHTTPResult, + BaseHTTPTransport, +) + + +@functools.cache +def _get_connection_error_types() -> tuple[type[Exception], ...]: + # pylint: disable-next=import-outside-toplevel + import urllib3.exceptions # noqa: PLC0415 + + types: list[type[Exception]] = [ + urllib3.exceptions.ConnectionError, + urllib3.exceptions.NewConnectionError, + urllib3.exceptions.ConnectTimeoutError, + urllib3.exceptions.MaxRetryError, + urllib3.exceptions.ProtocolError, + ] + + # NameResolutionError was added in urllib3 2.0 + name_resolution_error = getattr( + urllib3.exceptions, "NameResolutionError", None + ) + if name_resolution_error is not None: + types.append(name_resolution_error) + + return tuple(types) + + +@dataclass(frozen=True, slots=True) +class Urllib3HTTPResult(BaseHTTPResult): + def is_connection_error(self) -> bool: + if self.error is None: + return False + return isinstance(self.error, _get_connection_error_types()) + + +class Urllib3HTTPTransport(BaseHTTPTransport): + def __init__( + self, + *, + verify: bool | str = True, + cert: str | tuple[str, str] | None = None, + ) -> None: + # pylint: disable-next=import-outside-toplevel + import urllib3 # noqa: PLC0415 + + pool_kwargs: dict[str, object] = { + "retries": urllib3.Retry(0, redirect=False), + } + if verify is False: + pool_kwargs["cert_reqs"] = "CERT_NONE" + warnings.filterwarnings( + "ignore", + category=urllib3.exceptions.InsecureRequestWarning, + ) + else: + pool_kwargs["cert_reqs"] = "CERT_REQUIRED" + if isinstance(verify, str): + pool_kwargs["ca_certs"] = verify + if isinstance(cert, tuple): + pool_kwargs["cert_file"] = cert[0] + pool_kwargs["key_file"] = cert[1] + elif isinstance(cert, str): + pool_kwargs["cert_file"] = cert + + self._pool = urllib3.PoolManager(**pool_kwargs) # type: ignore + + def request( + self, + method: str, + url: str, + *, + headers: dict[str, str] | None = None, + timeout: float | None = None, + data: bytes | None = None, + ) -> BaseHTTPResult: + # pylint: disable-next=import-outside-toplevel + import urllib3 # noqa: PLC0415 + + try: + response = self._pool.request( + method=method, + url=url, + headers=headers, + body=data, + timeout=urllib3.Timeout(total=timeout) + if timeout is not None + else None, + preload_content=True, + ) + # pylint: disable-next=broad-exception-caught + except Exception as error: + return Urllib3HTTPResult(error=error) + + return Urllib3HTTPResult( + status_code=response.status, + reason=response.reason, + ) + + def close(self) -> None: + self._pool.clear() diff --git a/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/version/__init__.py b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/version/__init__.py new file mode 100644 index 0000000000..716ad67f7d --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/src/opentelemetry/exporter/http/transport/version/__init__.py @@ -0,0 +1,4 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +__version__ = "0.63b0.dev" diff --git a/exporter/opentelemetry-exporter-http-transport/test-requirements.txt b/exporter/opentelemetry-exporter-http-transport/test-requirements.txt new file mode 100644 index 0000000000..2d29f0f982 --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/test-requirements.txt @@ -0,0 +1,16 @@ +asgiref==3.7.2 +importlib-metadata==6.11.0 +iniconfig==2.0.0 +packaging==24.0 +pluggy==1.6.0 +protobuf==6.31.1 +py-cpuinfo==9.0.0 +pytest==7.4.4 +tomli==2.0.1 +typing_extensions==4.12.0 +wrapt==1.16.0 +zipp==3.19.2 +pook==2.1.6 +requests==2.32.3 +urllib3==2.2.2 +-e exporter/opentelemetry-exporter-http-transport[requests,urllib3] diff --git a/exporter/opentelemetry-exporter-http-transport/tests/__init__.py b/exporter/opentelemetry-exporter-http-transport/tests/__init__.py new file mode 100644 index 0000000000..e57cf4aba9 --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/tests/__init__.py @@ -0,0 +1,2 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 diff --git a/exporter/opentelemetry-exporter-http-transport/tests/test_otlp_client.py b/exporter/opentelemetry-exporter-http-transport/tests/test_otlp_client.py new file mode 100644 index 0000000000..7e9b6cc2c6 --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/tests/test_otlp_client.py @@ -0,0 +1,390 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +import gzip +import threading +import unittest +import zlib +from collections.abc import Callable, Iterator +from contextlib import contextmanager +from dataclasses import dataclass +from unittest.mock import Mock, patch + +from opentelemetry.exporter.http.transport._base import ( + BaseHTTPResult, + BaseHTTPTransport, +) +from opentelemetry.exporter.http.transport._otlp_client import ( + Compression, + OTLPHTTPClient, +) + + +@contextmanager +def _mock_clock( + shutdown_event: Mock | None = None, +) -> Iterator[Callable[[float], None]]: + _now = [0.0] + + def advance(delta: float) -> None: + _now[0] += delta + + def get_time() -> float: + return _now[0] + + if shutdown_event is not None: + + def _wait(duration: float) -> bool: + advance(duration) + return False + + shutdown_event.wait.side_effect = _wait + + with patch( + "opentelemetry.exporter.http.transport._otlp_client.time.time", + side_effect=get_time, + ): + yield advance + + +@dataclass(frozen=True, slots=True) +class _TestHTTPResult(BaseHTTPResult): + connection_error: bool = False + + def is_connection_error(self) -> bool: + return self.connection_error + + +class _TestHTTPTransport(BaseHTTPTransport): + def __init__(self, *results): + self.results = list(results) + self.requests = [] + self.closed = False + + def request( + self, + method, + url, + *, + headers=None, + timeout=None, + data=None, + ): + self.requests.append( + { + "method": method, + "url": url, + "headers": headers, + "timeout": timeout, + "data": data, + } + ) + result = self.results.pop(0) + if callable(result): + result = result() + if isinstance(result, Exception): + raise result + return result + + def close(self): + self.closed = True + + +class TestOTLPHTTPClient(unittest.TestCase): + @staticmethod + def _client( + transport, + *, + timeout=5.0, + compression=Compression.NONE, + shutdown_event=None, + jitter=0.0, + ): + return OTLPHTTPClient( + transport=transport, + endpoint="http://example.test/v1/traces", + timeout=timeout, + compression=compression, + shutdown_event=shutdown_event or threading.Event(), + headers={"content-type": "application/x-protobuf"}, + kind="spans", + jitter=jitter, + ) + + def test_export_success_status_codes(self): + cases = ( + (200, "OK"), + (204, "No Content"), + (302, "Found"), + ) + + for status_code, reason in cases: + with self.subTest(status_code=status_code): + transport = _TestHTTPTransport( + _TestHTTPResult(status_code=status_code, reason=reason) + ) + client = self._client(transport) + + result = client.export(b"payload") + + self.assertTrue(result.success) + self.assertEqual(result.status_code, status_code) + self.assertEqual(result.reason, reason) + self.assertIsNone(result.error) + + @patch( + "opentelemetry.exporter.http.transport._otlp_client.time.time", + side_effect=(100.0, 100.0, 100.0), + ) + def test_export_request_arguments(self, mock_time): + transport = _TestHTTPTransport( + _TestHTTPResult(status_code=200, reason="OK") + ) + client = self._client(transport, timeout=3.0) + + client.export(b"payload") + + self.assertEqual(len(transport.requests), 1) + self.assertEqual( + transport.requests[0], + { + "method": "POST", + "url": "http://example.test/v1/traces", + "headers": {"content-type": "application/x-protobuf"}, + "timeout": 3.0, + "data": b"payload", + }, + ) + self.assertEqual(mock_time.call_count, 3) + + def test_export_compresses_payload(self): + cases = ( + ( + Compression.NONE, + lambda data: data, + ), + ( + Compression.GZIP, + gzip.decompress, + ), + ( + Compression.DEFLATE, + zlib.decompress, + ), + ) + + for compression, decompress in cases: + with self.subTest(compression=compression): + transport = _TestHTTPTransport( + _TestHTTPResult(status_code=200, reason="OK") + ) + client = self._client(transport, compression=compression) + + result = client.export(b"payload") + + self.assertTrue(result.success) + self.assertEqual( + decompress(transport.requests[0]["data"]), b"payload" + ) + + def test_export_retryable_status_codes(self): + cases = ( + (408, "Request Timeout"), + (500, "Internal Server Error"), + (503, "Service Unavailable"), + ) + + for status_code, reason in cases: + with self.subTest(status_code=status_code): + shutdown_event = Mock(spec=threading.Event) + shutdown_event.is_set.return_value = False + shutdown_event.wait.return_value = False + transport = _TestHTTPTransport( + _TestHTTPResult( + status_code=status_code, + reason=reason, + ), + _TestHTTPResult(status_code=200, reason="OK"), + ) + client = self._client( + transport, + shutdown_event=shutdown_event, + ) + + result = client.export(b"payload") + + self.assertTrue(result.success) + self.assertEqual(len(transport.requests), 2) + shutdown_event.wait.assert_called_once_with(1.0) + + def test_export_connection_errors(self): + error = RuntimeError("connection failed") + transport = _TestHTTPTransport( + _TestHTTPResult(error=error, connection_error=True), + _TestHTTPResult(status_code=200, reason="OK"), + ) + client = self._client(transport) + + result = client.export(b"payload") + + self.assertTrue(result.success) + self.assertEqual(len(transport.requests), 2) + self.assertAlmostEqual(transport.requests[0]["timeout"], 5.0, 2) + self.assertLessEqual( + transport.requests[1]["timeout"], + transport.requests[0]["timeout"], + ) + self.assertGreater(transport.requests[1]["timeout"], 0.0) + + def test_export_non_retryable_errors(self): + exception = RuntimeError("request failed") + cases = ( + ( + _TestHTTPResult(status_code=400, reason="Bad Request"), + 400, + "Bad Request", + None, + ), + ( + _TestHTTPResult(error=exception), + None, + None, + exception, + ), + ( + exception, + None, + None, + exception, + ), + ) + + for ( + response, + expected_status_code, + expected_reason, + expected_error, + ) in cases: + with self.subTest(response=type(response).__name__): + transport = _TestHTTPTransport(response) + client = self._client(transport) + + result = client.export(b"payload") + + self.assertFalse(result.success) + self.assertEqual(result.status_code, expected_status_code) + self.assertEqual(result.reason, expected_reason) + self.assertIs(result.error, expected_error) + + def test_export_with_shutdown(self): + shutdown_event = Mock(spec=threading.Event) + shutdown_event.is_set.return_value = True + transport = _TestHTTPTransport( + _TestHTTPResult(status_code=503, reason="Service Unavailable") + ) + client = self._client(transport, shutdown_event=shutdown_event) + + result = client.export(b"payload") + + self.assertFalse(result.success) + self.assertEqual(result.status_code, 503) + self.assertEqual(result.reason, "Service Unavailable") + shutdown_event.wait.assert_not_called() + + def test_close_closes_transport(self): + transport = _TestHTTPTransport() + client = self._client(transport) + + client.close() + + self.assertTrue(transport.closed) + + def test_export_timeout_decreases_per_retry(self): + shutdown_event = Mock(spec=threading.Event) + shutdown_event.is_set.return_value = False + transport = _TestHTTPTransport( + _TestHTTPResult(status_code=503, reason="Service Unavailable"), + _TestHTTPResult(status_code=503, reason="Service Unavailable"), + _TestHTTPResult(status_code=200, reason="OK"), + ) + client = self._client( + transport, timeout=10.0, jitter=0.0, shutdown_event=shutdown_event + ) + + with _mock_clock(shutdown_event): + result = client.export(b"payload") + + # retry=0: wait(1.0) -> time=1.0, retry=1: wait(2.0) -> time=3.0, success + self.assertTrue(result.success) + self.assertAlmostEqual(transport.requests[0]["timeout"], 10.0) + self.assertAlmostEqual(transport.requests[1]["timeout"], 9.0) + self.assertAlmostEqual(transport.requests[2]["timeout"], 7.0) + + def test_export_backoff_exhausts_remaining_timeout(self): + shutdown_event = Mock(spec=threading.Event) + shutdown_event.is_set.return_value = False + transport = _TestHTTPTransport( + _TestHTTPResult(status_code=503, reason="Service Unavailable"), + _TestHTTPResult(status_code=503, reason="Service Unavailable"), + ) + # timeout=1.5: retry=0 backoff=1.0 fits -> wait(1.0) -> time=1.0 + # retry=1 backoff=2.0 > 0.5 remaining -> give up + client = self._client( + transport, timeout=1.5, jitter=0.0, shutdown_event=shutdown_event + ) + + with _mock_clock(shutdown_event): + result = client.export(b"payload") + + self.assertFalse(result.success) + self.assertEqual(result.status_code, 503) + self.assertEqual(len(transport.requests), 2) + shutdown_event.wait.assert_called_once_with(1.0) + + def test_export_exhausts_max_retries(self): + shutdown_event = Mock(spec=threading.Event) + shutdown_event.is_set.return_value = False + transport = _TestHTTPTransport( + *[_TestHTTPResult(status_code=503, reason="Service Unavailable")] + * 6 + ) + client = self._client( + transport, + timeout=1000.0, + jitter=0.0, + shutdown_event=shutdown_event, + ) + + with _mock_clock(shutdown_event): + result = client.export(b"payload") + + self.assertFalse(result.success) + self.assertEqual(len(transport.requests), 6) + self.assertEqual(shutdown_event.wait.call_count, 5) + self.assertEqual( + [call.args[0] for call in shutdown_event.wait.call_args_list], + [1.0, 2.0, 4.0, 8.0, 16.0], + ) + + def test_export_connection_error_gets_reduced_timeout(self): + transport = _TestHTTPTransport( + _TestHTTPResult(status_code=200, reason="OK"), + ) + + with _mock_clock() as advance: + + def _slow_connection_error() -> _TestHTTPResult: + advance(2.0) + return _TestHTTPResult( + error=RuntimeError("stale connection"), + connection_error=True, + ) + + transport.results.insert(0, _slow_connection_error) + client = self._client(transport, timeout=5.0) + result = client.export(b"payload") + + # _submit: deadline=0+5=5.0, after first request time=2.0, remaining=3.0 + self.assertTrue(result.success) + self.assertEqual(len(transport.requests), 2) + self.assertAlmostEqual(transport.requests[1]["timeout"], 3.0) diff --git a/exporter/opentelemetry-exporter-http-transport/tests/test_requests_transport.py b/exporter/opentelemetry-exporter-http-transport/tests/test_requests_transport.py new file mode 100644 index 0000000000..8ec7fc5743 --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/tests/test_requests_transport.py @@ -0,0 +1,185 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +from __future__ import annotations + +import unittest +from unittest.mock import MagicMock, patch + +import pook +import requests +import requests.exceptions + +from opentelemetry.exporter.http.transport._requests import ( + RequestsHTTPResult, + RequestsHTTPTransport, +) + +_TEST_URL = "http://example.test/v1/traces" + + +class TestRequestsHTTPResult(unittest.TestCase): + def test_is_connection_error(self): + cases = [ + (RequestsHTTPResult(status_code=200, reason="OK"), False), + ( + RequestsHTTPResult( + error=requests.exceptions.ConnectionError("error") + ), + True, + ), + ( + RequestsHTTPResult( + error=requests.exceptions.ConnectTimeout("error") + ), + True, + ), + ( + RequestsHTTPResult( + error=requests.exceptions.ReadTimeout("error") + ), + True, + ), + ( + RequestsHTTPResult(error=requests.exceptions.Timeout("error")), + True, + ), + ( + RequestsHTTPResult( + error=requests.exceptions.SSLError("error") + ), + True, + ), + ( + RequestsHTTPResult( + error=requests.exceptions.ProxyError("error") + ), + True, + ), + ( + RequestsHTTPResult( + error=requests.exceptions.HTTPError("error") + ), + False, + ), + ( + RequestsHTTPResult( + error=requests.exceptions.RequestException("error") + ), + False, + ), + (RequestsHTTPResult(error=RuntimeError("error")), False), + (RequestsHTTPResult(error=ValueError("error")), False), + ] + for result, expected in cases: + with self.subTest(error_type=type(result.error).__name__): + self.assertEqual(result.is_connection_error(), expected) + + +# pylint: disable=protected-access,no-self-use +class TestRequestsHTTPTransport(unittest.TestCase): + @pook.on + def test_request_returns_status_code_and_reason(self): + cases = [ + (200, "OK"), + (400, "Bad Request"), + (503, "Service Unavailable"), + ] + for status_code, reason in cases: + with self.subTest(status_code=status_code): + pook.post(_TEST_URL).reply(status_code) + transport = RequestsHTTPTransport() + result = transport.request("POST", _TEST_URL) + self.assertEqual(result.status_code, status_code) + self.assertEqual(result.reason, reason) + self.assertIsNone(result.error) + pook.reset() + + @pook.on + def test_request_result_is_not_a_connection_error(self): + pook.post(_TEST_URL).reply(200) + transport = RequestsHTTPTransport() + result = transport.request("POST", _TEST_URL) + self.assertFalse(result.is_connection_error()) + + @pook.on + def test_request_forwards_headers(self): + headers = { + "content-type": "application/x-protobuf", + "x-custom": "value", + } + pook.post(_TEST_URL, headers=headers).reply(200) + transport = RequestsHTTPTransport() + result = transport.request("POST", _TEST_URL, headers=headers) + self.assertEqual(result.status_code, 200) + self.assertTrue(pook.isdone()) + + @pook.on + def test_request_forwards_data(self): + pook.post(_TEST_URL, body=b"payload").reply(200) + transport = RequestsHTTPTransport() + result = transport.request("POST", _TEST_URL, data=b"payload") + self.assertEqual(result.status_code, 200) + self.assertTrue(pook.isdone()) + + def test_request_catches_exception(self): + cases = [ + (RuntimeError("unexpected"), False), + (requests.exceptions.ConnectionError("failed"), True), + ] + for error, expected_is_connection_error in cases: + with self.subTest(error_type=type(error).__name__): + with patch("requests.Session.request", side_effect=error): + transport = RequestsHTTPTransport() + result = transport.request("POST", _TEST_URL) + self.assertIsNone(result.status_code) + self.assertIsNone(result.reason) + self.assertIs(result.error, error) + self.assertEqual( + result.is_connection_error(), expected_is_connection_error + ) + + def test_verify_sets_session_verify(self): + cases = [ + (True, True), + (False, False), + ("/path/to/ca.pem", "/path/to/ca.pem"), + ] + for verify, expected in cases: + with self.subTest(verify=verify): + mock_session = MagicMock(spec=requests.Session) + RequestsHTTPTransport(verify=verify, session=mock_session) + self.assertEqual(mock_session.verify, expected) + + def test_cert_none_does_not_set_session_cert(self): + mock_session = MagicMock(spec=requests.Session) + RequestsHTTPTransport(cert=None, session=mock_session) + self.assertFalse(hasattr(mock_session, "cert")) + + def test_cert_sets_session_cert(self): + cases = [ + "/path/to/cert.pem", + ("/path/to/cert.pem", "/path/to/key.pem"), + ] + for cert in cases: + with self.subTest(cert=cert): + mock_session = MagicMock(spec=requests.Session) + RequestsHTTPTransport(cert=cert, session=mock_session) + self.assertEqual(mock_session.cert, cert) + + def test_custom_session_is_used(self): + mock_session = MagicMock(spec=requests.Session) + mock_session.request.return_value = MagicMock( + status_code=200, reason="OK" + ) + transport = RequestsHTTPTransport(session=mock_session) + result = transport.request("POST", _TEST_URL) + mock_session.request.assert_called_once() + self.assertEqual(result.status_code, 200) + self.assertEqual(result.reason, "OK") + + def test_close_closes_session(self): + mock_session = MagicMock(spec=requests.Session) + transport = RequestsHTTPTransport(session=mock_session) + transport.close() + mock_session.close.assert_called_once() diff --git a/exporter/opentelemetry-exporter-http-transport/tests/test_urllib3_transport.py b/exporter/opentelemetry-exporter-http-transport/tests/test_urllib3_transport.py new file mode 100644 index 0000000000..ed507ae202 --- /dev/null +++ b/exporter/opentelemetry-exporter-http-transport/tests/test_urllib3_transport.py @@ -0,0 +1,220 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +from __future__ import annotations + +import unittest +from unittest.mock import MagicMock, patch + +import pook +import urllib3 +import urllib3.exceptions + +from opentelemetry.exporter.http.transport._urllib3 import ( + Urllib3HTTPResult, + Urllib3HTTPTransport, +) + +_TEST_URL = "http://example.test/v1/traces" + + +class TestUrllib3HTTPResult(unittest.TestCase): + def test_is_connection_error(self): + cases: list[tuple[Urllib3HTTPResult, bool]] = [ + (Urllib3HTTPResult(status_code=200, reason="OK"), False), + ( + Urllib3HTTPResult( + error=urllib3.exceptions.ProtocolError("error") + ), + True, + ), + ( + Urllib3HTTPResult( + error=urllib3.exceptions.NewConnectionError(None, "error") + ), + True, + ), + ( + Urllib3HTTPResult( + error=urllib3.exceptions.ConnectTimeoutError(None, "error") + ), + True, + ), + ( + Urllib3HTTPResult( + error=urllib3.exceptions.MaxRetryError(None, "http://x") + ), + True, + ), + ( + Urllib3HTTPResult(error=urllib3.exceptions.HTTPError("error")), + False, + ), + ( + Urllib3HTTPResult( + error=urllib3.exceptions.ReadTimeoutError( + None, "http://x", "timeout" + ) + ), + False, + ), + (Urllib3HTTPResult(error=RuntimeError("error")), False), + (Urllib3HTTPResult(error=ValueError("error")), False), + ] + name_resolution_error = getattr( + urllib3.exceptions, "NameResolutionError", None + ) + if name_resolution_error is not None: + cases.append( + ( + Urllib3HTTPResult( + error=name_resolution_error("host", None, "error") + ), + True, + ) + ) + for result, expected in cases: + with self.subTest(error_type=type(result.error).__name__): + self.assertEqual(result.is_connection_error(), expected) + + +# pylint: disable=protected-access,no-self-use +class TestUrllib3HTTPTransport(unittest.TestCase): + @pook.on + def test_request_returns_status_code_and_reason(self): + cases = [ + (200, "OK"), + (400, "Bad Request"), + (503, "Service Unavailable"), + ] + for status_code, reason in cases: + with self.subTest(status_code=status_code): + pook.post(_TEST_URL).reply(status_code) + transport = Urllib3HTTPTransport() + result = transport.request("POST", _TEST_URL) + self.assertEqual(result.status_code, status_code) + self.assertEqual(result.reason, reason) + self.assertIsNone(result.error) + pook.reset() + + @pook.on + def test_request_result_is_not_a_connection_error(self): + pook.post(_TEST_URL).reply(200) + transport = Urllib3HTTPTransport() + result = transport.request("POST", _TEST_URL) + self.assertFalse(result.is_connection_error()) + + @pook.on + def test_request_forwards_headers(self): + headers = { + "content-type": "application/x-protobuf", + "x-custom": "value", + } + pook.post(_TEST_URL, headers=headers).reply(200) + transport = Urllib3HTTPTransport() + result = transport.request("POST", _TEST_URL, headers=headers) + self.assertEqual(result.status_code, 200) + self.assertTrue(pook.isdone()) + + @pook.on + def test_request_forwards_data(self): + pook.post(_TEST_URL, body=b"payload").reply(200) + transport = Urllib3HTTPTransport() + result = transport.request("POST", _TEST_URL, data=b"payload") + self.assertEqual(result.status_code, 200) + self.assertTrue(pook.isdone()) + + def test_request_catches_exception(self): + cases = [ + (RuntimeError("unexpected"), False), + (urllib3.exceptions.ProtocolError("failed"), True), + ] + for error, expected_is_connection_error in cases: + with self.subTest(error_type=type(error).__name__): + transport = Urllib3HTTPTransport() + with patch.object( + transport._pool, "request", side_effect=error + ): + result = transport.request("POST", _TEST_URL) + self.assertIsNone(result.status_code) + self.assertIsNone(result.reason) + self.assertIs(result.error, error) + self.assertEqual( + result.is_connection_error(), expected_is_connection_error + ) + + def test_request_passes_timeout(self): + cases = [ + (3.5,), + (None,), + ] + for (timeout,) in cases: + with self.subTest(timeout=timeout): + transport = Urllib3HTTPTransport() + with patch.object(transport._pool, "request") as mock_request: + mock_request.return_value = MagicMock( + status=200, reason="OK" + ) + transport.request("POST", _TEST_URL, timeout=timeout) + timeout_kwarg = mock_request.call_args.kwargs["timeout"] + if timeout is not None: + self.assertIsInstance(timeout_kwarg, urllib3.Timeout) + self.assertEqual(timeout_kwarg.total, timeout) + else: + self.assertIsNone(timeout_kwarg) + + def test_verify_sets_pool_manager_kwargs(self): + cases = [ + (True, "CERT_REQUIRED", None), + (False, "CERT_NONE", None), + ("/path/to/ca.pem", "CERT_REQUIRED", "/path/to/ca.pem"), + ] + for verify, expected_cert_reqs, expected_ca_certs in cases: + with self.subTest(verify=verify): + with patch("urllib3.PoolManager") as mock_pm: + Urllib3HTTPTransport(verify=verify) + kwargs = mock_pm.call_args.kwargs + self.assertEqual(kwargs["cert_reqs"], expected_cert_reqs) + if expected_ca_certs is not None: + self.assertEqual(kwargs["ca_certs"], expected_ca_certs) + else: + self.assertNotIn("ca_certs", kwargs) + + def test_cert_none_does_not_set_cert_file(self): + with patch("urllib3.PoolManager") as mock_pm: + Urllib3HTTPTransport(cert=None) + self.assertNotIn("cert_file", mock_pm.call_args.kwargs) + + def test_cert_sets_pool_manager_kwargs(self): + cases = [ + ("/path/to/cert.pem", "/path/to/cert.pem", None), + ( + ("/path/to/cert.pem", "/path/to/key.pem"), + "/path/to/cert.pem", + "/path/to/key.pem", + ), + ] + for cert, expected_cert_file, expected_key_file in cases: + with self.subTest(cert=cert): + with patch("urllib3.PoolManager") as mock_pm: + Urllib3HTTPTransport(cert=cert) + kwargs = mock_pm.call_args.kwargs + self.assertEqual(kwargs["cert_file"], expected_cert_file) + if expected_key_file is not None: + self.assertEqual(kwargs["key_file"], expected_key_file) + else: + self.assertNotIn("key_file", kwargs) + + def test_retries_disabled(self): + with patch("urllib3.PoolManager") as mock_pm: + Urllib3HTTPTransport() + retries = mock_pm.call_args.kwargs["retries"] + self.assertIsInstance(retries, urllib3.Retry) + self.assertEqual(retries.total, 0) + self.assertFalse(retries.redirect) + + def test_close_clears_pool(self): + with patch("urllib3.PoolManager") as mock_pm: + transport = Urllib3HTTPTransport() + transport.close() + mock_pm.return_value.clear.assert_called_once() diff --git a/pyproject.toml b/pyproject.toml index 83b489aa94..a92252f876 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ dependencies = [ "opentelemetry-proto", "opentelemetry-proto-json", "opentelemetry-test-utils", + "opentelemetry-exporter-http-transport", "opentelemetry-exporter-otlp-proto-grpc", "opentelemetry-exporter-otlp-proto-http", "opentelemetry-exporter-otlp-proto-common", @@ -32,6 +33,7 @@ opentelemetry-proto = { workspace = true } opentelemetry-proto-json = { workspace = true } opentelemetry-semantic-conventions = { workspace = true } opentelemetry-test-utils = { workspace = true } +opentelemetry-exporter-http-transport = { workspace = true } opentelemetry-exporter-otlp-proto-grpc = { workspace = true } opentelemetry-exporter-otlp-proto-http = { workspace = true } opentelemetry-exporter-otlp-proto-common = { workspace = true } @@ -119,6 +121,7 @@ include = [ "opentelemetry-api", "opentelemetry-sdk", "opentelemetry-proto-json", + "exporter/opentelemetry-exporter-http-transport", "exporter/opentelemetry-exporter-otlp-proto-grpc", "exporter/opentelemetry-exporter-otlp-proto-http", "exporter/opentelemetry-exporter-otlp-json-common", @@ -137,6 +140,7 @@ exclude = [ "opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/metric_reader_storage.py", "opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py", "opentelemetry-sdk/benchmarks", + "exporter/opentelemetry-exporter-http-transport/tests", "exporter/opentelemetry-exporter-otlp-proto-grpc/tests", "exporter/opentelemetry-exporter-otlp-proto-http/tests", "exporter/opentelemetry-exporter-otlp-json-common/tests", diff --git a/tox.ini b/tox.ini index 22bdbbd77d..c3d30a03a9 100644 --- a/tox.ini +++ b/tox.ini @@ -44,6 +44,10 @@ envlist = ; opencensus-shim intentionally excluded from pypy3 (grpcio install fails) lint-opentelemetry-opencensus-shim + py3{10,11,12,13,14,14t}-test-opentelemetry-exporter-http-transport + ; exporter-http-transport intentionally excluded from pypy3 + lint-opentelemetry-exporter-http-transport + py3{10,11,12,13,14}-test-opentelemetry-exporter-opencensus ; exporter-opencensus intentionally excluded from pypy3 lint-opentelemetry-exporter-opencensus @@ -136,6 +140,8 @@ deps = opentelemetry-protojson-gen-oldest: -r {toxinidir}/opentelemetry-proto-json/test-requirements.oldest.txt opentelemetry-protojson-gen-latest: -r {toxinidir}/opentelemetry-proto-json/test-requirements.latest.txt + opentelemetry-exporter-http-transport: -r {toxinidir}/exporter/opentelemetry-exporter-http-transport/test-requirements.txt + exporter-opencensus: -r {toxinidir}/exporter/opentelemetry-exporter-opencensus/test-requirements.txt exporter-otlp-proto-common: -r {toxinidir}/exporter/opentelemetry-exporter-otlp-proto-common/test-requirements.txt @@ -230,6 +236,9 @@ commands = test-opentelemetry-opencensus-shim: pytest {toxinidir}/shim/opentelemetry-opencensus-shim/tests {posargs} lint-opentelemetry-opencensus-shim: sh -c "cd shim && pylint --rcfile ../.pylintrc {toxinidir}/shim/opentelemetry-opencensus-shim" + test-opentelemetry-exporter-http-transport: pytest {toxinidir}/exporter/opentelemetry-exporter-http-transport/tests {posargs} + lint-opentelemetry-exporter-http-transport: sh -c "cd exporter && pylint --rcfile ../.pylintrc {toxinidir}/exporter/opentelemetry-exporter-http-transport" + test-opentelemetry-exporter-opencensus: pytest {toxinidir}/exporter/opentelemetry-exporter-opencensus/tests {posargs} lint-opentelemetry-exporter-opencensus: sh -c "cd exporter && pylint --rcfile ../.pylintrc {toxinidir}/exporter/opentelemetry-exporter-opencensus" @@ -402,6 +411,7 @@ deps = -e {toxinidir}/opentelemetry-semantic-conventions -e {toxinidir}/opentelemetry-sdk[file-configuration] -e {toxinidir}/tests/opentelemetry-test-utils + -e {toxinidir}/exporter/opentelemetry-exporter-http-transport -e {toxinidir}/exporter/opentelemetry-exporter-otlp-proto-common -e {toxinidir}/exporter/opentelemetry-exporter-otlp-json-common -e {toxinidir}/exporter/opentelemetry-exporter-otlp diff --git a/uv.lock b/uv.lock index e3402bd70b..ec20b7b2f5 100644 --- a/uv.lock +++ b/uv.lock @@ -11,6 +11,7 @@ resolution-markers = [ members = [ "opentelemetry-api", "opentelemetry-codegen-json", + "opentelemetry-exporter-http-transport", "opentelemetry-exporter-otlp", "opentelemetry-exporter-otlp-json-common", "opentelemetry-exporter-otlp-proto-common", @@ -856,6 +857,25 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/45/a4/d59629684cef86a9da39241edd25dc2dff849d08def6200d50c951e1dd2a/opentelemetry_exporter_credential_provider_gcp-0.62b0-py3-none-any.whl", hash = "sha256:56d15d6486c40d9f958f34b1e9b4b6c9122789f5d136fc1381948b7b4b3af3ca", size = 8343, upload-time = "2026-04-09T14:39:18.399Z" }, ] +[[package]] +name = "opentelemetry-exporter-http-transport" +source = { editable = "exporter/opentelemetry-exporter-http-transport" } + +[package.optional-dependencies] +requests = [ + { name = "requests" }, +] +urllib3 = [ + { name = "urllib3" }, +] + +[package.metadata] +requires-dist = [ + { name = "requests", marker = "extra == 'requests'", specifier = "~=2.7" }, + { name = "urllib3", marker = "extra == 'urllib3'", specifier = ">=1.11" }, +] +provides-extras = ["requests", "urllib3"] + [[package]] name = "opentelemetry-exporter-otlp" source = { editable = "exporter/opentelemetry-exporter-otlp" } @@ -1035,6 +1055,7 @@ source = { virtual = "." } dependencies = [ { name = "opentelemetry-api" }, { name = "opentelemetry-codegen-json" }, + { name = "opentelemetry-exporter-http-transport" }, { name = "opentelemetry-exporter-otlp-json-common" }, { name = "opentelemetry-exporter-otlp-proto-common" }, { name = "opentelemetry-exporter-otlp-proto-grpc" }, @@ -1062,6 +1083,7 @@ dev = [ requires-dist = [ { name = "opentelemetry-api", editable = "opentelemetry-api" }, { name = "opentelemetry-codegen-json", editable = "codegen/opentelemetry-codegen-json" }, + { name = "opentelemetry-exporter-http-transport", editable = "exporter/opentelemetry-exporter-http-transport" }, { name = "opentelemetry-exporter-otlp-json-common", editable = "exporter/opentelemetry-exporter-otlp-json-common" }, { name = "opentelemetry-exporter-otlp-proto-common", editable = "exporter/opentelemetry-exporter-otlp-proto-common" }, { name = "opentelemetry-exporter-otlp-proto-grpc", editable = "exporter/opentelemetry-exporter-otlp-proto-grpc" },