opencensus-python icon indicating copy to clipboard operation
opencensus-python copied to clipboard

Django DB Instrumentation problem

Open jordanorc opened this issue 6 years ago • 2 comments

I'm facing problems to tracing database queries. In the middleware.py file, execute_wrappers are updating to include _trace_db_call function here.

But when Django access execute_wrappers here, it seems to be empty. Looking closely, I've found different object instances when the execute_wrappers are updated at first (<django.db.backends.sqlite3.base.DatabaseWrapper object at 0x7fa410750240>), and when it is accessed after (<django.db.backends.sqlite3.base.DatabaseWrapper object at 0x7fa4103a24a8>). Seems that Django instantiate multiple DatabaseWrapper objects, causing the problem.

If I change the initialization of execute_wrappers to process_request it works, but only log one query:

    def process_request(self, request):
        """Called on each request, before Django decides which view to execute.

        :type request: :class:`~django.http.request.HttpRequest`
        :param request: Django http request.
        """
        if django.VERSION >= (2,) and not _trace_db_call in connection.execute_wrappers:
            connection.execute_wrappers.append(_trace_db_call)

My environment:

Package                  Version  
------------------------ ---------
cachetools               3.1.1    
certifi                  2019.9.11
chardet                  3.0.4    
contextvars              2.4      
Django                   2.0      
google-api-core          1.14.3   
google-auth              1.7.0    
googleapis-common-protos 1.6.0    
idna                     2.8      
immutables               0.11     
mysql                    0.0.2    
mysql-connector          2.2.9    
mysqlclient              1.4.4    
opencensus               0.7.5    
opencensus-context       0.1.1    
opencensus-ext-dbapi     0.1.2    
opencensus-ext-django    0.7.2    
opencensus-ext-mysql     0.1.2    
opencensus-ext-zipkin    0.2.2    
pip                      19.3.1   
pkg-resources            0.0.0    
protobuf                 3.10.0   
pyasn1                   0.4.7    
pyasn1-modules           0.2.7    
PyMySQL                  0.9.3    
pytz                     2019.3   
requests                 2.22.0   
rsa                      4.0      
setuptools               39.0.1   
six                      1.12.0   
sqlparse                 0.3.0    
urllib3                  1.25.6

I'm trying to use it in the example included on plugin (https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-django).

I just change settings to use SQLITE:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, '../db.sqlite3'),
    }
}

I'm doing something wrong?

jordanorc avatar Nov 06 '19 10:11 jordanorc

I also try running it with daphne, which is the server we use to deploy our solution and the problem still persists.

jordanorc avatar Nov 06 '19 10:11 jordanorc

I found out sync worker ok. async worker not.

jiamo avatar Nov 15 '21 06:11 jiamo