|  | 
| 39 | 39 | Configuration | 
| 40 | 40 | ------------- | 
| 41 | 41 | 
 | 
| 42 |  | -SQLCOMMENTER | 
| 43 |  | -***************************************** | 
| 44 |  | -You can optionally configure MySQLClient instrumentation to enable sqlcommenter which enriches | 
| 45 |  | -the query with contextual information. | 
|  | 42 | +SQLCommenter | 
|  | 43 | +************ | 
|  | 44 | +You can optionally enable sqlcommenter which enriches the query with contextual | 
|  | 45 | +information. Queries made after setting up trace integration with sqlcommenter | 
|  | 46 | +enabled will have configurable key-value pairs appended to them, e.g. | 
|  | 47 | +``"select * from auth_users; /*traceparent=00-01234567-abcd-01*/"``. This | 
|  | 48 | +supports context propagation between database client and server when database log | 
|  | 49 | +records are enabled. For more information, see: | 
|  | 50 | +
 | 
|  | 51 | +* `Semantic Conventions - Database Spans <https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#sql-commenter>`_ | 
|  | 52 | +* `sqlcommenter <https://google.github.io/sqlcommenter/>`_ | 
| 46 | 53 | 
 | 
| 47 | 54 | .. code:: python | 
| 48 | 55 | 
 | 
| 49 | 56 |     import MySQLdb | 
| 50 | 57 |     from opentelemetry.instrumentation.mysqlclient import MySQLClientInstrumentor | 
| 51 | 58 | 
 | 
| 52 |  | -    # Call instrument() to wrap all database connections | 
| 53 |  | -    MySQLClientInstrumentor().instrument(enable_commenter=True, commenter_options={}) | 
|  | 59 | +    MySQLClientInstrumentor().instrument(enable_commenter=True) | 
| 54 | 60 | 
 | 
| 55 | 61 |     cnx = MySQLdb.connect(database="MySQL_Database") | 
| 56 | 62 |     cursor = cnx.cursor() | 
|  | 
| 60 | 66 |     cursor.close() | 
| 61 | 67 |     cnx.close() | 
| 62 | 68 | 
 | 
|  | 69 | +SQLCommenter with commenter_options | 
|  | 70 | +*********************************** | 
|  | 71 | +The key-value pairs appended to the query can be configured using | 
|  | 72 | +``commenter_options``. When sqlcommenter is enabled, all available KVs/tags | 
|  | 73 | +are calculated by default. ``commenter_options`` supports *opting out* | 
|  | 74 | +of specific KVs. | 
|  | 75 | +
 | 
| 63 | 76 | .. code:: python | 
| 64 | 77 | 
 | 
| 65 | 78 |     import MySQLdb | 
| 66 | 79 |     from opentelemetry.instrumentation.mysqlclient import MySQLClientInstrumentor | 
| 67 | 80 | 
 | 
| 68 |  | -    # Alternatively, use instrument_connection for an individual connection | 
| 69 |  | -    cnx = MySQLdb.connect(database="MySQL_Database") | 
| 70 |  | -    instrumented_cnx = MySQLClientInstrumentor().instrument_connection( | 
| 71 |  | -        cnx, | 
|  | 81 | +    # Opts into sqlcomment for MySQLClient trace integration. | 
|  | 82 | +    # Opts out of tags for mysql_client_version, db_driver. | 
|  | 83 | +    MySQLClientInstrumentor().instrument( | 
| 72 | 84 |         enable_commenter=True, | 
| 73 | 85 |         commenter_options={ | 
| 74 |  | -            "db_driver": True, | 
| 75 |  | -            "mysql_client_version": True, | 
| 76 |  | -            "driver_paramstyle": False | 
|  | 86 | +            "mysql_client_version": False, | 
|  | 87 | +            "db_driver": False, | 
| 77 | 88 |         } | 
| 78 | 89 |     ) | 
| 79 |  | -    cursor = instrumented_cnx.cursor() | 
| 80 |  | -    cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)") | 
| 81 |  | -    cursor.execute("INSERT INTO test (testField) VALUES (123)") | 
| 82 |  | -    instrumented_cnx.commit() | 
| 83 |  | -    cursor.close() | 
| 84 |  | -    instrumented_cnx.close() | 
| 85 |  | -
 | 
| 86 |  | -For example, | 
| 87 |  | -:: | 
| 88 |  | -
 | 
| 89 |  | -   Invoking cursor.execute("INSERT INTO test (testField) VALUES (123)") will lead to sql query "INSERT INTO test (testField) VALUES (123)" but when SQLCommenter is enabled | 
| 90 |  | -   the query will get appended with some configurable tags like "INSERT INTO test (testField) VALUES (123) /*tag=value*/;" | 
| 91 |  | -
 | 
| 92 |  | -SQLCommenter Configurations | 
| 93 |  | -*************************** | 
| 94 |  | -We can configure the tags to be appended to the sqlquery log by adding configuration inside commenter_options(default:{}) keyword | 
| 95 |  | -
 | 
| 96 |  | -db_driver = True(Default) or False | 
| 97 |  | -
 | 
