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