Skip to content

dbapi instrumentation breaks psycopg2 when registering types. #1077

Closed
@lkiss80

Description

@lkiss80

Describe your environment
I'm using dbapi instrumentation to instrument postgresql. When I attempt to register jsonb, psycopg2 crashes. This issue is nearly identical to #143, with the exception that I'm not using Psycopg2Instrumentor.

The reason I'm using dbapi instrumentation vs Psycopg2Instrumentor is because the latter does not support capturing parameters as span attributes.

Steps to reproduce

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.instrumentation import dbapi as dbapi_instrumentor
import psycopg2
import psycopg2.extras

trace.set_tracer_provider(TracerProvider())
db_connection_attributes = {
    "database": "info.dbname",
    "port": "info.port",
    "host": "info.host",
    "user": "info.user",
}
dbapi_instrumentor.trace_integration(
    psycopg2,
    "connect",
    "postgresql",
    db_connection_attributes,
    capture_parameters=True,
)

cnx = psycopg2.connect(host='localhost', port='5432', dbname='postgres', password='1234', user='postgres')
psycopg2.extras.register_default_jsonb(conn_or_curs=cnx, loads=lambda x: x)

results in

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/psycopg2/_json.py", line 156, in register_default_jsonb
    loads=loads, oid=JSONB_OID, array_oid=JSONBARRAY_OID, name='jsonb')
  File "/usr/local/lib/python3.7/site-packages/psycopg2/_json.py", line 125, in register_json
    register_type(JSON, not globally and conn_or_curs or None)
TypeError: argument 2 must be a connection, cursor or None

What is the expected behavior?
For psycopg2 to not crash when used with dbapi instrumentation.

What is the actual behavior?
It crashes when psycopg2 enforces connection or cursor types.

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

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