|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | """ |
16 | | -The integration with PostgreSQL supports the `Psycopg`_ library, it can be enabled by |
| 16 | +The integration with PostgreSQL supports the `Psycopg`_ library. It can be enabled by |
17 | 17 | using ``PsycopgInstrumentor``. |
18 | 18 |
|
19 | | -.. _Psycopg: http://initd.org/psycopg/ |
| 19 | +.. _Psycopg: https://www.psycopg.org/psycopg3/docs/ |
20 | 20 |
|
21 | 21 | Usage |
22 | 22 | ----- |
|
55 | 55 | Configuration |
56 | 56 | ------------- |
57 | 57 |
|
58 | | -SQLCOMMENTER |
59 | | -***************************************** |
| 58 | +SQLCommenter |
| 59 | +************ |
60 | 60 | You can optionally configure Psycopg instrumentation to enable sqlcommenter which enriches |
61 | | -the query with contextual information. |
| 61 | +the query with contextual information. Queries made after setting up trace integration with |
| 62 | +sqlcommenter enabled will have configurable key-value pairs appended to them, e.g. |
| 63 | +``"select * from auth_users; /*traceparent=00-01234567-abcd-01*/"``. This supports context |
| 64 | +propagation between database client and server when database log records are enabled. |
| 65 | +For more information, see: |
| 66 | +
|
| 67 | +* `Semantic Conventions - Database Spans <https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#sql-commenter>`_ |
| 68 | +* `sqlcommenter <https://google.github.io/sqlcommenter/>`_ |
62 | 69 |
|
63 | 70 | .. code:: python |
64 | 71 |
|
65 | 72 | from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor |
66 | 73 |
|
67 | | - PsycopgInstrumentor().instrument(enable_commenter=True, commenter_options={}) |
68 | | -
|
69 | | -
|
70 | | -For example, |
71 | | -:: |
72 | | -
|
73 | | - Invoking cursor.execute("select * from auth_users") will lead to sql query "select * from auth_users" but when SQLCommenter is enabled |
74 | | - the query will get appended with some configurable tags like "select * from auth_users /*tag=value*/;" |
75 | | -
|
76 | | -
|
77 | | -SQLCommenter Configurations |
78 | | -*************************** |
79 | | -We can configure the tags to be appended to the sqlquery log by adding configuration inside commenter_options(default:{}) keyword |
80 | | -
|
81 | | -db_driver = True(Default) or False |
82 | | -
|
83 | | -For example, |
84 | | -:: |
85 | | -Enabling this flag will add psycopg and it's version which is /*psycopg%%3A2.9.3*/ |
86 | | -
|
87 | | -dbapi_threadsafety = True(Default) or False |
88 | | -
|
89 | | -For example, |
90 | | -:: |
91 | | -Enabling this flag will add threadsafety /*dbapi_threadsafety=2*/ |
| 74 | + PsycopgInstrumentor().instrument(enable_commenter=True) |
92 | 75 |
|
93 | | -dbapi_level = True(Default) or False |
94 | 76 |
|
95 | | -For example, |
96 | | -:: |
97 | | -Enabling this flag will add dbapi_level /*dbapi_level='2.0'*/ |
| 77 | +SQLCommenter with commenter_options |
| 78 | +*********************************** |
| 79 | +The key-value pairs appended to the query can be configured using |
| 80 | +``commenter_options``. When sqlcommenter is enabled, all available KVs/tags |
| 81 | +are calculated by default. ``commenter_options`` supports *opting out* |
| 82 | +of specific KVs. |
98 | 83 |
|
99 | | -libpq_version = True(Default) or False |
100 | | -
|
101 | | -For example, |
102 | | -:: |
103 | | -Enabling this flag will add libpq_version /*libpq_version=140001*/ |
104 | | -
|
105 | | -driver_paramstyle = True(Default) or False |
| 84 | +.. code:: python |
106 | 85 |
|
107 | | -For example, |
108 | | -:: |
109 | | -Enabling this flag will add driver_paramstyle /*driver_paramstyle='pyformat'*/ |
| 86 | + from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor |
110 | 87 |
|
111 | | -opentelemetry_values = True(Default) or False |
| 88 | + # Opts into sqlcomment for Psycopg trace integration. |
| 89 | + # Opts out of tags for libpq_version, db_driver. |
| 90 | + PsycopgInstrumentor().instrument( |
| 91 | + enable_commenter=True, |
| 92 | + commenter_options={ |
| 93 | + "libpq_version": False, |
| 94 | + "db_driver": False, |
| 95 | + } |
| 96 | + ) |
112 | 97 |
|
113 | | -For example, |
114 | | -:: |
115 | | -Enabling this flag will add traceparent values /*traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'*/ |
| 98 | +Available commenter_options |
| 99 | +########################### |
| 100 | +
|
| 101 | +The following sqlcomment key-values can be opted out of through ``commenter_options``: |
| 102 | +
|
| 103 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 104 | +| Commenter Option | Description | Example | |
| 105 | ++===========================+===========================================================+===========================================================================+ |
| 106 | +| ``db_driver`` | Database driver name with version. | ``psycopg='3.1.9'`` | |
| 107 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 108 | +| ``dbapi_threadsafety`` | DB-API threadsafety value: 0-3 or unknown. | ``dbapi_threadsafety=2`` | |
| 109 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 110 | +| ``dbapi_level`` | DB-API API level: 1.0, 2.0, or unknown. | ``dbapi_level='2.0'`` | |
| 111 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 112 | +| ``driver_paramstyle`` | DB-API paramstyle for SQL statement parameter. | ``driver_paramstyle='pyformat'`` | |
| 113 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 114 | +| ``libpq_version`` | PostgreSQL libpq version | ``libpq_version=140001`` | |
| 115 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
| 116 | +| ``opentelemetry_values`` | OpenTelemetry context as traceparent at time of query. | ``traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'`` | |
| 117 | ++---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+ |
116 | 118 |
|
117 | 119 | SQLComment in span attribute |
118 | 120 | **************************** |
119 | | -If sqlcommenter is enabled, you can optionally configure psycopg instrumentation to append sqlcomment to query span attribute for convenience of your platform. |
| 121 | +If sqlcommenter is enabled, you can opt into the inclusion of sqlcomment in |
| 122 | +the query span ``db.statement`` attribute for your needs. If ``commenter_options`` |
| 123 | +have been set, the span attribute comment will also be configured by this |
| 124 | +setting. |
120 | 125 |
|
121 | 126 | .. code:: python |
122 | 127 |
|
123 | 128 | from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor |
124 | 129 |
|
| 130 | + # Opts into sqlcomment for Psycopg trace integration. |
| 131 | + # Opts into sqlcomment for `db.statement` span attribute. |
125 | 132 | PsycopgInstrumentor().instrument( |
126 | 133 | enable_commenter=True, |
127 | 134 | enable_attribute_commenter=True, |
128 | 135 | ) |
129 | 136 |
|
130 | | -For example, |
131 | | -:: |
132 | | -
|
133 | | - Invoking cursor.execute("select * from auth_users") will lead to postgresql query "select * from auth_users" but when SQLCommenter and attribute_commenter are enabled |
134 | | - 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. |
135 | | -
|
| 137 | +Warning: |
| 138 | + 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. |
136 | 139 |
|
137 | 140 | API |
138 | 141 | --- |
|
0 commit comments