Correlation IDs in Django SQL
Tags: django, microservices, sql, python
Published: 2020-06-06 11:56:11

If you have more than one service in any of your projects, you should have probably heard of __Correlation ID__s already. You may know it under other names, but I am sure the concept sounds pretty familiar. Anyway, let me repeat it one more time for everyone to be on the same page.

Each good technical project writes all kind of operational logs. When there are several independent services, each service sends logs to some log aggregation system. The more services there are and the higher the load, the harder it becomes to figure out meaningful sequences of actions. We need some simple pattern to help us operate this mess. This pattern is called [Distributed Tracing](https://microservices.io/patterns/observability/distributed-tracing.html) and its core component is Correlation ID.

__Correlation ID__ (or Trace ID) is a unique identifier for a chain of events within a distributed system. When some distributed flow is started, it acquires a unique ID which is later propagated to all of the actors within the flow. As we are going to talk about [Django Web Framework](https://www.djangoproject.com/), we will assume that our main communication protocol is HTTP(s), which means that correlation ID is passed in some header. If you want to learn more on the topic, please, refer to the [trace context specification](https://www.w3.org/TR/trace-context/) by W3C.

Anyway, passing Correlation ID between services is great, but when it comes down to real problem solving, too often we find out that we need to map business flows to the exact technical details under the hood. In that case we need to pull the trace ID from headers and pass it to all the underlying layers, like service layer or ORM. For this article our key goal is to propagate this ID to the deepest possible layer — DB queries. We want SQL to contain this ID, as one API call may trigger several SQL queries and we want to correlate them also.

---

Let’s look which options we have for Django:
* [Django-Cid Library](https://github.com/Polyconseil/django-cid) (seems to have needed features, but is not widely used and tests are a big question)
* [Django Log Request Id](https://github.com/dabapps/django-log-request-id) (only logs, no SQL)
* Some DB specific things like [Django-MySQL](https://django-mysql.readthedocs.io/en/latest/queryset_extensions.html?highlight=label#django_mysql.models.label)

Hm, seems pretty scarce… What a perfect opportunity for writing code (🚲‍)!

---
We need to agree that we want *correlated* SQL only in request-response cycle. There are ways to pass correlation IDs to background processes like celery tasks which also use Django ORM, but we will leave this out of scope. Also we want the dumbest possible implementation to illustrate the idea ([KISS](https://en.wikipedia.org/wiki/KISS_principle)).

Thus, the best option is to use [Django Middleware](https://docs.djangoproject.com/en/3.0/topics/http/middleware/) to wrap all operations in one place. As we want to affect SQL we need to use [Django DB instrumentation](https://docs.djangoproject.com/en/3.0/topics/db/instrumentation/). Here is how our wrapper will look like:

{% gist https://gist.github.com/maxipavlovic/94d6c85457829d0293d1cc71eab5b698 %}

We are lucky that main SQL operators have the same length (__6__), so it’s easy to distinguish them from auxiliary things like `SET SESSION TRANSACTION`. Our correlation id will be placed right after the operator in SQL comment block `/* correlation_id */`.

> ⚠️ This code doesn’t seem like a production ready solution. Extend and test it OR use with caution.

Now let’s write the middleware:

{% gist https://gist.github.com/maxipavlovic/fec378994445b84cd8bf72a5c032bbba %}

Simple as that. In this example we extracted correlation ID from [OpenCensus](https://github.com/census-instrumentation/opencensus-python) context, but you could use any other library or simply generate uuid on the fly. In the next step we wrapped all our SQL operations with the above DB context wrapper.

> OpenCensus is a set of libraries for various languages that allow you to collect application metrics and distributed traces, then transfer the data to a backend of your choice in real time. This data can be analyzed by developers and admins to understand the health of the application and debug problems.

Don’t forget to add this middleware to Django __MIDDLEWARE__ setting (lower than your tracing middleware (!) if you have one). To test how it works, we can set __DEBUG__ level for django logs:

```
'loggers': {
    'django': {
        'handlers': ['console'],
        'level': 'DEBUG',
    },
}
```

And we will get the following result, when we go to Django admin:

```
2020-06-06 07:43:04,756 django.db.backends DEBUG (0.005) SELECT /* 89fc0ff588659981acaa114a743785d8 */ `django_admin_log`.`id`, `django_admin_log`.`action_time`, `django_admin_log`.`user_id`, `django_admin_log`.`content_type_id`, `django_admin_log`.`object_id`, `django_admin_log`.`object_repr`,....
```

And that’s it! Our SQLs have the same correlation ID that is used across all of our distributed services. Now it will be much easier to find out bugs and debug performance issues.

I guess, this is also a great example on why we ❤ Python and Django. Complex and useful things can be done in 10 minutes and 10 lines of code.

***

Thank you for you time and here is the [link](https://gist.github.com/maxipavlovic/21f76b465a5d808ddf3c6092773c2f17) to the whole code snippet. Stay healthy, inspired and 🤙 [me](https://www.linkedin.com/in/mxms/) if you have any questions.