dd-trace-py icon indicating copy to clipboard operation
dd-trace-py copied to clipboard

pymongo instrumentation issue with ddtrace 2.14.x: AttributeError: 'NoneType' object has no attribute 'onto'

Open pg-synth opened this issue 1 year ago • 1 comments

Hello,

We recently tried to bump our ddtrace lib from 2.9.x to 2.14 and encountered the following issue with pymongo (no other change were made beside the lib bump.

AttributeError: 'NoneType' object has no attribute 'onto'

We have the feeling it's a regression that appeared with the latest version.

Error details

Stack trace:

[....]
File "/home/lambda/pymongo/cursor.py", line 1243, in next
    if len(self.__data) or self._refresh():
  File "/home/lambda/pymongo/cursor.py", line 1160, in _refresh
    self.__send_message(q)
  File "/home/lambda/pymongo/cursor.py", line 1039, in __send_message
    response = client._run_operation(
  File "/home/lambda/pymongo/_csot.py", line 108, in csot_wrapper
    return func(self, *args, **kwargs)
  File "/home/lambda/pymongo/mongo_client.py", line 1431, in _run_operation
    return self._retryable_read(
  File "/home/lambda/pymongo/mongo_client.py", line 1540, in _retryable_read
    return self._retry_internal(
  File "/home/lambda/pymongo/_csot.py", line 108, in csot_wrapper
    return func(self, *args, **kwargs)
  File "/home/lambda/pymongo/mongo_client.py", line 1507, in _retry_internal
    ).run()
  File "/home/lambda/pymongo/mongo_client.py", line 2353, in run
    return self._read() if self._is_read else self._write()
  File "/home/lambda/pymongo/mongo_client.py", line 2483, in _read
    self._server = self._get_server()
  File "/home/lambda/pymongo/mongo_client.py", line 2439, in _get_server
    return self._client._select_server(
  File "/home/lambda/pymongo/mongo_client.py", line 1322, in _select_server
    server = topology.select_server(
  File "/home/lambda/pymongo/topology.py", line 358, in select_server
    def select_server(
  File "/home/lambda/ddtrace/contrib/internal/pymongo/client.py", line 104, in _trace_topology_select_server
    ddtrace.Pin.get_from(topology_instance).onto(server)
AttributeError: 'NoneType' object has no attribute 'onto'

Original lib code line in question

def _trace_topology_select_server(func, args, kwargs):
    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)
    return server

Instrumentation

Our bits of code patching pymongo and setting an override for the service name is very vanilla and follows the documentation recommendations:

import pymongo
from ddtrace import Pin, patch

from XXXX.mongodb.config import config

patch(pymongo=True)

client = pymongo.MongoClient(
    *config["client_args"],
    **config["client_kwargs"],
    uuidrepresentation="standard",
)

pin = Pin.override(client, service="XXX-CUSTOM")

db = client[config["database"]]
return_document = pymongo.ReturnDocument

def get_database():
    return client[config["database"]]

Other informations

Which version of dd-trace-py are you using?

2.14.2

Which libraries and their versions are you using?

pymongo: 4.8.0 datadog-lambda: 6.99.0

How can we reproduce your problem?

patch and pin with pymongo 4.8

pg-synth avatar Oct 16 '24 08:10 pg-synth

Hello, thank you for submitting this issue!

Could you please provide a full reproduction? I wasn't able to reproduce the error with the provided snippet.

quinna-h avatar Oct 23 '24 18:10 quinna-h

Hello, we have the same issue after upgrading to ddtrace==2.14.4 and pymongo==4.10.1. I am not sure if I would be able to provide the exact steps to reproduce this issue.

DSNortsev avatar Oct 28 '24 23:10 DSNortsev

Hi @DSNortsev @pg-synth ,

We recently merged a fix into ddtrace 2.15 and 2.14. Please feel free to re-open this issue or open a new issue if you are still facing issues!

quinna-h avatar Nov 01 '24 16:11 quinna-h

@quinna-h the issue is still reproducible with ddtrace 2.15.1. The producer can be found in the description of https://github.com/DataDog/dd-trace-py/issues/11287.

iherasymenko avatar Nov 04 '24 22:11 iherasymenko

@iherasymenko Responded on linked issue, this change has not been yet been released but will be released in the next patch releases.

quinna-h avatar Nov 04 '24 22:11 quinna-h