|
20 | 20 | Usage |
21 | 21 | ----- |
22 | 22 |
|
| 23 | +The DB-API instrumentor and its utilities provide common, core functionality for |
| 24 | +database framework or object relation mapper (ORM) instrumentations. Users will |
| 25 | +typically instrument database client code with those framework/ORM-specific |
| 26 | +instrumentations, instead of directly using this DB-API integration. Features |
| 27 | +such as sqlcommenter can be configured at framework/ORM level as well. See full |
| 28 | +list at `instrumentation`_. |
| 29 | +
|
| 30 | +.. _instrumentation: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation |
| 31 | +
|
| 32 | +If an instrumentation for your needs does not exist, then DB-API integration can |
| 33 | +be used directly as follows. |
| 34 | +
|
| 35 | +
|
23 | 36 | .. code-block:: python |
24 | 37 |
|
25 | 38 | import mysql.connector |
26 | 39 | import pyodbc |
27 | 40 |
|
28 | | - from opentelemetry.instrumentation.dbapi import trace_integration |
29 | | -
|
| 41 | + from opentelemetry.instrumentation.dbapi import ( |
| 42 | + trace_integration, |
| 43 | + wrap_connect, |
| 44 | + ) |
30 | 45 |
|
31 | | - # Ex: mysql.connector |
| 46 | + # Example: mysql.connector |
32 | 47 | trace_integration(mysql.connector, "connect", "mysql") |
33 | | - # Ex: pyodbc |
| 48 | + # Example: pyodbc |
34 | 49 | trace_integration(pyodbc, "Connection", "odbc") |
35 | 50 |
|
| 51 | + # Or, directly call wrap_connect for more configurability. |
| 52 | + wrap_connect(__name__, mysql.connector, "connect", "mysql") |
| 53 | + wrap_connect(__name__, pyodbc, "Connection", "odbc") |
| 54 | +
|
| 55 | +
|
| 56 | +Configuration |
| 57 | +------------- |
| 58 | +
|
| 59 | +SQLCommenter |
| 60 | +************ |
| 61 | +You can optionally enable sqlcommenter which enriches the query with contextual |
| 62 | +information. Queries made after setting up trace integration with sqlcommenter |
| 63 | +enabled will have configurable key-value pairs appended to them, e.g. |
| 64 | +``"select * from auth_users; /*traceparent=00-01234567-abcd-01*/"``. This |
| 65 | +supports context propagation between database client and server when database log |
| 66 | +records are enabled. For more information, see: |
| 67 | +
|
| 68 | +* `Semantic Conventions - Database Spans <https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#sql-commenter>`_ |
| 69 | +* `sqlcommenter <https://google.github.io/sqlcommenter/>`_ |
| 70 | +
|
| 71 | +.. code:: python |
| 72 | +
|
| 73 | + import mysql.connector |
| 74 | +
|
| 75 | + from opentelemetry.instrumentation.dbapi import wrap_connect |
| 76 | +
|
| 77 | +
|
| 78 | + # Opts into sqlcomment for MySQL trace integration. |
| 79 | + wrap_connect( |
| 80 | + __name__, |
| 81 | + mysql.connector, |
| 82 | + "connect", |
| 83 | + "mysql", |
| 84 | + enable_commenter=True, |
| 85 | + ) |
| 86 | +
|
| 87 | +
|
| 88 | +SQLCommenter with commenter_options |
| 89 | +*********************************** |
| 90 | +The key-value pairs appended to the query can be configured using |
| 91 | +``commenter_options``. When sqlcommenter is enabled, all available KVs/tags |
| 92 | +are calculated by default. ``commenter_options`` supports *opting out* |
| 93 | +of specific KVs. |
| 94 | +
|
| 95 | +.. code:: python |
| 96 | +
|
| 97 | + import mysql.connector |
| 98 | +
|
| 99 | + from opentelemetry.instrumentation.dbapi import wrap_connect |
| 100 | +
|
| 101 | +
|
| 102 | + # Opts into sqlcomment for MySQL trace integration. |
| 103 | + # Opts out of tags for libpq_version, db_driver. |
| 104 | + wrap_connect( |
| 105 | + __name__, |
| 106 | + mysql.connector, |
| 107 | + "connect", |
| 108 | + "mysql", |
| 109 | + enable_commenter=True, |
| 110 | + commenter_options={ |
| 111 | + "libpq_version": False, |
| 112 | + "db_driver": False, |
| 113 | + } |
| 114 | + ) |
| 115 | +
|
| 116 | +Available commenter_options |
| 117 | +########################### |
| 118 | +
|
| 119 | +The following sqlcomment key-values can be opted out of through ``commenter_options``: |
| 120 | +
|
| 121 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 122 | +| Commenter Option | Description | Example | |
| 123 | ++===========================+===========================================================+===========================================================================+ |
| 124 | +| ``db_driver`` | Database driver name with version. | ``mysql.connector=2.2.9`` | |
| 125 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 126 | +| ``dbapi_threadsafety`` | DB-API threadsafety value: 0-3 or unknown. | ``dbapi_threadsafety=2`` | |
| 127 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 128 | +| ``dbapi_level`` | DB-API API level: 1.0, 2.0, or unknown. | ``dbapi_level=2.0`` | |
| 129 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 130 | +| ``driver_paramstyle`` | DB-API paramstyle for SQL statement parameter. | ``driver_paramstyle='pyformat'`` | |
| 131 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 132 | +| ``libpq_version`` | PostgreSQL libpq version (checked for PostgreSQL only). | ``libpq_version=140001`` | |
| 133 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 134 | +| ``mysql_client_version`` | MySQL client version (checked for MySQL only). | ``mysql_client_version='123'`` | |
| 135 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 136 | +| ``opentelemetry_values`` | OpenTelemetry context as traceparent at time of query. | ``traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'`` | |
| 137 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 138 | +
|
| 139 | +SQLComment in span attribute |
| 140 | +**************************** |
| 141 | +If sqlcommenter is enabled, you can opt into the inclusion of sqlcomment in |
| 142 | +the query span ``db.statement`` attribute for your needs. If ``commenter_options`` |
| 143 | +have been set, the span attribute comment will also be configured by this |
| 144 | +setting. |
| 145 | +
|
| 146 | +.. code:: python |
| 147 | +
|
| 148 | + import mysql.connector |
| 149 | +
|
| 150 | + from opentelemetry.instrumentation.dbapi import wrap_connect |
| 151 | +
|
| 152 | +
|
| 153 | + # Opts into sqlcomment for MySQL trace integration. |
| 154 | + # Opts into sqlcomment for `db.statement` span attribute. |
| 155 | + wrap_connect( |
| 156 | + __name__, |
| 157 | + mysql.connector, |
| 158 | + "connect", |
| 159 | + "mysql", |
| 160 | + enable_commenter=True, |
| 161 | + enable_attribute_commenter=True, |
| 162 | + ) |
| 163 | +
|
| 164 | +
|
36 | 165 | API |
37 | 166 | --- |
38 | 167 | """ |
|
0 commit comments