Python trace context in queries
Adding trace context to application queries provides the ability to correlate the queries from a traced transaction, and if sampled, the queries to the transaction trace detail. This feature is also a requirement for the integration of APM with DBO.
Enabling
Trace context propagation to queries is disabled by default. It can be enabled in the Python Library using these configuration options:
-
SW_APM_ENABLED_SQLCOMMENT
: This option must be set. SolarWinds recommends the following values to support the integration of APM with DBO:-
MySQL: Enable this feature at the
SQLAlchemy
level:export SW_APM_ENABLED_SQLCOMMENT="sqlalchemy=true"
-
Postgres: Enable this feature at the database driver level:
# if using psycopg export SW_APM_ENABLED_SQLCOMMENT="psycopg=true" # if using psycopg2 export SW_APM_ENABLED_SQLCOMMENT="psycopg2=true"
-
-
SW_APM_OPTIONS_SQLCOMMENT
: This option may be used to customize the fields in the comment. Note that thetraceparent
field must be enabled (the default) to support the integration of APM with DBO. -
SW_APM_ENABLED_SQLCOMMENT_ATTRIBUTE
: This option may be used to customize comment addition to some database client span attributes.
The inserted SQL comment is in the format /*db_driver=<driver_name>,traceparent='00-<Trace_ID>-<Span_ID>-<Trace_Flags>'*/
. Other key value pairs may be included, depending on the client-side ORM framework, database driver, and upstream service code and frameworks involved in making the query.
For example, a query made by an instrumented Flask service upon receiving an HTTP request may also include controller=<controller_name>
and db_framework=<db_framework_name>
in addition to the standard SQL comment that is added.
Support Matrix
Database type |
Client library/framework |
Notes |
---|---|---|
MySQL | SQLAlchemy | |
Flask | Enabling this feature adds framework information but requires a driver-level or ORM instrumentation to insert the context. | |
Django ORM | Should not be enabled for integration of APM with DBO. The inserted context does not allow proper correlation. | |
PostgreSQL | psycop
psycopg2 |
SolarWinds does not recommend enabling this feature if your application uses server-side prepared statements, because the inserted context will likely be incorrect for executions of the statement. |
SQLAlchemy | Should only be enabled for integration of APM with DBO if trace context in queries is not enabled for psycopg/psycopg2. Otherwise, multiple comments are inserted. | |
Flask | Enabling this feature adds framework information but requires a driver-level or ORM instrumentation to insert the context. | |
Django ORM | Should not be enabled for integration of APM with DBO. The inserted context does not allow proper correlation. |