DataDog / dd-trace-py

Datadog Python APM Client
https://ddtrace.readthedocs.io/
Other
553 stars 415 forks source link

Slower pymysql module patching performance #7882

Open baraklevy20 opened 11 months ago

baraklevy20 commented 11 months ago

Summary of problem

It seems like patching pymysql in version 2.3.1 takes a lot longer now.

Example code:

from ddtrace import patch
patch(pymysql=True)

import time
start_time = time.time()

import pymysql

end_time = time.time()
print("pymysql import time: %s" % (end_time - start_time))

Running this on any version >= 1.19.0 (I'm using 2.3.1) shows:

pymysql import time: 0.40660572052001953
pymysql import time: 0.4103705883026123
pymysql import time: 0.4106619358062744
pymysql import time: 0.41042542457580566
pymysql import time: 0.4061594009399414

Running this on version 1.18.8 shows:

pymysql import time: 0.020429372787475586
pymysql import time: 0.020166873931884766
pymysql import time: 0.020434141159057617
pymysql import time: 0.020166873931884766
pymysql import time: 0.020166873931884766

If it helps, it seems like imported_module = importlib.import_module(path) in ddtrace/_monkey.py is taking 0.4s now.

I'm using short-lived instrumented processes so this additional load time adds up quickly. Would appreciate any help here. Thank you!

Which version of dd-trace-py are you using?

2.3.1

Which version of pip are you using?

23.3.1

Which libraries and their versions are you using?

pymysql==1.1.0

`pip freeze`

How can we reproduce your problem?

from ddtrace import patch
patch(pymysql=True)

import pymysql

What is the result that you get?

Slow performance.

What is the result that you expected?

Much faster performance.

emmettbutler commented 11 months ago

Thanks for bringing this to our attention, @baraklevy20. We'll look into it.

cc @P403n1x87

xanderhades commented 7 months ago

@emmettbutler @baraklevy20

We've had this issue in production on some AWS Lambdas

Using these old dependencies:

pymysql==1.1.0 # latest
datadog_lambda==3.56.0  # pulls ddtrace==0.59.2

Extremely fast results:

python app.py
pymysql import time: 0.00000095367431640625

Using latest:

pymysql==1.1.0 # latest
datadog_lambda==5.92.0 # latest - pulls ddtrace 2.8.0

Extremely slow (by comparison):

python app.py
pymysql import time: 0.02929711341857910
emmettbutler commented 7 months ago

cc @duncanista

TonySherman commented 2 months ago

Any updates on this issue? Still seems to be affecting pymysql connections.