Created
June 5, 2020 15:59
-
-
Save maxipavlovic/21f76b465a5d808ddf3c6092773c2f17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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