dd-trace-py icon indicating copy to clipboard operation
dd-trace-py copied to clipboard

Slower pymysql module patching performance

Open baraklevy20 opened this issue 1 year ago • 1 comments

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.

baraklevy20 avatar Dec 08 '23 21:12 baraklevy20

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

cc @P403n1x87

emmettbutler avatar Dec 11 '23 18:12 emmettbutler

@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

xanderhades avatar Apr 09 '24 16:04 xanderhades

cc @duncanista

emmettbutler avatar Apr 09 '24 17:04 emmettbutler