apm-agent-python icon indicating copy to clipboard operation
apm-agent-python copied to clipboard

psycopg pool not instrumented

Open plexaikm opened this issue 1 year ago • 3 comments

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
    

plexaikm avatar Aug 01 '24 06:08 plexaikm

Thanks for reporting and for providing a reproducer and even a workaround. Proper instrumentation would be indeed better.

xrmx avatar Aug 01 '24 09:08 xrmx

Any updates on this?

xflashxx avatar Aug 19 '25 14:08 xflashxx

Hi, I’ve created a PR for this issue!

  • https://github.com/elastic/apm-agent-python/pull/2460

JaeHyuckSa avatar Oct 20 '25 08:10 JaeHyuckSa