Skip to content

Commit e63e626

Browse files
tammy-baylis-swixrmxemdneto
authored
Improve FlaskInstrumentor readthedocs on sqlcommenter (open-telemetry#3883)
* Clean up FlaskInstrumentor readthedocs sqlcommenter * Fix quotes * Changelog * Update instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py Co-authored-by: Emídio Neto <[email protected]> --------- Co-authored-by: Riccardo Magliocchetti <[email protected]> Co-authored-by: Emídio Neto <[email protected]>
1 parent f9b7af8 commit e63e626

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
([#3850](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3850))
1818
- `opentelemetry-instrumentation-httpx`: add support for url exclusions via `OTEL_PYTHON_EXCLUDED_URLS` / `OTEL_PYTHON_HTTPX_EXCLUDED_URLS`
1919
([#3837](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3837))
20+
- `opentelemetry-instrumentation-flask`: improve readthedocs for sqlcommenter configuration.
21+
([#3883](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3883))
2022

2123
### Fixed
2224

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

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -189,54 +189,66 @@ def response_hook(span: Span, status: str, response_headers: List):
189189
Note:
190190
The environment variable names used to capture HTTP headers are still experimental, and thus are subject to change.
191191
192-
SQLCOMMENTER
193-
*****************************************
194-
You can optionally configure Flask instrumentation to enable sqlcommenter which enriches
195-
the query with contextual information.
192+
SQLCommenter
193+
************
194+
You can optionally enable sqlcommenter which enriches the query with contextual
195+
information. Queries made after setting up trace integration with sqlcommenter
196+
enabled will have configurable key-value pairs appended to them, e.g.
197+
``"select * from auth_users; /*framework=flask%%3A2.9.3*/"``. This
198+
supports context propagation between database client and server when database log
199+
records are enabled. For more information, see:
200+
201+
* `Semantic Conventions - Database Spans <https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#sql-commenter>`_
202+
* `sqlcommenter <https://google.github.io/sqlcommenter/>`_
196203
197204
.. code:: python
198205
199206
from opentelemetry.instrumentation.flask import FlaskInstrumentor
200207
201-
FlaskInstrumentor().instrument(enable_commenter=True, commenter_options={})
202-
203-
For example, FlaskInstrumentor when used with SQLAlchemyInstrumentor or Psycopg2Instrumentor,
204-
invoking ``cursor.execute("select * from auth_users")`` will lead to sql query
205-
``select * from auth_users`` but when SQLCommenter is enabled the query will get appended with
206-
some configurable tags like:
207-
208-
.. code::
209-
210-
select * from auth_users /*metrics=value*/;"
208+
FlaskInstrumentor().instrument(enable_commenter=True)
211209
212-
Inorder for the commenter to append flask related tags to sql queries, the commenter needs
213-
to enabled on the respective SQLAlchemyInstrumentor or Psycopg2Instrumentor framework too.
214-
215-
SQLCommenter Configurations
216-
***************************
217-
We can configure the tags to be appended to the sqlquery log by adding configuration
218-
inside ``commenter_options={}`` dict.
219-
220-
For example, enabling this flag will add flask and it's version which
221-
is ``/*flask%%3A2.9.3*/`` to the SQL query as a comment (default is True):
210+
Note:
211+
FlaskInstrumentor sqlcommenter requires that sqlcommenter is also
212+
enabled for an active instrumentation of a database driver or object-relational
213+
mapper (ORM) in the same database client stack. The latter, such as
214+
Psycopg2Instrumentor of SQLAlchemyInstrumentor, will create a base sqlcomment
215+
that is enhanced by FlaskInstrumentor with additional values from context
216+
before appending to the query statement.
217+
218+
SQLCommenter with commenter_options
219+
***********************************
220+
The key-value pairs appended to the query can be configured using
221+
``commenter_options``. When sqlcommenter is enabled, all available KVs/tags
222+
are calculated by default. ``commenter_options`` supports *opting out*
223+
of specific KVs.
222224
223225
.. code:: python
224226
225-
framework = True
226-
227-
For example, enabling this flag will add route uri ``/*route='/home'*/``
228-
to the SQL query as a comment (default is True):
229-
230-
.. code:: python
227+
from opentelemetry.instrumentation.flask import FlaskInstrumentor
231228
232-
route = True
229+
# Opts into sqlcomment for Flask trace integration.
230+
# Opts out of tags for controller.
231+
FlaskInstrumentor().instrument(
232+
enable_commenter=True,
233+
commenter_options={
234+
"controller": False,
235+
}
236+
)
233237
234-
For example, enabling this flag will add controller name ``/*controller='home_view'*/``
235-
to the SQL query as a comment (default is True):
238+
Available commenter_options
239+
###########################
236240
237-
.. code:: python
241+
The following sqlcomment key-values can be opted out of through ``commenter_options``:
238242
239-
controller = True
243+
+-------------------+----------------------------------------------------+----------------------------------------+
244+
| Commenter Option | Description | Example |
245+
+===================+====================================================+========================================+
246+
| ``framework`` | Flask framework name with version (URL encoded). | ``framework='flask%%%%3A2.9.3'`` |
247+
+-------------------+----------------------------------------------------+----------------------------------------+
248+
| ``route`` | Flask route URI pattern. | ``route='/home'`` |
249+
+-------------------+----------------------------------------------------+----------------------------------------+
250+
| ``controller`` | Flask controller/endpoint name. | ``controller='home_view'`` |
251+
+-------------------+----------------------------------------------------+----------------------------------------+
240252
241253
API
242254
---

0 commit comments

Comments
 (0)