Skip to content

psycopg pool not instrumented #2094

Open

Description

We use psycopg connection pool in out FastAPI auto instrumented component,
previously we used psycopg.connect directly without connection pool and PG spans were properly reported
after switching to connection pool we no longer generated PG spans...

Investigated the auto instrumentation, it binds on psycopg.connect,
however the connection pool in pool.py/_connect line 598 method use the Connection class connect method directly bypassing the instrumented instruction...

conn = self.connection_class.connect(self.conninfo, **kwargs)

Local (and quite ugly) fix is to provide ConnectionPool a custom connection_class using the instrumented psycopg.connect method

class WrapperConnectionClass(Connection):
  
  @classmethod
  def connect(
    cls,
    conninfo: str = "",
    **kwargs: Union[None, int, str],
  ) --> "Connection[Row]":
    
    return psycopg.connect(conninfo, **kwargs)

...
pool = ConnectionPool(
  ...
  connection_class=WrapperConnectionClass,
  ...
)

This workaround solves the problem locally and we now generate PG spans, but a proper instrumentation solution will be better

To Reproduce

import psycopg
from psycopg_pool import ConnectionPool
import elasticapm

apm_client = elasticapm.Client()
elasticapm.instrument()

pool = ConnectionPool(
  "... conn info ..",
  min_size=1,
  max_size=8,
  check=ConnectionPool.check_connection
)
pool.wait()

with pool.connection() as conn:
  with conn.cursor() as curr:
    curr.execute("SELECT VERSION()")
    curr.fetchone()

Environment (please complete the following information)

  • OS: Ubuntu 20.04.6 LTS
  • Python version: 3.10.11
  • Framework and version: FastAPI 0.95.2
  • APM Server version: 8.14.1
  • Agent version: 6.22.3

Additional context

Add any other context about the problem here.

  • Agent config options

    Click to expand
    default
    
  • requirements.txt:

    Click to expand
    elastic-apm==6.22.3
    psycopg[binary,pool]==3.1.19
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions