Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- `opentelemetry-instrumentation-flask`: improve readthedocs for sqlcommenter configuration.
([#3883](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3883))

## Version 1.38.0/0.59b0 (2025-10-16)

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,54 +189,66 @@ def response_hook(span: Span, status: str, response_headers: List):
Note:
The environment variable names used to capture HTTP headers are still experimental, and thus are subject to change.

SQLCOMMENTER
*****************************************
You can optionally configure Flask instrumentation to enable sqlcommenter which enriches
the query with contextual information.
SQLCommenter
************
You can optionally enable sqlcommenter which enriches the query with contextual
information. Queries made after setting up trace integration with sqlcommenter
enabled will have configurable key-value pairs appended to them, e.g.
``"select * from auth_users; /*framework=flask%%3A2.9.3*/"``. This
supports context propagation between database client and server when database log
records are enabled. For more information, see:

* `Semantic Conventions - Database Spans <https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#sql-commenter>`_
* `sqlcommenter <https://google.github.io/sqlcommenter/>`_

.. code:: python

from opentelemetry.instrumentation.flask import FlaskInstrumentor

FlaskInstrumentor().instrument(enable_commenter=True, commenter_options={})

For example, FlaskInstrumentor when used with SQLAlchemyInstrumentor or Psycopg2Instrumentor,
invoking ``cursor.execute("select * from auth_users")`` will lead to sql query
``select * from auth_users`` but when SQLCommenter is enabled the query will get appended with
some configurable tags like:

.. code::

select * from auth_users /*metrics=value*/;"
FlaskInstrumentor().instrument(enable_commenter=True)

Inorder for the commenter to append flask related tags to sql queries, the commenter needs
to enabled on the respective SQLAlchemyInstrumentor or Psycopg2Instrumentor framework too.

SQLCommenter Configurations
***************************
We can configure the tags to be appended to the sqlquery log by adding configuration
inside ``commenter_options={}`` dict.

For example, enabling this flag will add flask and it's version which
is ``/*flask%%3A2.9.3*/`` to the SQL query as a comment (default is True):
Note:
FlaskInstrumentor sqlcommenter requires that sqlcommenter is also
enabled for an active instrumention of a database driver or object relation
mapper (ORM) in the same database client stack. The latter, such as
Psycopg2Instrumentor of SQLAlchemyInstrumentor, will create a base sqlcomment
that is enhanced by FlaskInstrumentor with additional values from context
before appending to the query statement.

SQLCommenter with commenter_options
***********************************
The key-value pairs appended to the query can be configured using
``commenter_options``. When sqlcommenter is enabled, all available KVs/tags
are calculated by default. ``commenter_options`` supports *opting out*
of specific KVs.

.. code:: python

framework = True

For example, enabling this flag will add route uri ``/*route='/home'*/``
to the SQL query as a comment (default is True):

.. code:: python
from opentelemetry.instrumentation.flask import FlaskInstrumentor

route = True
# Opts into sqlcomment for Flask trace integration.
# Opts out of tags for controller.
FlaskInstrumentor().instrument(
enable_commenter=True,
commenter_options={
"controller": False,
}
)

For example, enabling this flag will add controller name ``/*controller='home_view'*/``
to the SQL query as a comment (default is True):
Available commenter_options
###########################

.. code:: python
The following sqlcomment key-values can be opted out of through ``commenter_options``:

controller = True
+-------------------+----------------------------------------------------+----------------------------------------+
| Commenter Option | Description | Example |
+===================+====================================================+========================================+
| ``framework`` | Flask framework name with version (URL encoded). | ``framework='flask%%%%3A2.9.3'`` |
+-------------------+----------------------------------------------------+----------------------------------------+
| ``route`` | Flask route URI pattern. | ``route='/home'`` |
+-------------------+----------------------------------------------------+----------------------------------------+
| ``controller`` | Flask controller/endpoint name. | ``controller='home_view'`` |
+-------------------+----------------------------------------------------+----------------------------------------+

API
---
Expand Down