Skip to content

Commit afdc78e

Browse files
mysqlclient instrumentor: improved keyword argument handling (#2894)
- forward "all" keyword arguments to wrap_connect() - keep defaults as defined previously
1 parent 96a9d0f commit afdc78e

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
1212
## Unreleased
1313

14-
### Added
14+
### Added
1515

1616
- `opentelemetry-instrumentation-aiohttp-client`: add support for url exclusions via `OTEL_PYTHON_EXCLUDED_URLS` / `OTEL_PYTHON_AIOHTTP_CLIENT_EXCLUDED_URLS`
1717
([#3850](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3850))
@@ -42,10 +42,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4242
([#3882](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3882))
4343
- `opentelemetry-instrumentation-aiohttp-server`: delay initialization of tracer, meter and excluded urls to instrumentation for testability
4444
([#3836](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3836))
45-
- Replace Python 3.14-deprecated `asyncio.iscoroutinefunction` with `inspect.iscoroutinefunction`.
45+
- Replace Python 3.14-deprecated `asyncio.iscoroutinefunction` with `inspect.iscoroutinefunction`.
4646
([#3880](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3880))
4747
- `opentelemetry-instrumentation-elasticsearch`: Enhance elasticsearch query body sanitization
48-
([#3919](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3919))
48+
([#3919](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3919))
4949
- `opentelemetry-instrumentation-pymongo`: Fix span error descriptions
5050
([#3904](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3904))
5151
- build: bump ruff to 0.14.1
@@ -54,6 +54,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5454
([#3941](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3941))
5555
- `opentelemetry-instrumentation-pymongo`: Fix invalid mongodb collection attribute type
5656
([#3942](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3942))
57+
- `opentelemetry-instrumentation-mysqlclient`: Pass all keyword parameters
58+
([#3950](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3950))
5759

5860
## Version 1.38.0/0.59b0 (2025-10-16)
5961

@@ -76,7 +78,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7678
([#3743](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3743))
7779
- Add `rstcheck` to pre-commit to stop introducing invalid RST
7880
([#3777](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3777))
79-
- `opentelemetry-exporter-credential-provider-gcp`: create this package which provides support for supplying your machine's Application Default
81+
- `opentelemetry-exporter-credential-provider-gcp`: create this package which provides support for supplying your machine's Application Default
8082
Credentials (https://cloud.google.com/docs/authentication/application-default-credentials) to the OTLP Exporters created automatically by OpenTelemetry Python's auto instrumentation. These credentials authorize OTLP traces to be sent to `telemetry.googleapis.com`. [#3766](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3766).
8183
- `opentelemetry-instrumentation-psycopg`: Add missing parameter `capture_parameters` to instrumentor.
8284
([#3676](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3676))

instrumentation/opentelemetry-instrumentation-mysqlclient/src/opentelemetry/instrumentation/mysqlclient/__init__.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ def _instrument(self, **kwargs): # pylint: disable=no-self-use
160160
"""Integrate with the mysqlclient library.
161161
https://github.com/PyMySQL/mysqlclient/
162162
"""
163-
tracer_provider = kwargs.get("tracer_provider")
164-
enable_sqlcommenter = kwargs.get("enable_commenter", False)
165-
commenter_options = kwargs.get("commenter_options", {})
166-
enable_attribute_commenter = kwargs.get(
167-
"enable_attribute_commenter", False
168-
)
163+
kwargs_with_defaults = {
164+
"tracer_provider": None,
165+
"enable_commenter": False,
166+
"commenter_options": {},
167+
"enable_attribute_commenter": False,
168+
**kwargs,
169+
}
169170

170171
dbapi.wrap_connect(
171172
__name__,
@@ -174,10 +175,7 @@ def _instrument(self, **kwargs): # pylint: disable=no-self-use
174175
_DATABASE_SYSTEM,
175176
_CONNECTION_ATTRIBUTES,
176177
version=__version__,
177-
tracer_provider=tracer_provider,
178-
enable_commenter=enable_sqlcommenter,
179-
commenter_options=commenter_options,
180-
enable_attribute_commenter=enable_attribute_commenter,
178+
**kwargs_with_defaults,
181179
)
182180

183181
def _uninstrument(self, **kwargs): # pylint: disable=no-self-use

instrumentation/opentelemetry-instrumentation-mysqlclient/tests/test_mysqlclient_integration.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,33 @@ def test_uninstrument_connection(self, mock_connect):
507507

508508
spans_list = self.memory_exporter.get_finished_spans()
509509
self.assertEqual(len(spans_list), 0)
510+
511+
@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
512+
@mock.patch("MySQLdb.connect")
513+
# pylint: disable=unused-argument
514+
def test_missing_capture_parameters_if_not_specified(
515+
self,
516+
_mock_connect,
517+
mock_wrap_connect,
518+
):
519+
instrumentor = MySQLClientInstrumentor()
520+
instrumentor.instrument()
521+
kwargs = mock_wrap_connect.call_args[1]
522+
self.assertNotIn("capture_parameters", kwargs)
523+
524+
@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
525+
@mock.patch("MySQLdb.connect")
526+
# pylint: disable=unused-argument
527+
def test_passes_capture_parameters_if_specified(
528+
self,
529+
_mock_connect,
530+
mock_wrap_connect,
531+
):
532+
"""
533+
MySQLClientInstrumentor.instrument should pass any provided,
534+
arbitrary kwargs to DB-API wrap_connect
535+
"""
536+
instrumentor = MySQLClientInstrumentor()
537+
instrumentor.instrument(capture_parameters=True)
538+
kwargs = mock_wrap_connect.call_args[1]
539+
self.assertTrue(kwargs["capture_parameters"])

0 commit comments

Comments
 (0)