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