diff --git a/.github/workflows/core_contrib_test.yml b/.github/workflows/core_contrib_test.yml index d9f90d57d8..ba6289a923 100644 --- a/.github/workflows/core_contrib_test.yml +++ b/.github/workflows/core_contrib_test.yml @@ -1767,6 +1767,36 @@ jobs: - name: Run tests run: tox -e py310-test-instrumentation-starlette-latest -- -ra + py310-test-instrumentation-structlog: + name: instrumentation-structlog + 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.10 + uses: actions/setup-python@v5 + with: + python-version: "3.10" + architecture: "x64" + + - name: Install tox + run: pip install tox-uv + + - name: Run tests + run: tox -e py310-test-instrumentation-structlog -- -ra + py310-test-instrumentation-jinja2: name: instrumentation-jinja2 runs-on: ubuntu-latest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 6bf3c631cd..b751f9b52f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -575,6 +575,25 @@ jobs: - name: Run tests run: tox -e lint-instrumentation-starlette + lint-instrumentation-structlog: + name: instrumentation-structlog + 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-instrumentation-structlog + lint-instrumentation-jinja2: name: instrumentation-jinja2 runs-on: ubuntu-latest diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9c694007ea..00174695a0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5914,6 +5914,120 @@ jobs: - name: Run tests run: tox -e pypy3-test-instrumentation-starlette-latest -- -ra + py310-test-instrumentation-structlog_ubuntu-latest: + name: instrumentation-structlog 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-structlog -- -ra + + py311-test-instrumentation-structlog_ubuntu-latest: + name: instrumentation-structlog 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-structlog -- -ra + + py312-test-instrumentation-structlog_ubuntu-latest: + name: instrumentation-structlog 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-structlog -- -ra + + py313-test-instrumentation-structlog_ubuntu-latest: + name: instrumentation-structlog 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-structlog -- -ra + + py314-test-instrumentation-structlog_ubuntu-latest: + name: instrumentation-structlog 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-instrumentation-structlog -- -ra + + pypy3-test-instrumentation-structlog_ubuntu-latest: + name: instrumentation-structlog 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-instrumentation-structlog -- -ra + py310-test-instrumentation-jinja2_ubuntu-latest: name: instrumentation-jinja2 3.10 Ubuntu runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index 64da93791c..de4261cbb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4244](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4244)) - `opentelemetry-instrumentation-sqlite3`: Add uninstrument, error status, suppress, and no-op tests ([#4335](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4335)) +- `opentelemetry-instrumentation-structlog`: Add new package providing `StructlogHandler` and `StructlogInstrumentor` to bridge structlog into the OpenTelemetry Logs SDK pipeline with trace context correlation and exception capture. `opentelemetry-instrumentation`: Add shared `log_utils` + ([#4286](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4286)) ### Fixed @@ -288,7 +290,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3912](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3912)) ### Added - - `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/docs-requirements.txt b/docs-requirements.txt index 9d0be53704..0b55338bcd 100644 --- a/docs-requirements.txt +++ b/docs-requirements.txt @@ -47,6 +47,7 @@ redis>=2.6 remoulade>=0.50 sqlalchemy>=1.0 starlette~=0.50 +structlog>=21.1 tornado>=5.1.1 tortoise-orm>=0.17.0 wrapt~=2.1 diff --git a/docs/instrumentation/structlog/structlog.rst b/docs/instrumentation/structlog/structlog.rst new file mode 100644 index 0000000000..26f11204d5 --- /dev/null +++ b/docs/instrumentation/structlog/structlog.rst @@ -0,0 +1,7 @@ +OpenTelemetry Structlog Instrumentation +======================================= + +.. automodule:: opentelemetry.instrumentation.structlog + :members: + :undoc-members: + :show-inheritance: diff --git a/instrumentation/README.md b/instrumentation/README.md index 8d9a247945..95b7fad9b7 100644 --- a/instrumentation/README.md +++ b/instrumentation/README.md @@ -44,6 +44,7 @@ | [opentelemetry-instrumentation-sqlalchemy](./opentelemetry-instrumentation-sqlalchemy) | sqlalchemy >= 1.0.0, < 2.1.0 | Yes | migration | [opentelemetry-instrumentation-sqlite3](./opentelemetry-instrumentation-sqlite3) | sqlite3 | No | development | [opentelemetry-instrumentation-starlette](./opentelemetry-instrumentation-starlette) | starlette >= 0.13 | Yes | development +| [opentelemetry-instrumentation-structlog](./opentelemetry-instrumentation-structlog) | structlog >= 21.1 | No | development | [opentelemetry-instrumentation-system-metrics](./opentelemetry-instrumentation-system-metrics) | psutil >= 5 | No | development | [opentelemetry-instrumentation-threading](./opentelemetry-instrumentation-threading) | threading | No | development | [opentelemetry-instrumentation-tornado](./opentelemetry-instrumentation-tornado) | tornado >= 5.1.1 | Yes | migration diff --git a/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/handler.py b/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/handler.py index 44ba3164ad..749925575d 100644 --- a/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/handler.py +++ b/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/handler.py @@ -26,12 +26,12 @@ LoggerProvider, LogRecord, NoOpLogger, - SeverityNumber, get_logger, get_logger_provider, ) from opentelemetry.attributes import _VALID_ANY_VALUE_TYPES from opentelemetry.context import get_current +from opentelemetry.instrumentation.log_utils import std_to_otel from opentelemetry.semconv._incubating.attributes import code_attributes from opentelemetry.semconv.attributes import exception_attributes from opentelemetry.util.types import _ExtendedAttributes @@ -248,63 +248,3 @@ def flush(self) -> None: # details see https://github.com/open-telemetry/opentelemetry-python/pull/4636. thread = threading.Thread(target=self._logger_provider.force_flush) # type: ignore[reportAttributeAccessIssue] thread.start() - - -_STD_TO_OTEL = { - 10: SeverityNumber.DEBUG, - 11: SeverityNumber.DEBUG2, - 12: SeverityNumber.DEBUG3, - 13: SeverityNumber.DEBUG4, - 14: SeverityNumber.DEBUG4, - 15: SeverityNumber.DEBUG4, - 16: SeverityNumber.DEBUG4, - 17: SeverityNumber.DEBUG4, - 18: SeverityNumber.DEBUG4, - 19: SeverityNumber.DEBUG4, - 20: SeverityNumber.INFO, - 21: SeverityNumber.INFO2, - 22: SeverityNumber.INFO3, - 23: SeverityNumber.INFO4, - 24: SeverityNumber.INFO4, - 25: SeverityNumber.INFO4, - 26: SeverityNumber.INFO4, - 27: SeverityNumber.INFO4, - 28: SeverityNumber.INFO4, - 29: SeverityNumber.INFO4, - 30: SeverityNumber.WARN, - 31: SeverityNumber.WARN2, - 32: SeverityNumber.WARN3, - 33: SeverityNumber.WARN4, - 34: SeverityNumber.WARN4, - 35: SeverityNumber.WARN4, - 36: SeverityNumber.WARN4, - 37: SeverityNumber.WARN4, - 38: SeverityNumber.WARN4, - 39: SeverityNumber.WARN4, - 40: SeverityNumber.ERROR, - 41: SeverityNumber.ERROR2, - 42: SeverityNumber.ERROR3, - 43: SeverityNumber.ERROR4, - 44: SeverityNumber.ERROR4, - 45: SeverityNumber.ERROR4, - 46: SeverityNumber.ERROR4, - 47: SeverityNumber.ERROR4, - 48: SeverityNumber.ERROR4, - 49: SeverityNumber.ERROR4, - 50: SeverityNumber.FATAL, - 51: SeverityNumber.FATAL2, - 52: SeverityNumber.FATAL3, - 53: SeverityNumber.FATAL4, -} - - -def std_to_otel(levelno: int) -> SeverityNumber: - """ - Map python log levelno as defined in https://docs.python.org/3/library/logging.html#logging-levels - to OTel log severity number as defined here: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-severitynumber - """ - if levelno < 10: - return SeverityNumber.UNSPECIFIED - if levelno > 53: - return SeverityNumber.FATAL4 - return _STD_TO_OTEL[levelno] diff --git a/instrumentation/opentelemetry-instrumentation-structlog/LICENSE b/instrumentation/opentelemetry-instrumentation-structlog/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-structlog/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-structlog/README.rst b/instrumentation/opentelemetry-instrumentation-structlog/README.rst new file mode 100644 index 0000000000..81ed7a9495 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-structlog/README.rst @@ -0,0 +1,22 @@ +OpenTelemetry structlog integration +=================================== + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-structlog.svg + :target: https://pypi.org/project/opentelemetry-instrumentation-structlog/ + +Installation +------------ + +:: + + pip install opentelemetry-instrumentation-structlog + + +References +---------- + +* `OpenTelemetry structlog integration `_ +* `OpenTelemetry Project `_ +* `OpenTelemetry Python Examples `_ diff --git a/instrumentation/opentelemetry-instrumentation-structlog/pyproject.toml b/instrumentation/opentelemetry-instrumentation-structlog/pyproject.toml new file mode 100644 index 0000000000..87824867aa --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-structlog/pyproject.toml @@ -0,0 +1,55 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "opentelemetry-instrumentation-structlog" +dynamic = ["version"] +description = "OpenTelemetry structlog instrumentation" +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", + "Intended Audience :: Developers", + "License :: OSI Approved :: Apache Software License", + "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 = [ + "opentelemetry-api ~= 1.12", + "opentelemetry-instrumentation == 0.63b0.dev", + "opentelemetry-semantic-conventions == 0.63b0.dev", +] + +[project.optional-dependencies] +instruments = [ + "structlog >= 21.1", +] + +[project.entry-points.opentelemetry_instrumentor] +structlog = "opentelemetry.instrumentation.structlog:StructlogInstrumentor" + +[project.urls] +Homepage = "https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-structlog" +Repository = "https://github.com/open-telemetry/opentelemetry-python-contrib" + +[tool.hatch.version] +path = "src/opentelemetry/instrumentation/structlog/version.py" + +[tool.hatch.build.targets.sdist] +include = [ + "/src", + "/tests", +] + +[tool.hatch.build.targets.wheel] +packages = ["src/opentelemetry"] diff --git a/instrumentation/opentelemetry-instrumentation-structlog/src/opentelemetry/instrumentation/structlog/__init__.py b/instrumentation/opentelemetry-instrumentation-structlog/src/opentelemetry/instrumentation/structlog/__init__.py new file mode 100644 index 0000000000..19094539e6 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-structlog/src/opentelemetry/instrumentation/structlog/__init__.py @@ -0,0 +1,384 @@ +# 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. + +""" +The OpenTelemetry structlog integration provides a processor that emits structlog +events as OpenTelemetry logs. + +.. code-block:: python + + import structlog + from opentelemetry.instrumentation.structlog import StructlogInstrumentor + + StructlogInstrumentor().instrument() + + logger = structlog.get_logger() + logger.info("user logged in", user_id=42) + +This will emit the structlog event as an OpenTelemetry LogRecord, preserving all +context including trace context, custom attributes, and exception information. +""" + +import sys +import traceback +from datetime import datetime, timezone +from time import time_ns +from typing import Any, Callable, Collection, Optional + +import structlog + +from opentelemetry._logs import ( + LogRecord, + NoOpLogger, + get_logger, + get_logger_provider, +) +from opentelemetry.context import get_current +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.log_utils import std_to_otel +from opentelemetry.instrumentation.structlog.package import _instruments +from opentelemetry.semconv.attributes import ( + exception_attributes, +) + +# Reserved keys that structlog uses internally and should not be passed as attributes +_STRUCTLOG_RESERVED_KEYS = frozenset( + { + "event", + "level", + "timestamp", + "exc_info", + "exception", + "_record", + "_logger", + "_name", + } +) + +# Map structlog level names to standard library log level numbers +_STRUCTLOG_LEVEL_TO_LEVELNO = { + "debug": 10, + "info": 20, + "warning": 30, + "warn": 30, + "error": 40, + "critical": 50, + "fatal": 50, +} + +# Map structlog level names to OTel canonical severity text where they differ +_STRUCTLOG_TO_OTEL_SEVERITY_TEXT = { + "warning": "WARN", + "critical": "FATAL", + "fatal": "FATAL", +} + + +def _parse_structlog_timestamp(value: Any) -> Optional[int]: + """ + Convert a structlog timestamp value to nanoseconds since epoch, or None. + + structlog's TimeStamper emits either a float (UNIX seconds, the default) + or a string (ISO 8601 when fmt="iso", or a strftime pattern otherwise). + We handle float and ISO 8601; anything else returns None so the SDK can + fill in the observed time. + """ + if value is None: + return None + if isinstance(value, (int, float)): + return int(value * 1e9) + if isinstance(value, str): + try: + dt = datetime.fromisoformat(value) + if dt.tzinfo is None: + dt = dt.replace(tzinfo=timezone.utc) + return int(dt.timestamp() * 1e9) + except ValueError: + return None + return None + + +class StructlogHandler: + """ + A structlog handler that translates structlog events into OpenTelemetry LogRecords. + + This handler should be added to the structlog processor chain to emit logs + to OpenTelemetry. It translates structlog's event dictionary format into the + OpenTelemetry Logs data model. + + Args: + logger_provider: The LoggerProvider to use. If None, uses the global provider. + """ + + def __init__(self, logger_provider=None): + """Initialize the handler with an optional logger provider.""" + self._logger_provider = logger_provider or get_logger_provider() + + def __call__(self, logger, name: str, event_dict: dict) -> dict: + """ + Process a structlog event and emit it as an OpenTelemetry log. + + This method implements the structlog handler interface. It receives + the event dictionary, translates it to an OTel LogRecord, and emits it. + + Args: + logger: The structlog logger instance (unused). + name: The logger name. + event_dict: The structlog event dictionary. + + Returns: + The unmodified event_dict (passthrough for other processors). + """ + otel_logger = get_logger(name, logger_provider=self._logger_provider) + + # Skip emission if we have a no-op logger + if not isinstance(otel_logger, NoOpLogger): + log_record = self._translate(event_dict) + otel_logger.emit(log_record) + + return event_dict + + @staticmethod + def _get_attributes(event_dict: dict) -> dict[str, Any]: + """ + Extract attributes from the structlog event dictionary. + + Filters out reserved keys and extracts exception information into + the appropriate semantic convention attributes. + + Args: + event_dict: The structlog event dictionary. + + Returns: + Dictionary of attributes to attach to the LogRecord. + """ + # Start with all non-reserved keys + attributes = { + k: v + for k, v in event_dict.items() + if k not in _STRUCTLOG_RESERVED_KEYS + } + + # Handle exception information + exc_info = event_dict.get("exc_info") + + if exc_info is True: + # exc_info=True means "get current exception" + exc_info = sys.exc_info() + elif isinstance(exc_info, BaseException): + # exc_info can also be passed as an exception instance directly + exc_info = (type(exc_info), exc_info, exc_info.__traceback__) + + if isinstance(exc_info, tuple) and len(exc_info) == 3: + exctype, value, tb = exc_info + if exctype is not None: + attributes[exception_attributes.EXCEPTION_TYPE] = ( + exctype.__name__ + ) + if value is not None and value.args: + attributes[exception_attributes.EXCEPTION_MESSAGE] = str( + value.args[0] + ) + if tb is not None: + attributes[exception_attributes.EXCEPTION_STACKTRACE] = ( + "".join(traceback.format_exception(*exc_info)) + ) + + # Handle pre-rendered exception string (from structlog's ExceptionRenderer) + exception_str = event_dict.get("exception") + if isinstance(exception_str, str): + # If we don't already have a stacktrace from exc_info, use this + if exception_attributes.EXCEPTION_STACKTRACE not in attributes: + attributes[exception_attributes.EXCEPTION_STACKTRACE] = ( + exception_str + ) + + return attributes + + def _translate(self, event_dict: dict) -> LogRecord: + """ + Translate a structlog event dictionary into an OpenTelemetry LogRecord. + + Args: + event_dict: The structlog event dictionary. + + Returns: + An OpenTelemetry LogRecord. + """ + # observed_timestamp is when the SDK received the event (always now). + # timestamp is when the event occurred; use the structlog "timestamp" + # field if present and parseable (UNIX float or ISO 8601 string), + # otherwise leave as None and let the SDK fill it in. + observed_timestamp = time_ns() + timestamp = _parse_structlog_timestamp(event_dict.get("timestamp")) + + # Get the log level and map to OTel severity + level_str = event_dict.get("level", "info") + levelno = _STRUCTLOG_LEVEL_TO_LEVELNO.get(level_str.lower(), 20) + severity_number = std_to_otel(levelno) + + # Normalize severity text to OTel canonical names where structlog + # level names differ: "warning" -> "WARN", "critical"/"fatal" -> "FATAL" + severity_text = _STRUCTLOG_TO_OTEL_SEVERITY_TEXT.get( + level_str.lower(), level_str.upper() + ) + + # Get the message body + body = event_dict.get("event") + + # Get attributes (filters reserved keys and extracts exception info) + attributes = self._get_attributes(event_dict) + + # Get the current OTel context (includes trace context and baggage) + context = get_current() + + return LogRecord( + timestamp=timestamp, + observed_timestamp=observed_timestamp, + context=context, + severity_text=severity_text, + severity_number=severity_number, + body=body, + attributes=attributes, + ) + + def flush(self) -> None: + """ + Flush the logger provider. + """ + if hasattr(self._logger_provider, "force_flush") and callable( + self._logger_provider.force_flush + ): + self._logger_provider.force_flush() + + +class StructlogInstrumentor(BaseInstrumentor): + """ + An instrumentor for the structlog logging library. + + This instrumentor adds a StructlogHandler to the structlog processor + chain, enabling automatic emission of structlog events as OpenTelemetry logs. + + Example: + >>> from opentelemetry.instrumentation.structlog import StructlogInstrumentor + >>> import structlog + >>> StructlogInstrumentor().instrument() + >>> logger = structlog.get_logger() + >>> logger.info("hello", user="alice") + """ + + _processor: Optional["StructlogHandler"] = None + _original_configure: Optional[Callable] = None + + def instrumentation_dependencies(self) -> Collection[str]: + """Return the required instrumentation dependencies.""" + return _instruments + + def _instrument(self, **kwargs): + """ + Add the StructlogHandler to structlog's processor chain. + + The handler is inserted before the last processor in the current chain. + This assumes the last processor is a renderer (e.g. ConsoleRenderer, + JSONRenderer). The handler must run before rendering so it receives the + raw event dict rather than a formatted string. + + If your chain does not end with a renderer, or has post-processing steps + after the renderer, configure the chain manually instead of relying on + auto-instrumentation: + + structlog.configure(processors=[ + structlog.stdlib.add_log_level, + StructlogHandler(logger_provider=provider), + structlog.dev.ConsoleRenderer(), + ]) + + Args: + logger_provider: Optional LoggerProvider to use. + """ + # Create the OTel processor + logger_provider = kwargs.get("logger_provider") + processor = StructlogHandler(logger_provider=logger_provider) + + # Get current structlog configuration + config = structlog.get_config() + current_processors = list(config.get("processors", [])) + + # Insert before the last processor, assumed to be the renderer. + if current_processors: + insert_position = len(current_processors) - 1 + else: + insert_position = 0 + + current_processors.insert(insert_position, processor) + + # Reconfigure structlog with the new processor chain + structlog.configure(processors=current_processors) + + # Store reference for uninstrumentation + StructlogInstrumentor._processor = processor + + # Wrap structlog.configure so that if user code calls it after + # instrumentation, the handler is re-inserted into the new chain. + StructlogInstrumentor._original_configure = structlog.configure + + def _patched_configure(**kwargs): + # If the user is supplying a processors list, ensure our handler + # is included before passing it to the original configure. + if "processors" in kwargs: + processors = list(kwargs["processors"]) + if not any( + isinstance(p, StructlogHandler) for p in processors + ): + insert_position = max(len(processors) - 1, 0) + processors.insert( + insert_position, StructlogInstrumentor._processor + ) + kwargs["processors"] = processors + original = StructlogInstrumentor._original_configure + if original is not None: + return original(**kwargs) + return None + + structlog.configure = _patched_configure + + def _uninstrument(self, **kwargs): + """ + Remove the StructlogHandler from structlog's processor chain. + """ + if StructlogInstrumentor._processor is None: + return + + # Get current structlog configuration + config = structlog.get_config() + current_processors = list(config.get("processors", [])) + + # Remove all StructlogHandler instances + new_processors = [ + p + for p in current_processors + if not isinstance(p, StructlogHandler) + ] + + # Restore the original structlog.configure before reconfiguring so + # the patched version does not re-insert the handler. + if StructlogInstrumentor._original_configure is not None: + structlog.configure = StructlogInstrumentor._original_configure + StructlogInstrumentor._original_configure = None + + # Reconfigure structlog without the handler + structlog.configure(processors=new_processors) + + # Clear reference + StructlogInstrumentor._processor = None diff --git a/instrumentation/opentelemetry-instrumentation-structlog/src/opentelemetry/instrumentation/structlog/package.py b/instrumentation/opentelemetry-instrumentation-structlog/src/opentelemetry/instrumentation/structlog/package.py new file mode 100644 index 0000000000..7fd1fd9cac --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-structlog/src/opentelemetry/instrumentation/structlog/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 = ("structlog >= 21.1",) diff --git a/instrumentation/opentelemetry-instrumentation-structlog/src/opentelemetry/instrumentation/structlog/version.py b/instrumentation/opentelemetry-instrumentation-structlog/src/opentelemetry/instrumentation/structlog/version.py new file mode 100644 index 0000000000..a07bc2663e --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-structlog/src/opentelemetry/instrumentation/structlog/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.63b0.dev" diff --git a/instrumentation/opentelemetry-instrumentation-structlog/test-requirements.txt b/instrumentation/opentelemetry-instrumentation-structlog/test-requirements.txt new file mode 100644 index 0000000000..b94de73cd1 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-structlog/test-requirements.txt @@ -0,0 +1,14 @@ +asgiref==3.8.1 +Deprecated==1.2.14 +iniconfig==2.0.0 +packaging==24.0 +pluggy==1.5.0 +py-cpuinfo==9.0.0 +pytest==7.4.4 +tomli==2.0.1 +typing_extensions==4.12.2 +wrapt==1.16.0 +zipp==3.19.2 +structlog~=21.1 +-e opentelemetry-instrumentation +-e instrumentation/opentelemetry-instrumentation-structlog diff --git a/instrumentation/opentelemetry-instrumentation-structlog/tests/test_structlog.py b/instrumentation/opentelemetry-instrumentation-structlog/tests/test_structlog.py new file mode 100644 index 0000000000..5eb8568339 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-structlog/tests/test_structlog.py @@ -0,0 +1,500 @@ +# 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 sys +import time +from unittest.mock import Mock + +import structlog + +from opentelemetry._logs import SeverityNumber +from opentelemetry.instrumentation.structlog import ( + StructlogHandler, + StructlogInstrumentor, +) +from opentelemetry.sdk._logs import LoggerProvider +from opentelemetry.sdk._logs.export import ( + InMemoryLogRecordExporter, + SimpleLogRecordProcessor, +) +from opentelemetry.semconv.attributes import exception_attributes +from opentelemetry.test.test_base import TestBase +from opentelemetry.trace import TraceFlags + + +class TestStructlogHandler(TestBase): + """Tests for the StructlogHandler class.""" + + def setUp(self): + super().setUp() + # Set up in-memory log exporter + self.exporter = InMemoryLogRecordExporter() + self.logger_provider = LoggerProvider() + self.logger_provider.add_log_record_processor( + SimpleLogRecordProcessor(self.exporter) + ) + + # Configure structlog with OTel handler + self.processor = StructlogHandler(logger_provider=self.logger_provider) + structlog.configure( + processors=[ + structlog.stdlib.add_log_level, + self.processor, + structlog.dev.ConsoleRenderer(), + ] + ) + self.logger = structlog.get_logger() + self.tracer = self.tracer_provider.get_tracer(__name__) + + def tearDown(self): + super().tearDown() + # Reset structlog configuration + structlog.reset_defaults() + self.exporter.clear() + + def test_basic_log_emission(self): + """Test that basic logs are emitted correctly.""" + self.logger.info("test message", user="alice", count=42) + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + self.assertEqual(log.log_record.body, "test message") + self.assertEqual(log.log_record.severity_text, "INFO") + self.assertEqual(log.log_record.severity_number, SeverityNumber.INFO) + self.assertIn("user", log.log_record.attributes) + self.assertEqual(log.log_record.attributes["user"], "alice") + self.assertIn("count", log.log_record.attributes) + self.assertEqual(log.log_record.attributes["count"], 42) + + def test_debug_level(self): + """Test debug level mapping.""" + self.logger.debug("debug message") + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + self.assertEqual(log.log_record.severity_text, "DEBUG") + self.assertEqual(log.log_record.severity_number, SeverityNumber.DEBUG) + + def test_info_level(self): + """Test info level mapping.""" + self.logger.info("info message") + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + self.assertEqual(log.log_record.severity_text, "INFO") + self.assertEqual(log.log_record.severity_number, SeverityNumber.INFO) + + def test_warning_level(self): + """Test warning level mapping and normalization to WARN.""" + self.logger.warning("warning message") + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + # Should be normalized to "WARN" per OTel spec + self.assertEqual(log.log_record.severity_text, "WARN") + self.assertEqual(log.log_record.severity_number, SeverityNumber.WARN) + + def test_error_level(self): + """Test error level mapping.""" + self.logger.error("error message") + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + self.assertEqual(log.log_record.severity_text, "ERROR") + self.assertEqual(log.log_record.severity_number, SeverityNumber.ERROR) + + def test_critical_level(self): + """Test critical level mapping.""" + self.logger.critical("critical message") + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + self.assertEqual(log.log_record.severity_text, "FATAL") + self.assertEqual(log.log_record.severity_number, SeverityNumber.FATAL) + + def test_exception_from_exc_info_tuple(self): + """Test exception extraction from exc_info tuple.""" + try: + raise ValueError("test error") + except ValueError: + exc_info = sys.exc_info() + self.logger.error("error occurred", exc_info=exc_info) + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + attrs = log.log_record.attributes + + self.assertIn(exception_attributes.EXCEPTION_TYPE, attrs) + self.assertEqual( + attrs[exception_attributes.EXCEPTION_TYPE], "ValueError" + ) + + self.assertIn(exception_attributes.EXCEPTION_MESSAGE, attrs) + self.assertEqual( + attrs[exception_attributes.EXCEPTION_MESSAGE], "test error" + ) + + self.assertIn(exception_attributes.EXCEPTION_STACKTRACE, attrs) + stacktrace = attrs[exception_attributes.EXCEPTION_STACKTRACE] + self.assertIn("ValueError", stacktrace) + self.assertIn("test error", stacktrace) + + def test_exception_from_exc_info_true(self): + """Test exception extraction when exc_info=True.""" + try: + raise RuntimeError("runtime error") + except RuntimeError: + self.logger.error("error occurred", exc_info=True) + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + attrs = log.log_record.attributes + + self.assertIn(exception_attributes.EXCEPTION_TYPE, attrs) + self.assertEqual( + attrs[exception_attributes.EXCEPTION_TYPE], "RuntimeError" + ) + + self.assertIn(exception_attributes.EXCEPTION_MESSAGE, attrs) + self.assertEqual( + attrs[exception_attributes.EXCEPTION_MESSAGE], "runtime error" + ) + + self.assertIn(exception_attributes.EXCEPTION_STACKTRACE, attrs) + + def test_exception_from_exception_instance(self): + """Test exception extraction when exc_info is an Exception instance.""" + try: + raise KeyError("missing key") + except KeyError as exc: + self.logger.error("error occurred", exc_info=exc) + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + attrs = log.log_record.attributes + + self.assertIn(exception_attributes.EXCEPTION_TYPE, attrs) + self.assertEqual( + attrs[exception_attributes.EXCEPTION_TYPE], "KeyError" + ) + + self.assertIn(exception_attributes.EXCEPTION_MESSAGE, attrs) + + self.assertIn(exception_attributes.EXCEPTION_STACKTRACE, attrs) + + def test_exception_from_string(self): + """Test exception from pre-rendered string (e.g., from ExceptionRenderer).""" + exception_string = "Traceback (most recent call last):\n File test.py\nValueError: test" + self.logger.error("error occurred", exception=exception_string) + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + attrs = log.log_record.attributes + + self.assertIn(exception_attributes.EXCEPTION_STACKTRACE, attrs) + self.assertEqual( + attrs[exception_attributes.EXCEPTION_STACKTRACE], exception_string + ) + + def test_trace_context_with_active_span(self): + """Test that trace context is captured with an active span.""" + with self.tracer.start_as_current_span("test-span") as span: + self.logger.info("message in span") + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + # Context should be set + self.assertIsNotNone(log.log_record.trace_id) + self.assertIsNotNone(log.log_record.span_id) + + # Should match the current span + span_context = span.get_span_context() + self.assertEqual(log.log_record.trace_id, span_context.trace_id) + self.assertEqual(log.log_record.span_id, span_context.span_id) + self.assertEqual( + log.log_record.trace_flags, + TraceFlags(span_context.trace_flags), + ) + + def test_without_active_span(self): + """Test that logging works without an active span.""" + self.logger.info("message without span") + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + # Should still have a log record, just no trace context + self.assertEqual(log.log_record.body, "message without span") + + def test_reserved_keys_filtered(self): + """Test that structlog reserved keys are filtered from attributes.""" + # These keys should be filtered out + self.logger.info( + "test", + user="alice", + _record="should be filtered", + _logger="should be filtered", + _name="should be filtered", + ) + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + attrs = log.log_record.attributes + + # User attribute should be present + self.assertIn("user", attrs) + self.assertEqual(attrs["user"], "alice") + + # Reserved keys should be filtered + self.assertNotIn("_record", attrs) + self.assertNotIn("_logger", attrs) + self.assertNotIn("_name", attrs) + self.assertNotIn("event", attrs) + self.assertNotIn("level", attrs) + self.assertNotIn("timestamp", attrs) + + def test_custom_attributes_pass_through(self): + """Test that custom attributes are passed through correctly.""" + self.logger.info( + "test", + user_id=123, + request_id="abc-def", + ip_address="192.168.1.1", + duration=1.5, + success=True, + ) + + logs = self.exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + + log = logs[0] + attrs = log.log_record.attributes + + self.assertEqual(attrs["user_id"], 123) + self.assertEqual(attrs["request_id"], "abc-def") + self.assertEqual(attrs["ip_address"], "192.168.1.1") + self.assertEqual(attrs["duration"], 1.5) + self.assertEqual(attrs["success"], True) + + @staticmethod + def test_flush(): + """Test that flush calls force_flush on the provider.""" + # Create a mock provider to verify flush is called + mock_provider = Mock() + mock_provider.force_flush = Mock() + processor = StructlogHandler(logger_provider=mock_provider) + + processor.flush() + + # Give the thread a moment to execute + time.sleep(0.1) + + # Verify force_flush was called + mock_provider.force_flush.assert_called_once() + + +class TestStructlogInstrumentor(TestBase): + """Tests for the StructlogInstrumentor class.""" + + def setUp(self): + super().setUp() + # Store original structlog config + self.original_config = structlog.get_config() + # Set up in-memory log exporter + self.exporter = InMemoryLogRecordExporter() + self.logger_provider = LoggerProvider() + self.logger_provider.add_log_record_processor( + SimpleLogRecordProcessor(self.exporter) + ) + + def tearDown(self): + super().tearDown() + # Uninstrument if needed + if StructlogInstrumentor()._is_instrumented_by_opentelemetry: + StructlogInstrumentor().uninstrument() + # Reset structlog + structlog.reset_defaults() + self.exporter.clear() + + def test_instrument_adds_processor(self): + """Test that instrument() adds the OTel handler to the chain.""" + # Configure structlog with a simple processor chain + structlog.configure( + processors=[ + structlog.dev.ConsoleRenderer(), + ] + ) + + # Get initial processor count + initial_processors = structlog.get_config()["processors"] + initial_count = len(initial_processors) + + # Instrument + StructlogInstrumentor().instrument( + logger_provider=self.logger_provider + ) + + # Check that processor was added + new_processors = structlog.get_config()["processors"] + self.assertEqual(len(new_processors), initial_count + 1) + + # Check that a StructlogHandler is in the chain + has_otel_handler = any( + isinstance(p, StructlogHandler) for p in new_processors + ) + self.assertTrue(has_otel_handler) + + def test_uninstrument_removes_processor(self): + """Test that uninstrument() removes the OTel handler.""" + # Configure structlog + structlog.configure( + processors=[ + structlog.dev.ConsoleRenderer(), + ] + ) + + # Instrument + StructlogInstrumentor().instrument( + logger_provider=self.logger_provider + ) + + # Verify handler was added + config_after_instrument = structlog.get_config()["processors"] + has_otel = any( + isinstance(p, StructlogHandler) for p in config_after_instrument + ) + self.assertTrue(has_otel) + + # Uninstrument + StructlogInstrumentor().uninstrument() + + # Verify handler was removed + config_after_uninstrument = structlog.get_config()["processors"] + has_otel = any( + isinstance(p, StructlogHandler) for p in config_after_uninstrument + ) + self.assertFalse(has_otel) + + def test_double_instrument_prevented(self): + """Test that double instrumentation is prevented by BaseInstrumentor.""" + structlog.configure( + processors=[ + structlog.dev.ConsoleRenderer(), + ] + ) + + # First instrumentation + StructlogInstrumentor().instrument( + logger_provider=self.logger_provider + ) + config_after_first = structlog.get_config()["processors"] + count_after_first = len(config_after_first) + + # Second instrumentation (should be no-op) + StructlogInstrumentor().instrument( + logger_provider=self.logger_provider + ) + config_after_second = structlog.get_config()["processors"] + count_after_second = len(config_after_second) + + # Should have same number of processors + self.assertEqual(count_after_first, count_after_second) + + def test_custom_logger_provider(self): + """Test that custom logger_provider is passed to the processor.""" + custom_provider = LoggerProvider() + custom_exporter = InMemoryLogRecordExporter() + custom_provider.add_log_record_processor( + SimpleLogRecordProcessor(custom_exporter) + ) + + structlog.configure( + processors=[ + structlog.dev.ConsoleRenderer(), + ] + ) + + # Instrument with custom provider + StructlogInstrumentor().instrument(logger_provider=custom_provider) + + # Log something + logger = structlog.get_logger() + logger.info("test message") + + # Verify it went to the custom exporter + logs = custom_exporter.get_finished_logs() + self.assertEqual(len(logs), 1) + self.assertEqual(logs[0].log_record.body, "test message") + + def test_configure_after_instrument_preserves_handler(self): + """Test that calling structlog.configure() after instrumentation preserves the handler.""" + StructlogInstrumentor().instrument( + logger_provider=self.logger_provider + ) + + # Simulate user code calling structlog.configure after instrumentation + structlog.configure( + processors=[ + structlog.dev.ConsoleRenderer(), + ] + ) + + processors = structlog.get_config()["processors"] + has_otel_handler = any( + isinstance(p, StructlogHandler) for p in processors + ) + self.assertTrue(has_otel_handler) + + def test_uninstrument_restores_configure(self): + """Test that uninstrument() restores the original structlog.configure.""" + original_configure = structlog.configure + + StructlogInstrumentor().instrument( + logger_provider=self.logger_provider + ) + self.assertIsNot(structlog.configure, original_configure) + + StructlogInstrumentor().uninstrument() + self.assertIs(structlog.configure, original_configure) + + def test_instrumentation_dependencies(self): + """Test that instrumentation_dependencies returns the correct value.""" + instrumentor = StructlogInstrumentor() + deps = instrumentor.instrumentation_dependencies() + self.assertIn("structlog", " ".join(deps)) diff --git a/opentelemetry-contrib-instrumentations/pyproject.toml b/opentelemetry-contrib-instrumentations/pyproject.toml index 630509a4b2..4b79e902a1 100644 --- a/opentelemetry-contrib-instrumentations/pyproject.toml +++ b/opentelemetry-contrib-instrumentations/pyproject.toml @@ -72,6 +72,7 @@ dependencies = [ "opentelemetry-instrumentation-sqlalchemy==0.63b0.dev", "opentelemetry-instrumentation-sqlite3==0.63b0.dev", "opentelemetry-instrumentation-starlette==0.63b0.dev", + "opentelemetry-instrumentation-structlog==0.63b0.dev", "opentelemetry-instrumentation-system-metrics==0.63b0.dev", "opentelemetry-instrumentation-threading==0.63b0.dev", "opentelemetry-instrumentation-tornado==0.63b0.dev", diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py index 999181bd9f..45e7bca192 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py @@ -192,6 +192,10 @@ "library": "starlette >= 0.13", "instrumentation": "opentelemetry-instrumentation-starlette==0.63b0.dev", }, + { + "library": "structlog >= 21.1", + "instrumentation": "opentelemetry-instrumentation-structlog==0.63b0.dev", + }, { "library": "psutil >= 5", "instrumentation": "opentelemetry-instrumentation-system-metrics==0.63b0.dev", diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/log_utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/log_utils.py new file mode 100644 index 0000000000..8ada6c4c97 --- /dev/null +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/log_utils.py @@ -0,0 +1,76 @@ +# 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 opentelemetry._logs import SeverityNumber + +_STD_TO_OTEL = { + 10: SeverityNumber.DEBUG, + 11: SeverityNumber.DEBUG2, + 12: SeverityNumber.DEBUG3, + 13: SeverityNumber.DEBUG4, + 14: SeverityNumber.DEBUG4, + 15: SeverityNumber.DEBUG4, + 16: SeverityNumber.DEBUG4, + 17: SeverityNumber.DEBUG4, + 18: SeverityNumber.DEBUG4, + 19: SeverityNumber.DEBUG4, + 20: SeverityNumber.INFO, + 21: SeverityNumber.INFO2, + 22: SeverityNumber.INFO3, + 23: SeverityNumber.INFO4, + 24: SeverityNumber.INFO4, + 25: SeverityNumber.INFO4, + 26: SeverityNumber.INFO4, + 27: SeverityNumber.INFO4, + 28: SeverityNumber.INFO4, + 29: SeverityNumber.INFO4, + 30: SeverityNumber.WARN, + 31: SeverityNumber.WARN2, + 32: SeverityNumber.WARN3, + 33: SeverityNumber.WARN4, + 34: SeverityNumber.WARN4, + 35: SeverityNumber.WARN4, + 36: SeverityNumber.WARN4, + 37: SeverityNumber.WARN4, + 38: SeverityNumber.WARN4, + 39: SeverityNumber.WARN4, + 40: SeverityNumber.ERROR, + 41: SeverityNumber.ERROR2, + 42: SeverityNumber.ERROR3, + 43: SeverityNumber.ERROR4, + 44: SeverityNumber.ERROR4, + 45: SeverityNumber.ERROR4, + 46: SeverityNumber.ERROR4, + 47: SeverityNumber.ERROR4, + 48: SeverityNumber.ERROR4, + 49: SeverityNumber.ERROR4, + 50: SeverityNumber.FATAL, + 51: SeverityNumber.FATAL2, + 52: SeverityNumber.FATAL3, + 53: SeverityNumber.FATAL4, +} + + +def std_to_otel(levelno: int) -> SeverityNumber: + """ + Map a Python log level number as defined in + https://docs.python.org/3/library/logging.html#logging-levels + to an OTel log severity number as defined in + https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-severitynumber + """ + if levelno < 10: + return SeverityNumber.UNSPECIFIED + if levelno > 53: + return SeverityNumber.FATAL4 + return _STD_TO_OTEL[levelno] diff --git a/pyproject.toml b/pyproject.toml index 92cfe8b5d2..c40d21cf55 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,6 +54,7 @@ dependencies = [ "opentelemetry-instrumentation-requests[instruments]", "opentelemetry-instrumentation-sqlalchemy[instruments]", "opentelemetry-instrumentation-sqlite3", + "opentelemetry-instrumentation-structlog[instruments]", "opentelemetry-instrumentation-system-metrics", "opentelemetry-instrumentation-threading", "opentelemetry-instrumentation-tornado", @@ -126,6 +127,7 @@ opentelemetry-instrumentation-remoulade = { workspace = true } opentelemetry-instrumentation-requests = { workspace = true } opentelemetry-instrumentation-sqlalchemy = { workspace = true } opentelemetry-instrumentation-sqlite3 = { workspace = true } +opentelemetry-instrumentation-structlog = { workspace = true } opentelemetry-instrumentation-system-metrics = { workspace = true } opentelemetry-instrumentation-threading = { workspace = true } opentelemetry-instrumentation-tornado = { workspace = true } diff --git a/tox.ini b/tox.ini index 1f2ca7f8e9..3ca857f241 100644 --- a/tox.ini +++ b/tox.ini @@ -194,6 +194,11 @@ envlist = pypy3-test-instrumentation-starlette-{oldest,latest} lint-instrumentation-starlette + ; opentelemetry-instrumentation-structlog + py3{10,11,12,13,14}-test-instrumentation-structlog + pypy3-test-instrumentation-structlog + lint-instrumentation-structlog + ; opentelemetry-instrumentation-jinja2 py3{10,11,12,13,14}-test-instrumentation-jinja2 pypy3-test-instrumentation-jinja2 @@ -676,6 +681,9 @@ deps = starlette-{oldest,lint}: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-starlette/test-requirements.oldest.txt starlette-latest: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-starlette/test-requirements.latest.txt + structlog: {[testenv]test_deps} + structlog: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-structlog/test-requirements.txt + system-metrics: {[testenv]test_deps} system-metrics: -r {toxinidir}/instrumentation/opentelemetry-instrumentation-system-metrics/test-requirements.txt @@ -968,6 +976,9 @@ commands = test-instrumentation-starlette: pytest {toxinidir}/instrumentation/opentelemetry-instrumentation-starlette/tests {posargs} lint-instrumentation-starlette: sh -c "cd instrumentation && pylint --rcfile ../.pylintrc opentelemetry-instrumentation-starlette" + test-instrumentation-structlog: pytest {toxinidir}/instrumentation/opentelemetry-instrumentation-structlog/tests {posargs} + lint-instrumentation-structlog: sh -c "cd instrumentation && pylint --rcfile ../.pylintrc opentelemetry-instrumentation-structlog" + 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" diff --git a/uv.lock b/uv.lock index 14d5ea35b6..6ef737a196 100644 --- a/uv.lock +++ b/uv.lock @@ -65,6 +65,7 @@ members = [ "opentelemetry-instrumentation-sqlalchemy", "opentelemetry-instrumentation-sqlite3", "opentelemetry-instrumentation-starlette", + "opentelemetry-instrumentation-structlog", "opentelemetry-instrumentation-system-metrics", "opentelemetry-instrumentation-threading", "opentelemetry-instrumentation-tornado", @@ -3996,6 +3997,29 @@ requires-dist = [ ] provides-extras = ["instruments"] +[[package]] +name = "opentelemetry-instrumentation-structlog" +source = { editable = "instrumentation/opentelemetry-instrumentation-structlog" } +dependencies = [ + { name = "opentelemetry-api" }, + { name = "opentelemetry-instrumentation" }, + { name = "opentelemetry-semantic-conventions" }, +] + +[package.optional-dependencies] +instruments = [ + { name = "structlog" }, +] + +[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 = "structlog", marker = "extra == 'instruments'", specifier = ">=21.1" }, +] +provides-extras = ["instruments"] + [[package]] name = "opentelemetry-instrumentation-system-metrics" source = { editable = "instrumentation/opentelemetry-instrumentation-system-metrics" } @@ -4282,6 +4306,7 @@ dependencies = [ { name = "opentelemetry-instrumentation-requests", extra = ["instruments"] }, { name = "opentelemetry-instrumentation-sqlalchemy", extra = ["instruments"] }, { name = "opentelemetry-instrumentation-sqlite3" }, + { name = "opentelemetry-instrumentation-structlog", extra = ["instruments"] }, { name = "opentelemetry-instrumentation-system-metrics" }, { name = "opentelemetry-instrumentation-threading" }, { name = "opentelemetry-instrumentation-tornado" }, @@ -4356,6 +4381,7 @@ requires-dist = [ { name = "opentelemetry-instrumentation-requests", extras = ["instruments"], editable = "instrumentation/opentelemetry-instrumentation-requests" }, { name = "opentelemetry-instrumentation-sqlalchemy", extras = ["instruments"], editable = "instrumentation/opentelemetry-instrumentation-sqlalchemy" }, { name = "opentelemetry-instrumentation-sqlite3", editable = "instrumentation/opentelemetry-instrumentation-sqlite3" }, + { name = "opentelemetry-instrumentation-structlog", extras = ["instruments"], editable = "instrumentation/opentelemetry-instrumentation-structlog" }, { name = "opentelemetry-instrumentation-system-metrics", editable = "instrumentation/opentelemetry-instrumentation-system-metrics" }, { name = "opentelemetry-instrumentation-threading", editable = "instrumentation/opentelemetry-instrumentation-threading" }, { name = "opentelemetry-instrumentation-tornado", editable = "instrumentation/opentelemetry-instrumentation-tornado" }, @@ -5892,6 +5918,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/81/0d/13d1d239a25cbfb19e740db83143e95c772a1fe10202dda4b76792b114dd/starlette-0.52.1-py3-none-any.whl", hash = "sha256:0029d43eb3d273bc4f83a08720b4912ea4b071087a3b48db01b7c839f7954d74", size = 74272, upload-time = "2026-01-18T13:34:09.188Z" }, ] +[[package]] +name = "structlog" +version = "21.5.0" +source = { registry = "https://pypi.org/simple/" } +sdist = { url = "https://files.pythonhosted.org/packages/ea/77/e38019e698b0c0134f903ab40e87f0975813ca7f74dad287272788134f03/structlog-21.5.0.tar.gz", hash = "sha256:68c4c29c003714fe86834f347cb107452847ba52414390a7ee583472bde00fc9", size = 473089, upload-time = "2021-12-16T12:48:11.976Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/4b/a2/7c5d78fa0b6fc4f249d0a732811b2cef979d16677e21f10637bbedcd0e6a/structlog-21.5.0-py3-none-any.whl", hash = "sha256:fd7922e195262b337da85c2a91c84be94ccab1f8fd1957bd6986f6904e3761c8", size = 53394, upload-time = "2021-12-16T12:48:09.837Z" }, +] + [[package]] name = "tenacity" version = "9.1.4"