Skip to content

Commit fa91e41

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

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-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,14 +42,16 @@ 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
5252
([#3842](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3842))
53+
- `opentelemetry-instrumentation-mysqlclient`: Pass all keyword parameters
54+
([#3950](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3950))
5355

5456
## Version 1.38.0/0.59b0 (2025-10-16)
5557

@@ -72,7 +74,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7274
([#3743](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3743))
7375
- Add `rstcheck` to pre-commit to stop introducing invalid RST
7476
([#3777](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3777))
75-
- `opentelemetry-exporter-credential-provider-gcp`: create this package which provides support for supplying your machine's Application Default
77+
- `opentelemetry-exporter-credential-provider-gcp`: create this package which provides support for supplying your machine's Application Default
7678
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).
7779
- `opentelemetry-instrumentation-psycopg`: Add missing parameter `capture_parameters` to instrumentor.
7880
([#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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,29 @@ 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_not_specified(
528+
self,
529+
_mock_connect,
530+
mock_wrap_connect,
531+
):
532+
instrumentor = MySQLClientInstrumentor()
533+
instrumentor.instrument(capture_parameters=True)
534+
kwargs = mock_wrap_connect.call_args[1]
535+
self.assertTrue(kwargs["capture_parameters"])

0 commit comments

Comments
 (0)