Skip to content

Commit 017d8d6

Browse files
Improve sqlalchemy sqlcomment docs
1 parent 34db73e commit 017d8d6

File tree

1 file changed

+49
-36
lines changed
  • instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy

1 file changed

+49
-36
lines changed

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

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,68 +53,81 @@
5353
Configuration
5454
-------------
5555
56-
SQLCOMMENTER
57-
****************************************
58-
You can optionally configure SQLAlchemy instrumentation to enable sqlcommenter which enriches
59-
the query with contextual information.
56+
SQLCommenter
57+
************
58+
You can optionally enable sqlcommenter which enriches the query with contextual
59+
information. Queries made after setting up trace integration with sqlcommenter
60+
enabled will have configurable key-value pairs appended to them, e.g.
61+
``"select * from auth_users; /*traceparent=00-01234567-abcd-01*/"``. This
62+
supports context propagation between database client and server when database log
63+
records are enabled. For more information, see:
64+
65+
* `Semantic Conventions - Database Spans <https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#sql-commenter>`_
66+
* `sqlcommenter <https://google.github.io/sqlcommenter/>`_
6067
6168
.. code:: python
6269
6370
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
6471
65-
SQLAlchemyInstrumentor().instrument(enable_commenter=True, commenter_options={})
72+
SQLAlchemyInstrumentor().instrument(enable_commenter=True)
6673
74+
SQLCommenter with commenter_options
75+
***********************************
76+
The key-value pairs appended to the query can be configured using
77+
``commenter_options``. When sqlcommenter is enabled, all available KVs/tags
78+
are calculated by default. ``commenter_options`` supports *opting out*
79+
of specific KVs.
6780
68-
For example,
69-
::
70-
71-
Invoking engine.execute("select * from auth_users") will lead to sql query "select * from auth_users" but when SQLCommenter is enabled
72-
the query will get appended with some configurable tags like "select * from auth_users /*tag=value*/;"
73-
74-
SQLCommenter Configurations
75-
***************************
76-
We can configure the tags to be appended to the sqlquery log by adding configuration inside commenter_options(default:{}) keyword
77-
78-
db_driver = True(Default) or False
81+
.. code:: python
7982
80-
For example,
81-
::
82-
Enabling this flag will add any underlying driver like psycopg2 /*db_driver='psycopg2'*/
83+
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
8384
84-
db_framework = True(Default) or False
85+
# Opts into sqlcomment for SQLAlchemy trace integration.
86+
# Opts out of tags for db_driver, db_framework.
87+
SQLAlchemyInstrumentor().instrument(
88+
enable_commenter=True,
89+
commenter_options={
90+
"db_driver": False,
91+
"db_framework": False,
92+
}
93+
)
8594
86-
For example,
87-
::
88-
Enabling this flag will add db_framework and it's version /*db_framework='sqlalchemy:0.41b0'*/
95+
Available commenter_options
96+
###########################
8997
90-
opentelemetry_values = True(Default) or False
98+
The following sqlcomment key-values can be opted out of through ``commenter_options``:
9199
92-
For example,
93-
::
94-
Enabling this flag will add traceparent values /*traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'*/
100+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
101+
| Commenter Option | Description | Example |
102+
+===========================+===========================================================+===========================================================================+
103+
| ``db_driver`` | Database driver name. | ``db_driver='psycopg2'`` |
104+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
105+
| ``db_framework`` | Database framework name with version. | ``db_framework='sqlalchemy:1.4.0'`` |
106+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
107+
| ``opentelemetry_values`` | OpenTelemetry context as traceparent at time of query. | ``traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'`` |
108+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
95109
96110
SQLComment in span attribute
97111
****************************
98-
If sqlcommenter is enabled, you can further configure SQLAlchemy instrumentation to append sqlcomment to the `db.statement` span attribute for convenience of your platform.
112+
If sqlcommenter is enabled, you can opt into the inclusion of sqlcomment in
113+
the query span ``db.statement`` attribute for your needs. If ``commenter_options``
114+
have been set, the span attribute comment will also be configured by this
115+
setting.
99116
100117
.. code:: python
101118
102119
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
103120
121+
# Opts into sqlcomment for SQLAlchemy trace integration.
122+
# Opts into sqlcomment for `db.statement` span attribute.
104123
SQLAlchemyInstrumentor().instrument(
105124
enable_commenter=True,
106125
commenter_options={},
107126
enable_attribute_commenter=True,
108127
)
109128
110-
111-
For example,
112-
::
113-
114-
Invoking `engine.execute("select * from auth_users")` will lead to sql query "select * from auth_users" but when SQLCommenter and `attribute_commenter` is enabled
115-
the query will get appended with some configurable tags like "select * from auth_users /*tag=value*/;" for both server query and `db.statement` span attribute.
116-
117-
Warning: capture of sqlcomment in ``db.statement`` may have high cardinality without platform normalization. See `Semantic Conventions for database spans <https://opentelemetry.io/docs/specs/semconv/database/database-spans/#generating-a-summary-of-the-query-text>`_ for more information.
129+
Warning:
130+
Capture of sqlcomment in ``db.statement`` may have high cardinality without platform normalization. See `Semantic Conventions for database spans <https://opentelemetry.io/docs/specs/semconv/database/database-spans/#generating-a-summary-of-the-query-text>`_ for more information.
118131
119132
API
120133
---

0 commit comments

Comments
 (0)