diff --git a/.github/workflows/core_contrib_test_0.yml b/.github/workflows/core_contrib_test_0.yml index 40fc729620..4540e6bf15 100644 --- a/.github/workflows/core_contrib_test_0.yml +++ b/.github/workflows/core_contrib_test_0.yml @@ -2273,6 +2273,36 @@ jobs: - name: Run tests run: tox -e py39-test-instrumentation-system-metrics -- -ra + py39-test-instrumentation-taskiq: + name: instrumentation-taskiq + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout contrib repo @ SHA - ${{ env.CONTRIB_REPO_SHA }} + uses: actions/checkout@v4 + with: + repository: open-telemetry/opentelemetry-python-contrib + ref: ${{ env.CONTRIB_REPO_SHA }} + + - name: Checkout core repo @ SHA - ${{ env.CORE_REPO_SHA }} + uses: actions/checkout@v4 + with: + repository: open-telemetry/opentelemetry-python + ref: ${{ env.CORE_REPO_SHA }} + path: opentelemetry-python + + - name: Set up Python 3.9 + uses: actions/setup-python@v5 + with: + python-version: "3.9" + architecture: "x64" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py39-test-instrumentation-taskiq -- -ra + py39-test-instrumentation-threading: name: instrumentation-threading runs-on: ubuntu-latest diff --git a/.github/workflows/lint_0.yml b/.github/workflows/lint_0.yml index 309705ae5a..66f2e25fb6 100644 --- a/.github/workflows/lint_0.yml +++ b/.github/workflows/lint_0.yml @@ -982,6 +982,25 @@ jobs: - name: Run tests run: tox -e lint-instrumentation-system-metrics + lint-instrumentation-taskiq: + name: instrumentation-taskiq + 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 lint-instrumentation-taskiq + lint-instrumentation-threading: name: instrumentation-threading runs-on: ubuntu-latest diff --git a/.github/workflows/test_1.yml b/.github/workflows/test_1.yml index e60b3c530b..d3d510bbc4 100644 --- a/.github/workflows/test_1.yml +++ b/.github/workflows/test_1.yml @@ -3300,6 +3300,120 @@ jobs: - name: Run tests run: tox -e pypy3-test-instrumentation-system-metrics -- -ra + py39-test-instrumentation-taskiq_ubuntu-latest: + name: instrumentation-taskiq 3.9 Ubuntu + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.9 + uses: actions/setup-python@v5 + with: + python-version: "3.9" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py39-test-instrumentation-taskiq -- -ra + + py310-test-instrumentation-taskiq_ubuntu-latest: + name: instrumentation-taskiq 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-instrumentation-taskiq -- -ra + + py311-test-instrumentation-taskiq_ubuntu-latest: + name: instrumentation-taskiq 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-instrumentation-taskiq -- -ra + + py312-test-instrumentation-taskiq_ubuntu-latest: + name: instrumentation-taskiq 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-instrumentation-taskiq -- -ra + + py313-test-instrumentation-taskiq_ubuntu-latest: + name: instrumentation-taskiq 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-instrumentation-taskiq -- -ra + + pypy3-test-instrumentation-taskiq_ubuntu-latest: + name: instrumentation-taskiq pypy-3.9 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.9 + uses: actions/setup-python@v5 + with: + python-version: "pypy-3.9" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e pypy3-test-instrumentation-taskiq -- -ra + py39-test-instrumentation-threading_ubuntu-latest: name: instrumentation-threading 3.9 Ubuntu runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index 9061fdeb1a..94423b0b07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- `opentelemetry-instrumentation-taskiq`: New instrumentation for `taskiq` + ([#3832](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3832)) +- `opentelemetry-instrumentation`: botocore: Add support for AWS Secrets Manager semantic convention attribute - `opentelemetry-instrumentation-botocore`: Add support for AWS Secrets Manager semantic convention attribute ([#3765](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3765)) - `opentelemetry-instrumentation-dbapi`: Add support for `commenter_options` in `trace_integration` function to control SQLCommenter behavior diff --git a/instrumentation/README.md b/instrumentation/README.md index 1597be99af..3732e6c515 100644 --- a/instrumentation/README.md +++ b/instrumentation/README.md @@ -46,6 +46,7 @@ | [opentelemetry-instrumentation-sqlite3](./opentelemetry-instrumentation-sqlite3) | sqlite3 | No | development | [opentelemetry-instrumentation-starlette](./opentelemetry-instrumentation-starlette) | starlette >= 0.13 | Yes | development | [opentelemetry-instrumentation-system-metrics](./opentelemetry-instrumentation-system-metrics) | psutil >= 5 | No | development +| [opentelemetry-instrumentation-taskiq](./opentelemetry-instrumentation-taskiq) | taskiq >= 0.11.0 | No | development | [opentelemetry-instrumentation-threading](./opentelemetry-instrumentation-threading) | threading | No | development | [opentelemetry-instrumentation-tornado](./opentelemetry-instrumentation-tornado) | tornado >= 5.1.1 | Yes | development | [opentelemetry-instrumentation-tortoiseorm](./opentelemetry-instrumentation-tortoiseorm) | tortoise-orm >= 0.17.0 | No | development diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/LICENSE b/instrumentation/opentelemetry-instrumentation-taskiq/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/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/instrumentation/opentelemetry-instrumentation-taskiq/README.rst b/instrumentation/opentelemetry-instrumentation-taskiq/README.rst new file mode 100644 index 0000000000..63b46c955f --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/README.rst @@ -0,0 +1,24 @@ +OpenTelemetry Taskiq Instrumentation +==================================== + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-taskiq.svg + :target: https://pypi.org/project/opentelemetry-instrumentation-taskiq/ + +Instrumentation for Taskiq. + + +Installation +------------ + +:: + + pip install opentelemetry-instrumentation-taskiq + +References +---------- +* `OpenTelemetry Taskiq Instrumentation `_ +* `OpenTelemetry Project `_ +* `OpenTelemetry Python Examples `_ + diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/pyproject.toml b/instrumentation/opentelemetry-instrumentation-taskiq/pyproject.toml new file mode 100644 index 0000000000..efe0b3ef42 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/pyproject.toml @@ -0,0 +1,55 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "opentelemetry-instrumentation-taskiq" +dynamic = ["version"] +description = "OpenTelemetry Taskiq Instrumentation" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.9" +authors = [ + { name = "OpenTelemetry Authors", email = "cncf-opentelemetry-contributors@lists.cncf.io" }, +] +classifiers = [ + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "License :: OSI Approved :: Apache Software License", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", +] +dependencies = [ + "opentelemetry-api ~= 1.12", + "opentelemetry-instrumentation == 0.59b0.dev", + "opentelemetry-semantic-conventions == 0.59b0.dev", +] + +[project.optional-dependencies] +instruments = [ + "taskiq >= 0.11", +] + +[project.entry-points.opentelemetry_instrumentor] +taskiq = "opentelemetry.instrumentation.taskiq:TaskiqInstrumentor" + +[project.urls] +Homepage = "https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-taskiq" +Repository = "https://github.com/open-telemetry/opentelemetry-python-contrib" + +[tool.hatch.version] +path = "src/opentelemetry/instrumentation/taskiq/version.py" + +[tool.hatch.build.targets.sdist] +include = [ + "/src", + "/tests", +] + +[tool.hatch.build.targets.wheel] +packages = ["src/opentelemetry"] diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/__init__.py b/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/__init__.py new file mode 100644 index 0000000000..317c5455e8 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/__init__.py @@ -0,0 +1,298 @@ +# Copyright The OpenTelemetry Authors +# +# 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. +""" +Instrument `taskiq`_ to trace Taskiq applications. + +.. _taskiq: https://pypi.org/project/taskiq/ + +Usage +----- + +* Run instrumented task + +.. code:: python + + import asyncio + + from opentelemetry.instrumentation.taskiq import TaskiqInstrumentor + from taskiq import InMemoryBroker, TaskiqEvents, TaskiqState + + broker = InMemoryBroker() + + @broker.on_event(TaskiqEvents.WORKER_STARTUP) + async def startup(state: TaskiqState) -> None: + TaskiqInstrumentor().instrument() + + @broker.task + async def add(x, y): + return x + y + + async def main(): + await broker.startup() + await my_task.kiq(1, 2) + await broker.shutdown() + + if __name__ == "__main__": + asyncio.run(main()) + +API +--- +""" + +import logging +from typing import Any, Collection, Optional, TypeVar +from weakref import WeakSet as _WeakSet + +from taskiq import AsyncBroker, TaskiqMessage, TaskiqMiddleware, TaskiqResult +from wrapt import wrap_function_wrapper + +from opentelemetry import context as context_api +from opentelemetry import trace +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.taskiq import utils +from opentelemetry.instrumentation.taskiq.version import __version__ +from opentelemetry.instrumentation.utils import unwrap +from opentelemetry.metrics import Meter, MeterProvider, get_meter +from opentelemetry.propagate import extract, inject +from opentelemetry.semconv.trace import SpanAttributes +from opentelemetry.trace import Tracer, TracerProvider +from opentelemetry.trace.status import Status, StatusCode + +logger = logging.getLogger(__name__) + +T = TypeVar("T") + + +# Task operations +_TASK_TAG_KEY = "taskiq.action" +_TASK_SEND = "send" +_TASK_EXECUTE = "execute" + + +_TASK_RETRY_REASON_KEY = "taskiq.retry.reason" +_TASK_NAME_KEY = "taskiq.task_name" + + +class OpenTelemetryMiddleware(TaskiqMiddleware): + """Middleware to instrument Taskiq with OpenTelemetry.""" + + def __init__( + self, + tracer_provider: Optional[TracerProvider] = None, + meter_provider: Optional[MeterProvider] = None, + tracer: Optional[Tracer] = None, + meter: Optional[Meter] = None, + ): + super().__init__() + self._tracer = ( + trace.get_tracer( + __name__, + __version__, + tracer_provider, + schema_url="https://opentelemetry.io/schemas/1.11.0", + ) + if tracer is None + else tracer + ) + self._meter = ( + get_meter( + __name__, + __version__, + meter_provider, + schema_url="https://opentelemetry.io/schemas/1.11.0", + ) + if meter is None + else meter + ) + + def pre_send(self, message: TaskiqMessage) -> TaskiqMessage: + """Called before sending a task.""" + logger.debug("pre_send task_id=%s", message.task_id) + + operation_name = f"{_TASK_SEND}/{message.task_name}" + span = self._tracer.start_span( + operation_name, kind=trace.SpanKind.PRODUCER + ) + + if span.is_recording(): + span.set_attribute(_TASK_TAG_KEY, _TASK_SEND) + span.set_attribute( + SpanAttributes.MESSAGING_MESSAGE_ID, message.task_id + ) + span.set_attribute(_TASK_NAME_KEY, message.task_name) + utils.set_attributes_from_context(span, message.labels) + + activation = trace.use_span(span, end_on_exit=True) + activation.__enter__() # pylint: disable=E1101 + + utils.attach_context(message, span, activation, None, is_publish=True) + inject(message.labels) + + return message + + def post_send(self, message: TaskiqMessage) -> None: # pylint: disable=R6301 + logger.debug("post_send task_id=%s", message.task_id) + # retrieve and finish the Span + ctx = utils.retrieve_context(message, is_publish=True) + + if ctx is None: + logger.warning( + "no existing span found for task_id=%s", message.task_id + ) + return + + _, activation, _ = ctx + + activation.__exit__(None, None, None) # pylint: disable=E1101 + utils.detach_context(message, is_publish=True) + + def pre_execute(self, message: TaskiqMessage) -> TaskiqMessage: + logger.debug("pre_execute task_id=%s", message.task_id) + tracectx = extract(message.labels) or None + token = context_api.attach(tracectx) if tracectx is not None else None + + operation_name = f"{_TASK_EXECUTE}/{message.task_name}" + span = self._tracer.start_span( + operation_name, context=tracectx, kind=trace.SpanKind.CONSUMER + ) + + activation = trace.use_span(span, end_on_exit=True) + activation.__enter__() # pylint: disable=E1101 + utils.attach_context(message, span, activation, token) + return message + + def post_execute( # pylint: disable=R6301 + self, message: TaskiqMessage, result: TaskiqResult[T] + ) -> None: + logger.debug("post_execute task_id=%s", message.task_id) + + # retrieve and finish the Span + ctx = utils.retrieve_context(message) + + if ctx is None: + logger.warning( + "no existing span found for task_id=%s", message.task_id + ) + return + + span, activation, token = ctx + + if span.is_recording(): + span.set_attribute(_TASK_TAG_KEY, _TASK_EXECUTE) + utils.set_attributes_from_context(span, message.labels) + span.set_attribute(_TASK_NAME_KEY, message.task_name) + + activation.__exit__(None, None, None) + utils.detach_context(message) + # if the process sending the task is not instrumented + # there's no incoming context and no token to detach + if token is not None: + context_api.detach(token) + + def on_error( # pylint: disable=R6301 + self, + message: TaskiqMessage, + result: TaskiqResult[T], + exception: BaseException, + ) -> None: + ctx = utils.retrieve_context(message) + + if ctx is None: + return + + span, _, _ = ctx + + if not span.is_recording(): + return + + retry_on_error = message.labels.get("retry_on_error") + if isinstance(retry_on_error, str): + retry_on_error = retry_on_error.lower() == "true" + + if retry_on_error is None: + retry_on_error = False + + if retry_on_error: + # Add retry reason metadata to span + span.set_attribute(_TASK_RETRY_REASON_KEY, str(exception)) + return + + status_kwargs = { + "status_code": StatusCode.ERROR, + "description": str(exception), + } + span.record_exception(exception) + span.set_status(Status(**status_kwargs)) + + +class TaskiqInstrumentor(BaseInstrumentor): + """OpenTelemetry instrumentor for Taskiq.""" + + _instrumented_brokers: _WeakSet[AsyncBroker] = _WeakSet() + + def __init__(self): + super().__init__() + self._middleware = None + + def instrument_broker( + self, + broker: AsyncBroker, + tracer_provider: Optional[TracerProvider] = None, + meter_provider: Optional[MeterProvider] = None, + ): + if not hasattr(broker, "_is_instrumented_by_opentelemetry"): + broker._is_instrumented_by_opentelemetry = False + + if not getattr(broker, "is_instrumented_by_opentelemetry", False): + broker.middlewares.insert( + 0, + OpenTelemetryMiddleware( + tracer_provider=tracer_provider, + meter_provider=meter_provider, + ), + ) + broker._is_instrumented_by_opentelemetry = True + if broker not in self._instrumented_brokers: + self._instrumented_brokers.add(broker) + else: + logger.warning( + "Attempting to instrument taskiq broker while already instrumented" + ) + + def uninstrument_broker(self, broker: AsyncBroker): + broker.middlewares = [ + middleware + for middleware in broker.middlewares + if not isinstance(middleware, OpenTelemetryMiddleware) + ] + broker._is_instrumented_by_opentelemetry = False + self._instrumented_brokers.discard(broker) + + def instrumentation_dependencies(self) -> Collection[str]: + return ("taskiq >= 0.0.1",) + + def _instrument(self, **kwargs: Any): + def broker_init(init, broker, args, kwargs): + result = init(*args, **kwargs) + self.instrument_broker(broker) + return result + + wrap_function_wrapper("taskiq", "AsyncBroker.__init__", broker_init) + + def _uninstrument(self, **kwargs): + instances_to_uninstrument = list(self._instrumented_brokers) + for broker in instances_to_uninstrument: + self.uninstrument_broker(broker) + self._instrumented_brokers.clear() + unwrap(AsyncBroker, "__init__") diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/package.py b/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/package.py new file mode 100644 index 0000000000..949e35a134 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/package.py @@ -0,0 +1,16 @@ +# Copyright The OpenTelemetry Authors +# +# 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. + + +_instruments = ("taskiq >= 0.11.0",) diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/utils.py b/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/utils.py new file mode 100644 index 0000000000..63921649d9 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/utils.py @@ -0,0 +1,120 @@ +# Copyright The OpenTelemetry Authors +# +# 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. + +from __future__ import annotations + +import logging +from typing import TYPE_CHECKING, Optional, Tuple + +from taskiq import TaskiqMessage + +from opentelemetry.trace import Span + +if TYPE_CHECKING: + from contextlib import AbstractContextManager + +logger = logging.getLogger(__name__) + +# Taskiq Context key +CTX_KEY = "__otel_task_span" + +# Taskiq Context attributes +TASKIQ_CONTEXT_ATTRIBUTES = [ + "_retries", + "delay", + "max_retries", + "retry_on_error", + "timeout", + "X-Taskiq-requeue", +] + + +# pylint:disable=too-many-branches +def set_attributes_from_context(span, context): + """Helper to extract meta values from a Taskiq Context""" + if not span.is_recording(): + return + + for key in TASKIQ_CONTEXT_ATTRIBUTES: + value = context.get(key) + + # Skip this key if it is not set + if value is None: + continue + + # Skip `retries` if it's value is `0` + if key == "_retries": + if value == "0": + continue + + attribute_name = f"taskiq.{key}" + + span.set_attribute(attribute_name, value) + + +def attach_context( + message: Optional[TaskiqMessage], + span: Span, + activation: AbstractContextManager[Span], + token: Optional[object], + is_publish: bool = False, +) -> None: + """Helper to propagate a `Span`, `ContextManager` and context token + for the given `Task` instance. This function uses a `dict` that stores + the Span using the `(task_id, is_publish)` as a key. This is useful + when information must be propagated from one Celery signal to another. + + We use (task_id, is_publish) for the key to ensure that publishing a + task from within another task does not cause any conflicts. + + This mostly happens when either a task fails and a retry policy is in place, + we end up trying to publish a task with the same id as the task currently running. + """ + if message is None: + return + + ctx_dict = getattr(message, CTX_KEY, None) + + if ctx_dict is None: + ctx_dict = {} + setattr(message, CTX_KEY, ctx_dict) + + ctx_dict[(message.task_id, is_publish)] = (span, activation, token) + + +def detach_context(message: TaskiqMessage, is_publish: bool = False) -> None: + """Helper to remove `Span`, `ContextManager` and context token in a + Taskiq task when it's propagated. + This function handles tasks where no values are attached to the `Task`. + """ + span_dict = getattr(message, CTX_KEY, None) + if span_dict is None: + return + + # See note in `attach_context` for key info + span_dict.pop((message.task_id, is_publish), None) + + +def retrieve_context( + message: TaskiqMessage, is_publish: bool = False +) -> Optional[Tuple[Span, AbstractContextManager[Span], Optional[object]]]: + """Helper to retrieve an active `Span`, `ContextManager` and context token + stored in a `TaskiqMessage` instance + """ + span_dict = getattr(message, CTX_KEY, None) + if span_dict is None: + return None + + # See note in `attach_context` for key info + return span_dict.get((message.task_id, is_publish), None) diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/version.py b/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/version.py new file mode 100644 index 0000000000..46aee9202b --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/src/opentelemetry/instrumentation/taskiq/version.py @@ -0,0 +1,15 @@ +# Copyright The OpenTelemetry Authors +# +# 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. + +__version__ = "0.59b0.dev" diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/test-requirements.txt b/instrumentation/opentelemetry-instrumentation-taskiq/test-requirements.txt new file mode 100644 index 0000000000..4c6ccf9765 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/test-requirements.txt @@ -0,0 +1,5 @@ +pytest==7.4.4 +pytest-asyncio==0.23.5 +taskiq >= 0.11 +-e opentelemetry-instrumentation +-e instrumentation/opentelemetry-instrumentation-taskiq diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/tests/__init__.py b/instrumentation/opentelemetry-instrumentation-taskiq/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/tests/taskiq_test_tasks.py b/instrumentation/opentelemetry-instrumentation-taskiq/tests/taskiq_test_tasks.py new file mode 100644 index 0000000000..a1e1bd6096 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/tests/taskiq_test_tasks.py @@ -0,0 +1,39 @@ +# Copyright The OpenTelemetry Authors +# +# 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. +from taskiq import InMemoryBroker + +from opentelemetry import baggage + +broker = InMemoryBroker( + await_inplace=True # used to sort spans in a deterministic way +) + + +class CustomError(Exception): + pass + + +@broker.task +async def task_add(num_a, num_b): + return num_a + num_b + + +@broker.task +async def task_raises(): + raise CustomError("The task failed!") + + +@broker.task +async def task_returns_baggage(): + return dict(baggage.get_all()) diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_auto_instrumentation.py b/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_auto_instrumentation.py new file mode 100644 index 0000000000..edc9383b49 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_auto_instrumentation.py @@ -0,0 +1,63 @@ +import asyncio + +from taskiq import InMemoryBroker + +from opentelemetry.instrumentation.taskiq import TaskiqInstrumentor +from opentelemetry.test.test_base import TestBase +from opentelemetry.trace import SpanKind, StatusCode + + +class TestTaskiqAutoInstrumentation(TestBase): + def test_auto_instrument(self): + TaskiqInstrumentor().instrument() + + broker = InMemoryBroker(await_inplace=True) + + @broker.task + async def task_add(a, b): + return a + b + + async def test(): + await task_add.kiq(1, 2) + await broker.wait_all() + + asyncio.run(test()) + + spans = self.sorted_spans(self.memory_exporter.get_finished_spans()) + self.assertEqual(len(spans), 2) + + consumer, producer = spans + + self.assertEqual( + consumer.name, + "execute/tests.test_auto_instrumentation:task_add", + f"{consumer._end_time}:{producer._end_time}", + ) + self.assertEqual(consumer.kind, SpanKind.CONSUMER) + self.assertSpanHasAttributes( + consumer, + { + "taskiq.action": "execute", + "taskiq.task_name": "tests.test_auto_instrumentation:task_add", + }, + ) + + self.assertEqual(consumer.status.status_code, StatusCode.UNSET) + + self.assertEqual(0, len(consumer.events)) + + self.assertEqual( + producer.name, "send/tests.test_auto_instrumentation:task_add" + ) + self.assertEqual(producer.kind, SpanKind.PRODUCER) + self.assertSpanHasAttributes( + producer, + { + "taskiq.action": "send", + "taskiq.task_name": "tests.test_auto_instrumentation:task_add", + }, + ) + + self.assertNotEqual(consumer.parent, producer.context) + self.assertEqual(consumer.parent.span_id, producer.context.span_id) + self.assertEqual(consumer.context.trace_id, producer.context.trace_id) diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_duplicate.py b/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_duplicate.py new file mode 100644 index 0000000000..d37d0a4256 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_duplicate.py @@ -0,0 +1,29 @@ +# Copyright The OpenTelemetry Authors +# +# 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. + +import unittest + +from opentelemetry.instrumentation.taskiq import TaskiqInstrumentor + +from .taskiq_test_tasks import broker + + +class TestUtils(unittest.TestCase): + @staticmethod + def test_duplicate_instrumentaion(): + first = TaskiqInstrumentor() + first.instrument_broker(broker) + second = TaskiqInstrumentor() + second.instrument_broker(broker) + TaskiqInstrumentor().uninstrument() diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_tasks.py b/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_tasks.py new file mode 100644 index 0000000000..9ee138682b --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_tasks.py @@ -0,0 +1,204 @@ +# Copyright The OpenTelemetry Authors +# +# 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. + +import asyncio + +from wrapt import wrap_function_wrapper + +from opentelemetry import baggage, context +from opentelemetry.instrumentation.taskiq import TaskiqInstrumentor, utils +from opentelemetry.instrumentation.utils import unwrap +from opentelemetry.semconv.trace import SpanAttributes +from opentelemetry.test.test_base import TestBase +from opentelemetry.trace import SpanKind, StatusCode + +from .taskiq_test_tasks import ( + broker, + task_add, + task_raises, + task_returns_baggage, +) + + +class TestTaskiqInstrumentation(TestBase): + def tearDown(self): + super().tearDown() + TaskiqInstrumentor().uninstrument_broker(broker) + + def test_task(self): + TaskiqInstrumentor().instrument_broker(broker) + + async def test(): + await task_add.kiq(1, 2) + await broker.wait_all() + + asyncio.run(test()) + + spans = self.sorted_spans(self.memory_exporter.get_finished_spans()) + self.assertEqual(len(spans), 2) + + consumer, producer = spans + + self.assertEqual( + consumer.name, + "execute/tests.taskiq_test_tasks:task_add", + f"{consumer._end_time}:{producer._end_time}", + ) + self.assertEqual(consumer.kind, SpanKind.CONSUMER) + self.assertSpanHasAttributes( + consumer, + { + "taskiq.action": "execute", + "taskiq.task_name": "tests.taskiq_test_tasks:task_add", + }, + ) + + self.assertEqual(consumer.status.status_code, StatusCode.UNSET) + + self.assertEqual(0, len(consumer.events)) + + self.assertEqual( + producer.name, "send/tests.taskiq_test_tasks:task_add" + ) + self.assertEqual(producer.kind, SpanKind.PRODUCER) + self.assertSpanHasAttributes( + producer, + { + "taskiq.action": "send", + "taskiq.task_name": "tests.taskiq_test_tasks:task_add", + }, + ) + + self.assertNotEqual(consumer.parent, producer.context) + self.assertEqual(consumer.parent.span_id, producer.context.span_id) + self.assertEqual(consumer.context.trace_id, producer.context.trace_id) + + def test_task_raises(self): + TaskiqInstrumentor().instrument_broker(broker) + + async def test(): + await task_raises.kiq() + await broker.wait_all() + + asyncio.run(test()) + + spans = self.sorted_spans(self.memory_exporter.get_finished_spans()) + self.assertEqual(len(spans), 2) + + consumer, producer = spans + + self.assertEqual( + consumer.name, "execute/tests.taskiq_test_tasks:task_raises" + ) + self.assertEqual(consumer.kind, SpanKind.CONSUMER) + self.assertSpanHasAttributes( + consumer, + { + "taskiq.action": "execute", + "taskiq.task_name": "tests.taskiq_test_tasks:task_raises", + }, + ) + + self.assertEqual(consumer.status.status_code, StatusCode.ERROR) + + self.assertEqual(1, len(consumer.events)) + event = consumer.events[0] + + self.assertIn(SpanAttributes.EXCEPTION_STACKTRACE, event.attributes) + + self.assertEqual( + event.attributes[SpanAttributes.EXCEPTION_TYPE], + "tests.taskiq_test_tasks.CustomError", + ) + + self.assertEqual( + event.attributes[SpanAttributes.EXCEPTION_MESSAGE], + "The task failed!", + ) + + self.assertEqual( + producer.name, "send/tests.taskiq_test_tasks:task_raises" + ) + self.assertEqual(producer.kind, SpanKind.PRODUCER) + self.assertSpanHasAttributes( + producer, + { + "taskiq.action": "send", + "taskiq.task_name": "tests.taskiq_test_tasks:task_raises", + }, + ) + + self.assertNotEqual(consumer.parent, producer.context) + self.assertEqual(consumer.parent.span_id, producer.context.span_id) + self.assertEqual(consumer.context.trace_id, producer.context.trace_id) + + def test_uninstrument(self): + TaskiqInstrumentor().instrument_broker(broker) + TaskiqInstrumentor().uninstrument_broker(broker) + + async def test(): + await task_add.kiq(1, 2) + await broker.wait_all() + + asyncio.run(test()) + + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 0) + + def test_baggage(self): + TaskiqInstrumentor().instrument_broker(broker) + + ctx = baggage.set_baggage("key", "value") + context.attach(ctx) + + async def test(): + task = await task_returns_baggage.kiq() + result = await task.wait_result(timeout=2) + return result.return_value + + result = asyncio.run(test()) + + self.assertEqual(result, {"key": "value"}) + + def test_task_not_instrumented_does_not_raise(self): + def _retrieve_context_wrapper_none_token( + wrapped, instance, args, kwargs + ): + ctx = wrapped(*args, **kwargs) + if ctx is None: + return ctx + span, activation, _ = ctx + return span, activation, None + + wrap_function_wrapper( + utils, + "retrieve_context", + _retrieve_context_wrapper_none_token, + ) + + TaskiqInstrumentor().instrument_broker(broker) + + async def test(): + task = await task_add.kiq(1, 2) + result = await task.wait_result(timeout=2) + return result.return_value + + result = asyncio.run(test()) + + spans = self.sorted_spans(self.memory_exporter.get_finished_spans()) + self.assertEqual(len(spans), 2) + + self.assertTrue(result) + + unwrap(utils, "retrieve_context") diff --git a/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_utils.py b/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_utils.py new file mode 100644 index 0000000000..c31aeec501 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-taskiq/tests/test_utils.py @@ -0,0 +1,124 @@ +# Copyright The OpenTelemetry Authors +# +# 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. + +import unittest +from unittest import mock + +from taskiq import TaskiqMessage + +from opentelemetry import trace as trace_api +from opentelemetry.instrumentation.taskiq import utils +from opentelemetry.sdk import trace + +from .taskiq_test_tasks import broker + + +class TestUtils(unittest.TestCase): + def setUp(self): + self.broker = broker + + def test_set_attributes_from_context(self): + # it should extract only relevant keys + context = { + "_retries": "4", + "delay": "30", + "max_retries": "6", + "retry_on_error": "true", + "timeout": "60", + "X-Taskiq-requeue": "4", + "custom_meta": "custom_value", + } + + span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext)) + utils.set_attributes_from_context(span, context) + + self.assertEqual(span.attributes.get("taskiq._retries"), "4") + self.assertEqual(span.attributes.get("taskiq.delay"), "30") + self.assertEqual(span.attributes.get("taskiq.max_retries"), "6") + self.assertEqual(span.attributes.get("taskiq.retry_on_error"), "true") + self.assertEqual(span.attributes.get("taskiq.timeout"), "60") + self.assertEqual(span.attributes.get("taskiq.X-Taskiq-requeue"), "4") + + self.assertNotIn("custom_meta", span.attributes) + + def test_set_attributes_not_recording(self): + # it should extract only relevant keys + context = { + "_retries": "4", + "delay": "30", + "max_retries": "6", + "retry_on_error": "true", + "timeout": "60", + "X-Taskiq-requeue": "4", + "custom_meta": "custom_value", + } + + mock_span = mock.Mock() + mock_span.is_recording.return_value = False + utils.set_attributes_from_context(mock_span, context) + self.assertFalse(mock_span.is_recording()) + self.assertTrue(mock_span.is_recording.called) + self.assertFalse(mock_span.set_attribute.called) + self.assertFalse(mock_span.set_status.called) + + def test_set_attributes_from_context_empty_keys(self): + # it should not extract empty keys + context = { + "retries": 0, + } + + span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext)) + utils.set_attributes_from_context(span, context) + + self.assertEqual(len(span.attributes), 0) + + def test_span_propagation(self): + # propagate and retrieve a Span + message = mock.Mock( + task_id="7c6731af-9533-40c3-83a9-25b58f0d837f", spec=TaskiqMessage + ) + span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext)) + utils.attach_context(message, span, mock.Mock(), "") + ctx = utils.retrieve_context(message) + self.assertIsNotNone(ctx) + span_after, _, _ = ctx + self.assertIs(span, span_after) + + def test_span_delete(self): + # propagate a Span + message = mock.Mock( + task_id="7c6731af-9533-40c3-83a9-25b58f0d837f", spec=TaskiqMessage + ) + span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext)) + utils.attach_context(message, span, mock.Mock(), "") + # delete the Span + utils.detach_context(message) + self.assertEqual(utils.retrieve_context(message), None) + + def test_optional_message_span_attach(self): + span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext)) + + # assert this is is a no-aop + self.assertIsNone(utils.attach_context(None, span, mock.Mock(), "")) + + def test_span_delete_empty(self): + # delete the Span + message = mock.Mock( + task_id="7c6731af-9533-40c3-83a9-25b58f0d837f", spec=TaskiqMessage + ) + try: + utils.detach_context(message) + self.assertEqual(utils.retrieve_context(message), None) + except Exception as ex: # pylint: disable=broad-except + self.fail(f"Exception was raised: {ex}") diff --git a/opentelemetry-contrib-instrumentations/pyproject.toml b/opentelemetry-contrib-instrumentations/pyproject.toml index 40c9893c37..5854a27847 100644 --- a/opentelemetry-contrib-instrumentations/pyproject.toml +++ b/opentelemetry-contrib-instrumentations/pyproject.toml @@ -74,6 +74,7 @@ dependencies = [ "opentelemetry-instrumentation-sqlite3==0.59b0.dev", "opentelemetry-instrumentation-starlette==0.59b0.dev", "opentelemetry-instrumentation-system-metrics==0.59b0.dev", + "opentelemetry-instrumentation-taskiq==0.59b0.dev", "opentelemetry-instrumentation-threading==0.59b0.dev", "opentelemetry-instrumentation-tornado==0.59b0.dev", "opentelemetry-instrumentation-tortoiseorm==0.59b0.dev", diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py index fae84a772a..2c973ffda4 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py @@ -196,6 +196,10 @@ "library": "psutil >= 5", "instrumentation": "opentelemetry-instrumentation-system-metrics==0.59b0.dev", }, + { + "library": "taskiq >= 0.11", + "instrumentation": "opentelemetry-instrumentation-taskiq==0.59b0.dev", + }, { "library": "tornado >= 5.1.1", "instrumentation": "opentelemetry-instrumentation-tornado==0.59b0.dev", diff --git a/pyproject.toml b/pyproject.toml index 30ada5bb25..b2695a4926 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -55,6 +55,7 @@ dependencies = [ "opentelemetry-instrumentation-sqlalchemy[instruments]", "opentelemetry-instrumentation-sqlite3", "opentelemetry-instrumentation-system-metrics", + "opentelemetry-instrumentation-taskiq[instruments]", "opentelemetry-instrumentation-threading", "opentelemetry-instrumentation-tornado", "opentelemetry-instrumentation-tortoiseorm", @@ -126,6 +127,7 @@ opentelemetry-instrumentation-requests = { workspace = true } opentelemetry-instrumentation-sqlalchemy = { workspace = true } opentelemetry-instrumentation-sqlite3 = { workspace = true } opentelemetry-instrumentation-system-metrics = { workspace = true } +opentelemetry-instrumentation-taskiq = { workspace = true } opentelemetry-instrumentation-threading = { workspace = true } opentelemetry-instrumentation-tornado = { workspace = true } opentelemetry-instrumentation-tortoiseorm = { workspace = true } diff --git a/tox.ini b/tox.ini index 6d3aee161e..1943e7f1f4 100644 --- a/tox.ini +++ b/tox.ini @@ -326,6 +326,11 @@ envlist = pypy3-test-instrumentation-system-metrics lint-instrumentation-system-metrics + ; opentelemetry-instrumentation-taskiq + py3{9,10,11,12,13}-test-instrumentation-taskiq + pypy3-test-instrumentation-taskiq + lint-instrumentation-taskiq + ; opentelemetry-instrumentation-threading py3{9,10,11,12,13}-test-instrumentation-threading pypy3-test-instrumentation-threading @@ -480,6 +485,11 @@ deps = pypy3-test-instrumentation-celery: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-celery/test-requirements-1.txt lint-instrumentation-celery: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-celery/test-requirements-1.txt + taskiq: {[testenv]test_deps} + py3{9,10,11,12,13}-test-instrumentation-taskiq: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-taskiq/test-requirements.txt + pypy3-test-instrumentation-taskiq: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-taskiq/test-requirements.txt + lint-instrumentation-taskiq: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-taskiq/test-requirements.txt + asyncclick: {[testenv]test_deps} asyncclick: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-asyncclick/test-requirements.txt @@ -905,6 +915,9 @@ commands = test-instrumentation-system-metrics: pytest {toxinidir}/instrumentation/opentelemetry-instrumentation-system-metrics/tests {posargs} lint-instrumentation-system-metrics: sh -c "cd instrumentation && pylint --rcfile ../.pylintrc opentelemetry-instrumentation-system-metrics" + test-instrumentation-taskiq: pytest {toxinidir}/instrumentation/opentelemetry-instrumentation-taskiq/tests {posargs} + lint-instrumentation-taskiq: sh -c "cd instrumentation && pylint --rcfile ../.pylintrc opentelemetry-instrumentation-taskiq" + test-instrumentation-threading: pytest {toxinidir}/instrumentation/opentelemetry-instrumentation-threading/tests {posargs} lint-instrumentation-threading: sh -c "cd instrumentation && pylint --rcfile ../.pylintrc opentelemetry-instrumentation-threading" diff --git a/uv.lock b/uv.lock index 6b290de793..01fc4ee8c3 100644 --- a/uv.lock +++ b/uv.lock @@ -64,6 +64,7 @@ members = [ "opentelemetry-instrumentation-sqlite3", "opentelemetry-instrumentation-starlette", "opentelemetry-instrumentation-system-metrics", + "opentelemetry-instrumentation-taskiq", "opentelemetry-instrumentation-threading", "opentelemetry-instrumentation-tornado", "opentelemetry-instrumentation-tortoiseorm", @@ -1787,6 +1788,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/04/96/92447566d16df59b2a776c0fb82dbc4d9e07cd95062562af01e408583fc4/itsdangerous-2.2.0-py3-none-any.whl", hash = "sha256:c6242fc49e35958c8b15141343aa660db5fc54d4f13a1db01a3f5891b98700ef", size = 16234 }, ] +[[package]] +name = "izulu" +version = "0.50.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d0/58/6d6335c78b7ade54d8a6c6dbaa589e5c21b3fd916341d5a16f774c72652a/izulu-0.50.0.tar.gz", hash = "sha256:cc8e252d5e8560c70b95380295008eeb0786f7b745a405a40d3556ab3252d5f5", size = 48558, upload-time = "2025-03-24T15:52:21.51Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/4a/9f/bf9d33546bbb6e5e80ebafe46f90b7d8b4a77410b7b05160b0ca8978c15a/izulu-0.50.0-py3-none-any.whl", hash = "sha256:4e9ae2508844e7c5f62c468a8b9e2deba2f60325ef63f01e65b39fd9a6b3fab4", size = 18095, upload-time = "2025-03-24T15:52:19.667Z" }, +] + [[package]] name = "jinja2" version = "3.1.6" @@ -3753,6 +3763,29 @@ requires-dist = [ ] provides-extras = ["instruments"] +[[package]] +name = "opentelemetry-instrumentation-taskiq" +source = { editable = "instrumentation/opentelemetry-instrumentation-taskiq" } +dependencies = [ + { name = "opentelemetry-api" }, + { name = "opentelemetry-instrumentation" }, + { name = "opentelemetry-semantic-conventions" }, +] + +[package.optional-dependencies] +instruments = [ + { name = "taskiq" }, +] + +[package.metadata] +requires-dist = [ + { name = "opentelemetry-api", git = "https://github.com/open-telemetry/opentelemetry-python?subdirectory=opentelemetry-api&branch=main" }, + { name = "opentelemetry-instrumentation", editable = "opentelemetry-instrumentation" }, + { name = "opentelemetry-semantic-conventions", git = "https://github.com/open-telemetry/opentelemetry-python?subdirectory=opentelemetry-semantic-conventions&branch=main" }, + { name = "taskiq", marker = "extra == 'instruments'", specifier = ">=0.11" }, +] +provides-extras = ["instruments"] + [[package]] name = "opentelemetry-instrumentation-threading" source = { editable = "instrumentation/opentelemetry-instrumentation-threading" } @@ -4010,6 +4043,7 @@ dependencies = [ { name = "opentelemetry-instrumentation-sqlalchemy", extra = ["instruments"] }, { name = "opentelemetry-instrumentation-sqlite3" }, { name = "opentelemetry-instrumentation-system-metrics" }, + { name = "opentelemetry-instrumentation-taskiq", extra = ["instruments"] }, { name = "opentelemetry-instrumentation-threading" }, { name = "opentelemetry-instrumentation-tornado" }, { name = "opentelemetry-instrumentation-tortoiseorm" }, @@ -4077,6 +4111,7 @@ requires-dist = [ { name = "opentelemetry-instrumentation-sqlalchemy", extras = ["instruments"], editable = "instrumentation/opentelemetry-instrumentation-sqlalchemy" }, { name = "opentelemetry-instrumentation-sqlite3", editable = "instrumentation/opentelemetry-instrumentation-sqlite3" }, { name = "opentelemetry-instrumentation-system-metrics", editable = "instrumentation/opentelemetry-instrumentation-system-metrics" }, + { name = "opentelemetry-instrumentation-taskiq", extras = ["instruments"], editable = "instrumentation/opentelemetry-instrumentation-taskiq" }, { name = "opentelemetry-instrumentation-threading", editable = "instrumentation/opentelemetry-instrumentation-threading" }, { name = "opentelemetry-instrumentation-tornado", editable = "instrumentation/opentelemetry-instrumentation-tornado" }, { name = "opentelemetry-instrumentation-tortoiseorm", editable = "instrumentation/opentelemetry-instrumentation-tortoiseorm" }, @@ -4594,6 +4629,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/13/a3/a812df4e2dd5696d1f351d58b8fe16a405b234ad2886a0dab9183fb78109/pycparser-2.22-py3-none-any.whl", hash = "sha256:c3702b6d3dd8c7abc1afa565d7e63d53a1d0bd86cdc24edd75470f4de499cfcc", size = 117552 }, ] +[[package]] +name = "pycron" +version = "3.2.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/8e/5d/340be12ae4a69c33102dfb6ddc1dc6e53e69b2d504fa26b5d34a472c3057/pycron-3.2.0.tar.gz", hash = "sha256:e125a28aca0295769541a40633f70b602579df48c9cb357c36c28d2628ba2b13", size = 4248, upload-time = "2025-06-05T13:24:12.636Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/f0/76/caf316909f4545e7158e0e1defd8956a1da49f4af04f5d16b18c358dfeac/pycron-3.2.0-py3-none-any.whl", hash = "sha256:6d2349746270bd642b71b9f7187cf13f4d9ee2412b4710396a507b5fe4f60dac", size = 4904, upload-time = "2025-06-05T13:24:11.477Z" }, +] + [[package]] name = "pydantic" version = "2.11.4" @@ -5598,6 +5642,38 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/8b/0c/9d30a4ebeb6db2b25a841afbb80f6ef9a854fc3b41be131d249a977b4959/starlette-0.46.2-py3-none-any.whl", hash = "sha256:595633ce89f8ffa71a015caed34a5b2dc1c0cdb3f0f1fbd1e69339cf2abeec35", size = 72037 }, ] +[[package]] +name = "taskiq" +version = "0.11.18" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio" }, + { name = "importlib-metadata" }, + { name = "izulu" }, + { name = "packaging" }, + { name = "pycron" }, + { name = "pydantic" }, + { name = "pytz" }, + { name = "taskiq-dependencies" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/91/4d/0d1b3b6c77a45d7a8c685a9c916b2532cca36a26771831949b874f6d15c3/taskiq-0.11.18.tar.gz", hash = "sha256:b83e1b70aee74d0a197d4a4a5ba165b8ba85b12a2b3b7ebfa3c6fdcc9e3128a7", size = 54323, upload-time = "2025-07-15T16:25:54.37Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/4a/d5/46505f57c140d10d4c36f6bd2f2047fb0460e4d5b9b841dc3b93ab8c893d/taskiq-0.11.18-py3-none-any.whl", hash = "sha256:0df58be24e4ef5d19c8ef02581d35d392b0d780d3fe37950e0478022b85ce288", size = 79608, upload-time = "2025-07-15T16:25:52.707Z" }, +] + +[[package]] +name = "taskiq-dependencies" +version = "1.5.7" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "typing-extensions", marker = "python_full_version < '3.10'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/47/90/47a627696e53bfdcacabc3e8c05b73bf1424685bcb5f17209cb8b12da1bf/taskiq_dependencies-1.5.7.tar.gz", hash = "sha256:0d3b240872ef152b719153b9526d866d2be978aeeaea6600e878414babc2dcb4", size = 14875, upload-time = "2025-02-26T22:07:39.876Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/99/6d/4a012f2de002c2e93273f5e7d3e3feea02f7fdbb7b75ca2ca1dd10703091/taskiq_dependencies-1.5.7-py3-none-any.whl", hash = "sha256:6fcee5d159bdb035ef915d4d848826169b6f06fe57cc2297a39b62ea3e76036f", size = 13801, upload-time = "2025-02-26T22:07:38.622Z" }, +] + [[package]] name = "tenacity" version = "9.1.2"