From 631988d8f3c15de942c2d58e22b372fe4f8126df Mon Sep 17 00:00:00 2001 From: Quinna Halim Date: Wed, 30 Oct 2024 10:52:49 -0400 Subject: [PATCH] fix(pymongo): add pin type check (#11145) Small type check fix for expected `Pin` type to unblock #11041 ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) (cherry picked from commit f3b5275b8a6be8d3b7c8ab1e4e40ed96cb9aa386) --- ddtrace/contrib/internal/pymongo/client.py | 5 ++++- releasenotes/notes/pymongo-check-pin-c3cb9d1fbdc82fb3.yaml | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/pymongo-check-pin-c3cb9d1fbdc82fb3.yaml diff --git a/ddtrace/contrib/internal/pymongo/client.py b/ddtrace/contrib/internal/pymongo/client.py index f8441f3091b..d9a59bac945 100644 --- a/ddtrace/contrib/internal/pymongo/client.py +++ b/ddtrace/contrib/internal/pymongo/client.py @@ -101,7 +101,10 @@ def _trace_topology_select_server(func, args, kwargs): # Ensure the pin used on the traced mongo client is passed down to the topology instance # This allows us to pass the same pin in traced server objects. topology_instance = get_argument_value(args, kwargs, 0, "self") - ddtrace.Pin.get_from(topology_instance).onto(server) + pin = ddtrace.Pin.get_from(topology_instance) + + if pin is not None: + pin.onto(server) return server diff --git a/releasenotes/notes/pymongo-check-pin-c3cb9d1fbdc82fb3.yaml b/releasenotes/notes/pymongo-check-pin-c3cb9d1fbdc82fb3.yaml new file mode 100644 index 00000000000..1f1ad12a669 --- /dev/null +++ b/releasenotes/notes/pymongo-check-pin-c3cb9d1fbdc82fb3.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + pymongo: add type checking to solve an issue where ``NoneType`` instead of expected ``Pin`` object would throw an error in ``TracedTopology`` method.