Skip to content

Commit 7ffbe30

Browse files
Improve Django sqlcomment readthedocs
1 parent 34db73e commit 7ffbe30

File tree

1 file changed

+48
-51
lines changed
  • instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django

1 file changed

+48
-51
lines changed

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/__init__.py

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -170,64 +170,61 @@ def response_hook(span, request, response):
170170
Note:
171171
The environment variable names used to capture HTTP headers are still experimental, and thus are subject to change.
172172
173-
SQLCOMMENTER
174-
*****************************************
175-
You can optionally configure Django instrumentation to enable sqlcommenter which enriches
176-
the query with contextual information.
173+
SQLCommenter
174+
************
175+
You can optionally enable sqlcommenter which enriches the query with contextual
176+
information. Queries made after setting up trace integration with sqlcommenter
177+
enabled will have configurable key-value pairs appended to them, e.g.
178+
``Users().objects.all()`` will result in
179+
``"select * from auth_users; /*traceparent=00-01234567-abcd-01*/"``. This
180+
supports context propagation between database client and server when database log
181+
records are enabled. For more information, see:
182+
183+
* `Semantic Conventions - Database Spans <https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#sql-commenter>`_
184+
* `sqlcommenter <https://google.github.io/sqlcommenter/>`_
177185
178186
.. code:: python
179187
180188
from opentelemetry.instrumentation.django import DjangoInstrumentor
181189
182190
DjangoInstrumentor().instrument(is_sql_commentor_enabled=True)
183191
184-
185-
For example,
186-
::
187-
188-
Invoking Users().objects.all() will lead to sql query "select * from auth_users" but when SQLCommenter is enabled
189-
the query will get appended with some configurable tags like "select * from auth_users /*metrics=value*/;"
190-
191-
192-
SQLCommenter Configurations
193-
***************************
194-
We can configure the tags to be appended to the sqlquery log by adding below variables to the settings.py
195-
196-
SQLCOMMENTER_WITH_FRAMEWORK = True(Default) or False
197-
198-
For example,
199-
::
200-
Enabling this flag will add django framework and it's version which is /*framework='django%3A2.2.3*/
201-
202-
SQLCOMMENTER_WITH_CONTROLLER = True(Default) or False
203-
204-
For example,
205-
::
206-
Enabling this flag will add controller name that handles the request /*controller='index'*/
207-
208-
SQLCOMMENTER_WITH_ROUTE = True(Default) or False
209-
210-
For example,
211-
::
212-
Enabling this flag will add url path that handles the request /*route='polls/'*/
213-
214-
SQLCOMMENTER_WITH_APP_NAME = True(Default) or False
215-
216-
For example,
217-
::
218-
Enabling this flag will add app name that handles the request /*app_name='polls'*/
219-
220-
SQLCOMMENTER_WITH_OPENTELEMETRY = True(Default) or False
221-
222-
For example,
223-
::
224-
Enabling this flag will add opentelemetry traceparent /*traceparent='00-fd720cffceba94bbf75940ff3caaf3cc-4fd1a2bdacf56388-01'*/
225-
226-
SQLCOMMENTER_WITH_DB_DRIVER = True(Default) or False
227-
228-
For example,
229-
::
230-
Enabling this flag will add name of the db driver /*db_driver='django.db.backends.postgresql'*/
192+
Warning:
193+
Duplicate sqlcomments may be appended to the sqlquery log if DjangoInstrumentor
194+
sqlcommenter is enabled in addition to sqlcommenter for an active instrumention
195+
of a database driver or object relation mapper (ORM) in the same database client
196+
stack. For example, if psycopg2 driver is used and Psycopg2Instrumentor has
197+
sqlcommenter enabled, then both DjangoInstrumentor and Psycopg2Instrumentor will
198+
append comments to the query statement.
199+
200+
SQLCommenter with commenter_options
201+
***********************************
202+
The key-value pairs appended to the query can be configured using
203+
variables in Django ``settings.py``. When sqlcommenter is enabled, all
204+
available KVs/tags are calculated by default, i.e. ``True`` for each. The
205+
``settings.py`` values support *opting out* of specific KVs.
206+
207+
Available settings.py commenter options
208+
#######################################
209+
210+
We can configure the tags to be appended to the sqlquery log by adding below variables to
211+
``settings.py``, e.g. ``SQLCOMMENTER_WITH_FRAMEWORK = False``
212+
213+
+-------------------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
214+
| ``settings.py`` variable | Description | Example |
215+
+=====================================+===========================================================+===========================================================================+
216+
| ``SQLCOMMENTER_WITH_FRAMEWORK`` | Django framework name with version (URL encoded). | ``framework='django%%%%3A4.2.0'`` |
217+
+-------------------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
218+
| ``SQLCOMMENTER_WITH_CONTROLLER`` | Django controller/view name that handles the request. | ``controller='index'`` |
219+
+-------------------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
220+
| ``SQLCOMMENTER_WITH_ROUTE`` | URL path pattern that handles the request. | ``route='polls/'`` |
221+
+-------------------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
222+
| ``SQLCOMMENTER_WITH_APP_NAME`` | Django app name that handles the request. | ``app_name='polls'`` |
223+
+-------------------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
224+
| ``SQLCOMMENTER_WITH_OPENTELEMETRY`` | OpenTelemetry context as traceparent at time of query. | ``traceparent='00-fd720cffceba94bbf75940ff3caaf3cc-4fd1a2bdacf56388-01'`` |
225+
+-------------------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
226+
| ``SQLCOMMENTER_WITH_DB_DRIVER`` | Database driver name used by Django. | ``db_driver='django.db.backends.postgresql'`` |
227+
+-------------------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
231228
232229
API
233230
---

0 commit comments

Comments
 (0)