Skip to content

Instantly share code, notes, and snippets.

@maxipavlovic
Created June 5, 2020 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxipavlovic/21f76b465a5d808ddf3c6092773c2f17 to your computer and use it in GitHub Desktop.
Save maxipavlovic/21f76b465a5d808ddf3c6092773c2f17 to your computer and use it in GitHub Desktop.
from django.db import connection
from opencensus.trace.execution_context import get_opencensus_tracer
class SQLCorrelationWrapper:
def __init__(self, trace_id):
self.trace_id = trace_id
def __call__(self, execute, sql, params, many, context):
if len(sql) > 6:
operation = sql[:6]
if operation in ('SELECT', 'INSERT', 'DELETE', 'UPDATE'):
sql = operation + r' /* {} */'.format(self.trace_id) + sql[6:]
execute(sql, params, many, context)
class SQLCorrelationMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
trace_id = get_opencensus_tracer().span_context.trace_id
with connection.execute_wrapper(SQLCorrelationWrapper(trace_id)):
response = self.get_response(request)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment