Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pymongo Invalid type in attribute sequence when using sessions #1918

Open
sanzoghenzo opened this issue Aug 22, 2023 · 2 comments · May be fixed by #2010
Open

pymongo Invalid type in attribute sequence when using sessions #1918

sanzoghenzo opened this issue Aug 22, 2023 · 2 comments · May be fixed by #2010
Labels
bug Something isn't working

Comments

@sanzoghenzo
Copy link

#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

@sanzoghenzo sanzoghenzo added the bug Something isn't working label Aug 22, 2023
@WolfEYc
Copy link

WolfEYc commented Jun 27, 2024

+1

@UltimateLobster
Copy link

+1, this seems like an easy fix. I wonder why the PR was not merged even a year later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants