Skip to content

pymongo Invalid type in attribute sequence when using sessions #1918

@sanzoghenzo

Description

@sanzoghenzo

#1555 introduced a bug when using pymongo sessions, the instrumentor now outputs the message

Invalid type dict in attribute value sequence. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or None

while exiting the session.
After some debugging, I realized that this id triggered by the "endSessions" command, that triggers the CommandStartedEvent with a SON command like this:

SON([('endSessions', [{'id': Binary(b'U\xf4\x19x\xdd\xd8FK\x80\x13\x97\xcc_U\xd5L', 4)}]), ('$db', 'admin')])

and the collection variable becomes a list of dictionaries instead of the expected string.

Describe your environment

  • python 3.10
  • windows 10, but also on linux/docker
  • pymongo 4.4.1
  • opentelemetry-instrumentation-pymongo 0.40b0
  • opentelemetry 1.19.0

Steps to reproduce

from opentelemetry import trace
from opentelemetry.instrumentation.pymongo import PymongoInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from pymongo import MongoClient

provider = TracerProvider()
processor = BatchSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)

PymongoInstrumentor().instrument()
client = MongoClient()
session = client.start_session()
RECORD = {"test": "123"}
db = client["MongoDB_Database"]
collection = db["MongoDB_Collection"]
collection.find_one(RECORD)
session.end_session()
client.close()

What is the expected behavior?
span.set_attribute(SpanAttributes.DB_MONGODB_COLLECTION) should not be called if the collection variable is not a string

What is the actual behavior?
The set_attribute method is called and the message above is output in the console.

Additional context
Another solution would be to check for the pymongo command names that doesn't have/need a collection name

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions