Skip to content

Commit 582be03

Browse files
committed
update README.md
1 parent 8fed2d7 commit 582be03

1 file changed

Lines changed: 57 additions & 26 deletions

File tree

python/sqlcommenter-python/README.md

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ Python modules for popular projects that add meta info to your SQL queries as co
1212
pip3 install --user google-cloud-sqlcommenter
1313
```
1414

15-
If you'd like to record some OpenCensus information as well, just install it:
15+
If you'd like to record the OpenCensus trace context as well, just install it:
1616

1717
```shell
18-
pip3 install opencensus
18+
pip3 install google-cloud-sqlcommenter[opencensus]
19+
```
20+
21+
If you'd like to record the OpenTelemetry trace context as well (Python 3+ only),
22+
just install it:
23+
24+
```shell
25+
pip3 install google-cloud-sqlcommenter[opentelemetry]
1926
```
2027

2128
## Usage
@@ -38,11 +45,13 @@ which when viewed say on Postgresql logs, produces
3845
('Wassup?', '2019-05-28T18:54:50.767481+00:00'::timestamptz) RETURNING
3946
"polls_question"."id" /*controller='index',framework='django%3A2.2.1',route='%5Epolls/%24'*/
4047
```
41-
If you want the opencensus attributes included, you must set the
42-
``SQLCOMMENTER_WITH_OPENCENSUS`` setting to ``True`` and include
43-
``'opencensus.ext.django.middleware.OpencensusMiddleware'`` before
44-
``'google.cloud.sqlcommenter.django.middleware.SqlCommenter',`` in your ``MIDDLEWARE``
45-
setting.
48+
If you want the OpenCensus attributes included, you must set the
49+
``SQLCOMMENTER_WITH_OPENCENSUS`` setting to ``True``.
50+
51+
If you want the OpenTelemetry attributes included, you must set the
52+
``SQLCOMMENTER_WITH_OPENTELEMETRY`` setting to ``True``.
53+
54+
You cannot use OpenTelemetry and OpenCensus together, as they use the same attributes.
4655

4756
### SQLAlchemy
4857

@@ -53,7 +62,13 @@ import sqlalchemy
5362
from google.cloud.sqlcommenter.sqlalchemy.executor import BeforeExecuteFactory
5463

5564
engine = sqlalchemy.create_engine(...)
56-
listener = BeforeExecuteFactory(with_db_driver=True, with_db_framework=True, with_opencensus=True)
65+
listener = BeforeExecuteFactory(
66+
with_db_driver=True,
67+
with_db_framework=True,
68+
# you may use one of opencensus or opentelemetry
69+
with_opencensus=True,
70+
with_opentelemetry=True,
71+
)
5772
sqlalchemy.event.listen(engine, 'before_cursor_execute', listener, retval=True)
5873
engine.execute(...) # comment will be added before execution
5974
```
@@ -75,8 +90,15 @@ import psycopg2
7590
from google.cloud.sqlcommenter.psycopg2.extension import CommenterCursorFactory
7691

7792
cursor_factory = CommenterCursorFactory(
78-
with_db_driver=True, with_dbapi_level=True, with_dbapi_threadsafety=True,
79-
with_driver_paramstyle=True, with_libpq_version=True, with_opencensus=True)
93+
with_db_driver=True,
94+
with_dbapi_level=True,
95+
with_dbapi_threadsafety=True,
96+
with_driver_paramstyle=True,
97+
with_libpq_version=True,
98+
# you may use one of opencensus or opentelemetry
99+
with_opencensus=True,
100+
with_opentelemetry=True,
101+
)
80102
conn = psycopg2.connect(..., cursor_factory=cursor_factory)
81103
cursor = conn.cursor()
82104
cursor.execute(...) # comment will be added before execution
@@ -95,23 +117,32 @@ tracestate='congo%%3Dt61rcWkgMzE%%2Crojo%%3D00f067aa0ba902b7'*/
95117

96118
With Django, each option translates to a Django setting by uppercasing it and prepending `SQLCOMMENTER_`. For example, `with_framework` is controlled by the django setting `SQLCOMMENTER_WITH_FRAMEWORK`.
97119

98-
| Options | Included by default? | Django | SQLAlchemy | psycopg2 | Notes |
99-
| ------- | :------------------: | ------ | ---------- | -------- | :---: |
100-
| `with_framework` | :heavy_check_mark: | [Django version](https://docs.djangoproject.com/en/stable/releases/) | [Flask version](http://flask.pocoo.org/) | [Flask version](http://flask.pocoo.org/) |
101-
| `with_controller` | :heavy_check_mark: | [Django view](https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.view_name) | [Flask endpoint](http://flask.pocoo.org/docs/1.0/api/#flask.Flask.endpoint) | [Flask endpoint](http://flask.pocoo.org/docs/1.0/api/#flask.Flask.endpoint) |
102-
| `with_route` | :heavy_check_mark: | [Django route](https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.route) | [Flask route](http://flask.pocoo.org/docs/1.0/api/#flask.Flask.route) | [Flask route](http://flask.pocoo.org/docs/1.0/api/#flask.Flask.route) |
103-
| `with_app_name ` | | [Django app name](https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.app_name) | | |
104-
| `with_opencensus` | | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | [[1]](#1-opencensus)
105-
| `with_db_driver` | | [Django DB engine](https://docs.djangoproject.com/en/stable/ref/settings/#engine) | [SQLAlchemy DB driver](https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls) | [psycopg2 version](http://initd.org/psycopg/docs/) |
106-
| `with_db_framework` | | | [SQLAlchemy version](https://www.sqlalchemy.org/) | |
107-
| `with_dbapi_threadsafety` | | | | [psycopg2 thread safety](http://initd.org/psycopg/docs/module.html#psycopg2.threadsafety) |
108-
| `with_dbapi_level` | | | | [psycopg2 api level](http://initd.org/psycopg/docs/module.html#psycopg2.apilevel) |
109-
| `with_libpq_version` | | | | [psycopg2 libpq version](http://initd.org/psycopg/docs/module.html#psycopg2.__libpq_version__) |
110-
| `with_driver_paramstyle` | | | | [psycopg2 parameter style](http://initd.org/psycopg/docs/module.html#psycopg2.paramstyle) |
120+
| Options | Included by default? | Django | SQLAlchemy | psycopg2 | Notes |
121+
| ------------------------- | :------------------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-----------------------------------------------------: |
122+
| `with_framework` | :heavy_check_mark: | [Django version](https://docs.djangoproject.com/en/stable/releases/) | [Flask version](http://flask.pocoo.org/) | [Flask version](http://flask.pocoo.org/) |
123+
| `with_controller` | :heavy_check_mark: | [Django view](https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.view_name) | [Flask endpoint](http://flask.pocoo.org/docs/1.0/api/#flask.Flask.endpoint) | [Flask endpoint](http://flask.pocoo.org/docs/1.0/api/#flask.Flask.endpoint) |
124+
| `with_route` | :heavy_check_mark: | [Django route](https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.route) | [Flask route](http://flask.pocoo.org/docs/1.0/api/#flask.Flask.route) | [Flask route](http://flask.pocoo.org/docs/1.0/api/#flask.Flask.route) |
125+
| `with_app_name ` | | [Django app name](https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.app_name) | | |
126+
| `with_opencensus` | | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | [[1]](#1-opencensus)[[3]](#3-traceparent/tracestate) |
127+
| `with_opentelemetry` | | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | [[2]](#2-opentelemetry)[[3]](#3-traceparent/tracestate) |
128+
| `with_db_driver` | | [Django DB engine](https://docs.djangoproject.com/en/stable/ref/settings/#engine) | [SQLAlchemy DB driver](https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls) | [psycopg2 version](http://initd.org/psycopg/docs/) |
129+
| `with_db_framework` | | | [SQLAlchemy version](https://www.sqlalchemy.org/) | |
130+
| `with_dbapi_threadsafety` | | | | [psycopg2 thread safety](http://initd.org/psycopg/docs/module.html#psycopg2.threadsafety) |
131+
| `with_dbapi_level` | | | | [psycopg2 api level](http://initd.org/psycopg/docs/module.html#psycopg2.apilevel) |
132+
| `with_libpq_version` | | | | [psycopg2 libpq version](http://initd.org/psycopg/docs/module.html#psycopg2.__libpq_version__) |
133+
| `with_driver_paramstyle` | | | | [psycopg2 parameter style](http://initd.org/psycopg/docs/module.html#psycopg2.paramstyle) |
111134

112135
#### [1] `opencensus`
113136

114-
For `opencensus` to work correctly, note that:
137+
For `opencensus` to work correctly, note that [OpenCensus for
138+
Python](https://github.com/census-instrumentation/opencensus-python) must be
139+
installed in the python environment.
140+
141+
#### [2] `opentelemetry`
142+
143+
For `opentelemetry` to work correctly, note that [OpenTelemetry for
144+
Python](https://github.com/open-telemetry/opentelemetry-python) must be
145+
installed in the python environment.
115146

116-
* [OpenCensus for Python](https://github.com/census-instrumentation/opencensus-python) must be installed in the python environment.
117-
* Because the W3C TraceContext's `traceparent` and `tracestate` are quite ephemeral per request, including these attributes can have a negative impact on query caching.
147+
#### [3] `traceparent/tracestate`
148+
Because the W3C TraceContext's `traceparent` and `tracestate` are quite ephemeral per request, including these attributes can have a negative impact on query caching.

0 commit comments

Comments
 (0)