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

psycopg pool not instrumented #2094

Open
plexaikm opened this issue Aug 1, 2024 · 1 comment
Open

psycopg pool not instrumented #2094

plexaikm opened this issue Aug 1, 2024 · 1 comment
Labels
agent-python community Issues opened by the community triage Issues awaiting triage

Comments

@plexaikm
Copy link

plexaikm commented Aug 1, 2024

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
    
@github-actions github-actions bot added agent-python community Issues opened by the community triage Issues awaiting triage labels Aug 1, 2024
@xrmx
Copy link
Member

xrmx commented Aug 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-python community Issues opened by the community triage Issues awaiting triage
Projects
None yet
Development

No branches or pull requests

2 participants