| 98 |  | -For example, | 
| 99 |  | -:: | 
| 100 |  | -Enabling this flag will add MySQLdb and its version, e.g. /*MySQLdb%%3A1.2.3*/ | 
| 101 | 90 | 
 | 
| 102 |  | -dbapi_threadsafety = True(Default) or False | 
| 103 |  | -
 | 
| 104 |  | -For example, | 
| 105 |  | -:: | 
| 106 |  | -Enabling this flag will add threadsafety /*dbapi_threadsafety=2*/ | 
| 107 |  | -
 | 
| 108 |  | -dbapi_level = True(Default) or False | 
| 109 |  | -
 | 
| 110 |  | -For example, | 
| 111 |  | -:: | 
| 112 |  | -Enabling this flag will add dbapi_level /*dbapi_level='2.0'*/ | 
| 113 |  | -
 | 
| 114 |  | -mysql_client_version = True(Default) or False | 
| 115 |  | -
 | 
| 116 |  | -For example, | 
| 117 |  | -:: | 
| 118 |  | -Enabling this flag will add mysql_client_version /*mysql_client_version='123'*/ | 
| 119 |  | -
 | 
| 120 |  | -driver_paramstyle = True(Default) or False | 
| 121 |  | -
 | 
| 122 |  | -For example, | 
| 123 |  | -:: | 
| 124 |  | -Enabling this flag will add driver_paramstyle /*driver_paramstyle='pyformat'*/ | 
| 125 |  | -
 | 
| 126 |  | -opentelemetry_values = True(Default) or False | 
| 127 |  | -
 | 
| 128 |  | -For example, | 
| 129 |  | -:: | 
| 130 |  | -Enabling this flag will add traceparent values /*traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'*/ | 
|  | 91 | +Available commenter_options | 
|  | 92 | +########################### | 
|  | 93 | +
 | 
|  | 94 | +The following sqlcomment key-values can be opted out of through ``commenter_options``: | 
|  | 95 | +
 | 
|  | 96 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ | 
|  | 97 | +| Commenter Option          | Description                                               | Example                                                                   | | 
|  | 98 | ++===========================+===========================================================+===========================================================================+ | 
|  | 99 | +| ``db_driver``             | Database driver name with version (URL encoded).          | ``MySQLdb%%%%3A1.2.3``                                                    | | 
|  | 100 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ | 
|  | 101 | +| ``dbapi_threadsafety``    | DB-API threadsafety value: 0-3 or unknown.                | ``dbapi_threadsafety=2``                                                  | | 
|  | 102 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ | 
|  | 103 | +| ``dbapi_level``           | DB-API API level: 1.0, 2.0, or unknown.                   | ``dbapi_level='2.0'``                                                     | | 
|  | 104 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ | 
|  | 105 | +| ``driver_paramstyle``     | DB-API paramstyle for SQL statement parameter.            | ``driver_paramstyle='pyformat'``                                          | | 
|  | 106 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ | 
|  | 107 | +| ``mysql_client_version``  | MySQL client version.                                     | ``mysql_client_version='123'``                                            | | 
|  | 108 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ | 
|  | 109 | +| ``opentelemetry_values``  | OpenTelemetry context as traceparent at time of query.    | ``traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'`` | | 
|  | 110 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ | 
| 131 | 111 | 
 | 
| 132 | 112 | SQLComment in span attribute | 
| 133 | 113 | **************************** | 
| 134 |  | -If sqlcommenter is enabled, you can optionally configure MySQLClient instrumentation to append sqlcomment to query span attribute for convenience of your platform. | 
|  | 114 | +If sqlcommenter is enabled, you can opt into the inclusion of sqlcomment in | 
|  | 115 | +the query span ``db.statement`` attribute for your needs. If ``commenter_options`` | 
|  | 116 | +have been set, the span attribute comment will also be configured by this | 
|  | 117 | +setting. | 
| 135 | 118 | 
 | 
| 136 | 119 | .. code:: python | 
| 137 | 120 | 
 | 
| 138 | 121 |     from opentelemetry.instrumentation.mysqlclient import MySQLClientInstrumentor | 
| 139 | 122 | 
 | 
|  | 123 | +    # Opts into sqlcomment for mysqlclient trace integration. | 
|  | 124 | +    # Opts into sqlcomment for `db.statement` span attribute. | 
| 140 | 125 |     MySQLClientInstrumentor().instrument( | 
| 141 | 126 |         enable_commenter=True, | 
| 142 | 127 |         enable_attribute_commenter=True, | 
| 143 | 128 |     ) | 
| 144 | 129 | 
 | 
| 145 |  | -
 | 
| 146 |  | -For example, | 
| 147 |  | -:: | 
| 148 |  | -
 | 
| 149 |  | -    Invoking cursor.execute("select * from auth_users") will lead to sql query "select * from auth_users" but when SQLCommenter and attribute_commenter are enabled | 
| 150 |  | -    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. | 
|  | 130 | +Warning: | 
|  | 131 | +    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. | 
| 151 | 132 | 
 | 
| 152 | 133 | API | 
| 153 | 134 | --- | 
|  | 
0 commit comments