Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit c4b4f26

Browse files
Jon Wayne Parrottliyanhui1228
authored andcommitted
Update README.rst (#75)
1 parent abe9a69 commit c4b4f26

1 file changed

Lines changed: 93 additions & 126 deletions

File tree

README.rst

Lines changed: 93 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
1-
OpenCensus - A stats collection and distributed tracing framework
2-
=================================================================
1+
OpenCensus for Python - A stats collection and distributed tracing framework
2+
============================================================================
33

4-
This is the open-source release of Census for Python. Census provides a
5-
framework to measure a server's resource usage and collect performance
6-
stats. This repository contains python related utilities and supporting
7-
software needed by Census.
4+
`Census`_ for Python. Census provides a framework to measure a server's resource
5+
usage and collect performance stats. This repository contains Python related
6+
utilities and supporting software needed by Census.
87

9-
Installation
10-
------------
8+
.. _Census: https://github.com/census-instrumentation
9+
10+
Installation & basic usage
11+
--------------------------
1112

12-
1. Install the opencensus-trace package using pip
13+
1. Install the opencensus-trace package using `pip`_ or `pipenv`_:
1314

1415
::
1516

1617
pip install opencensus-trace
18+
pipenv install opencensus-trace
1719

18-
2. Initialize a tracer to enable trace in your application
20+
2. Initialize a tracer for application:
1921

2022
.. code:: python
2123
2224
from opencensus.trace import request_tracer
2325
2426
tracer = request_tracer.RequestTracer()
2527
28+
.. _pip: https://pip.pypa.io
29+
.. _pipenv: https://docs.pipenv.org/
30+
2631
Usage
2732
-----
2833

29-
There are two ways to trace your code blocks. One is using a ``with``
30-
statement to wrap your code, and the trace span will end when exit the
31-
``with`` statement. Another is explicitly start and finish the trace
32-
span before and after your code block. Sample code for the two usages as
33-
below:
34-
35-
Usage 1: ``with`` statement (Recommended)
36-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34+
You can collect traces using the ``RequestTracer`` `context manager`_:
3735

3836
.. code:: python
3937
@@ -52,10 +50,9 @@ Usage 1: ``with`` statement (Recommended)
5250
with tracer.span(name='span2') as span2:
5351
do_something_to_trace()
5452
55-
# The spans will be exported when exiting the with block.
53+
Census will collect everything within the ``with`` statement as a single span.
5654

57-
Usage 2: Explicitly start and end spans
58-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55+
Alternatively, you can explicitly start and end a span:
5956

6057
.. code:: python
6158
@@ -69,6 +66,10 @@ Usage 2: Explicitly start and end spans
6966
do_something_to_trace()
7067
tracer.end_span()
7168
69+
70+
.. _context manager: https://docs.python.org/3/reference/datamodel.html#context-managers
71+
72+
7273
Customization
7374
-------------
7475

@@ -91,21 +92,23 @@ and ``ProbabilitySampler``
9192
Exporters
9293
~~~~~~~~~
9394

94-
You can choose different exporters to send the traces to. Default is
95-
printing the traces in JSON format. The rest options are sending to
96-
logging, or write to a file. Will add exporters to report to different
97-
trace backend later.
95+
You can choose different exporters to send the traces to. By default,
96+
the traces are printed to stdout in JSON format. Other options include
97+
writing to a file, sending to Python logging, or reporting to
98+
Stackdriver.
99+
100+
This example shows how to configure Census to save the traces to a
101+
file:
98102

99103
.. code:: python
100104
101105
from opencensus.trace.exporters import file_exporter
102106
from opencensus.trace.tracer import context_tracer
103107
104-
# Export the traces to a local file
105108
exporter = file_exporter.FileExporter(file_name='traces')
106109
tracer = context_tracer.ContextTracer(exporter=exporter)
107110
108-
Report to Stackdriver Trace:
111+
This example shows how to report the traces to Stackdriver Trace:
109112

110113
.. code:: python
111114
@@ -119,9 +122,11 @@ Report to Stackdriver Trace:
119122
Propagators
120123
~~~~~~~~~~~
121124

122-
You can specify the propagator type for serialize and deserialize the
123-
SpanContext and headers. Currently support
124-
``GoogleCloudFormatPropagator``, ``TextFormatPropagator``.
125+
You can specify the propagator type for serializing and deserializing the
126+
``SpanContex`` and its headers. There are currently two built in propagators:
127+
``GoogleCloudFormatPropagator`` and ``TextFormatPropagator``.
128+
129+
This example shows how to use the ``GoogleCloudFormatPropagator``:
125130

126131
.. code:: python
127132
@@ -139,12 +144,10 @@ Blacklist Paths
139144
~~~~~~~~~~~~~~~
140145

141146
You can specify which paths you do not want to trace by configuring the
142-
blacklist paths. By default the health check path in GAE Flex is not traced,
143-
but you can turn it on in the setting.
144-
145-
Here is the sample code for configuring the blacklist:
147+
blacklist paths.
146148

147-
For Flask:
149+
This example shows how to configure the blacklist to ignore the `_ah/health` endpoint
150+
for a Flask application:
148151

149152
.. code:: python
150153
@@ -155,22 +158,27 @@ For Flask:
155158
blacklist_paths = ['_ah/health']
156159
middleware = FlaskMiddleware(app, blacklist_paths=blacklist_paths)
157160
158-
For Django:
161+
For Django, you can configure the blacklist in the ``OPENCENSUS_PARAMS`` in ``settings.py``:
159162

160-
Add this line in the OPENCENSUS_PARAMS:
163+
.. code:: python
161164
162-
::
165+
OPENCENSUS_PARAMS: {
166+
...
167+
'BLACKLIST_PATHS': ['_ah/health',],
168+
}
163169
164-
'BLACKLIST_PATHS': ['_ah/health',]
170+
171+
.. note:: By default the health check path for the App Engine flexible environment is not traced,
172+
but you can turn it on by excluding it from the blacklist setting.
165173

166174
Framework Integration
167175
---------------------
168176

169177
Opencensus supports integration with popular web frameworks including
170-
Django, Flask and Webapp2. When the application receives a HTTP request,
178+
Django, Flask, and Webapp2. When the application receives a HTTP request,
171179
the tracer will automatically generate a span context using the trace
172180
information extracted from the request headers, and propagated to the
173-
child spans. Below is the sample code snippets:
181+
child spans.
174182

175183
Flask
176184
~~~~~
@@ -195,19 +203,26 @@ Django
195203
For tracing Django requests, you will need to add the following line to
196204
the ``MIDDLEWARE_CLASSES`` section in the Django ``settings.py`` file.
197205

198-
::
206+
.. code:: python
199207
200-
'opencensus.trace.ext.django.middleware.OpencensusMiddleware',
208+
MIDDLEWARE_CLASSES = [
209+
...
210+
'opencensus.trace.ext.django.middleware.OpencensusMiddleware',
211+
]
201212
202-
Add this line to the ``INSTALLED_APPS`` section:
213+
And add this line to the ``INSTALLED_APPS`` section:
203214

204-
::
215+
.. code:: python
205216
206-
'opencensus.trace.ext.django',
217+
INSTALLED_APPS = [
218+
...
219+
'opencensus.trace.ext.django',
220+
]
207221
208-
Customize the sampler, exporter, propagator in the ``settings.py`` file:
222+
You can configure the the sampler, exporter, propagator using the ``OPENCENSUS_TRACE`` setting in
223+
``settings.py``:
209224

210-
::
225+
.. code:: python
211226
212227
OPENCENSUS_TRACE = {
213228
'SAMPLER': 'opencensus.trace.samplers.probability.ProbabilitySampler',
@@ -216,9 +231,10 @@ Customize the sampler, exporter, propagator in the ``settings.py`` file:
216231
'GoogleCloudFormatPropagator',
217232
}
218233
219-
Configure the sampling rate and other params:
234+
You can configure the sampling rate and other parameters using the ``OPENCENSUS_TRACE_PARAMS``
235+
setting in ``settings.py``:
220236

221-
::
237+
.. code:: python
222238
223239
OPENCENSUS_TRACE_PARAMS = {
224240
'BLACKLIST_PATHS': ['/_ah/health'],
@@ -227,11 +243,8 @@ Configure the sampling rate and other params:
227243
'ZIPKIN_EXPORTER_SERVICE_NAME': 'my_service',
228244
'ZIPKIN_EXPORTER_HOST_NAME': 'localhost',
229245
'ZIPKIN_EXPORTER_PORT': 9411,
230-
231246
}
232247
233-
Then the requests will be automatically traced.
234-
235248
Webapp2
236249
~~~~~~~
237250

@@ -244,12 +257,13 @@ Webapp2
244257
with tracer.span(name='span1'):
245258
do_something_to_trace()
246259
260+
247261
Service Integration
248262
-------------------
249263

250-
Opencensus supports integration with various popular services (working in progress).
251-
By default there is no integration, you need to specify which service(s) you
252-
want to instrument. Usage for enabling MySQL instrumentation like below:
264+
Opencensus supports integration with various popular outbound services such as
265+
MySQL and Requests. To enable integration you will need to pass the list of
266+
services to census:
253267

254268
.. code:: python
255269
@@ -258,96 +272,57 @@ want to instrument. Usage for enabling MySQL instrumentation like below:
258272
259273
import mysql.connector
260274
261-
INTEGRATIONS = ['mysql', 'postgresql']
275+
# Trace both mysql-connection and psycopg2
276+
integration = ['mysql', 'postgresql']
262277
263-
config_integration.trace_integrations(INTEGRATIONS)
278+
config_integration.trace_integrations(integration)
264279
265-
tracer = request_tracer.RequestTracer()
266-
267-
conn = mysql.connector.connect(user='user', password='password')
268-
cursor = conn.cursor()
269-
query = 'SELECT 2*3'
270-
cursor.execute(query)
271280
272281
MySQL
273282
~~~~~
274283

275-
The integration with MySQL is based on the mysql-connector-python library,
276-
github link is https://github.com/mysql/mysql-connector-python.
277-
278-
Run this command to install this package,
284+
The integration with MySQL supports the `mysql-connector`_ library and is specified
285+
to ``trace_integrations`` using ``'mysql'``.
279286

280-
.. code:: bash
281-
282-
pip install mysql-connector
287+
.. _mysql-connector: https://pypi.org/project/mysql-connector
283288

284289
PostgreSQL
285290
~~~~~~~~~~
286291

287-
The integration with PostgreSQL is based on the psycopg2 library, which is the
288-
most popular PostgreSQL python library based on the download data in PSF stats.
289-
290-
Run this command to install this package,
292+
The integration with PostgreSQL supports the `psycopg2`_ library and is specified
293+
to ``trace_integrations`` using ``'postgresql'``.
291294

292-
.. code:: bash
295+
.. _psycopg2: https://pypi.org/project/psycopg2
293296

294-
pip install psycopg2
295297

296298
SQLAlchemy
297299
~~~~~~~~~~
298300

299-
Note that if enabled tracing both SQLAlchemy and the database it connected,
300-
the communication between SQLAlchemy and the database will also be traced.
301-
To avoid the verbose spans, you can just trace SQLAlchemy.
301+
You can trace usage of `SQLAlchemy`_, regardless of the underlying database, by
302+
specifying ``'sqlalchemy'`` to ``trace_integrations``.
302303

303-
Run this command to install the SQLAlchemy package,
304+
.. _SQLAlchemy: https://pypi.org/project/SQLAlchemy
304305

305-
.. code:: bash
306-
307-
pip install sqlalchemy
306+
.. note:: If you enable tracing of SQLAlchemy and the underlying database
307+
driver, you will get duplicate spans. Instead, just trace SQLAlchemy.
308308

309309
Requests
310310
~~~~~~~~
311311

312-
Supports tracing the requests methods including get, post, put, delete, head
313-
and options. The request url and status code will be added to the span labels.
314-
315-
As most of the Google Cloud client libraries supports HTTP as the background
316-
transport, to trace the client libraries requests, you can turn on the trace
317-
integration with requests module.
312+
Census can trace HTTP requests made with the `Requests`_ library. The request URL,
313+
method, and status will be collected.
318314

319-
.. code:: python
315+
You can enable Requests integration by specifying ``'requests'`` to ``trace_integrations``.
320316

321-
import requests
322-
import uuid
323317

324-
from opencensus.trace.config_integration import trace_integrations
325-
from opencensus.trace.request_tracer import RequestTracer
326-
327-
from google.cloud import bigquery
328-
329-
# Create a tracer
330-
tracer = RequestTracer()
331-
332-
# Integrate with requests module
333-
trace_integrations(['requests'])
334-
335-
# Run a query to trace
336-
query = 'SELECT * FROM sample_table'
337-
client = bigquery.Client()
338-
query_job = client.run_async_query(str(uuid.uuid4()), query)
339-
340-
# Start the query job and wait it to complete
341-
query_job.begin()
342-
query_job.result()
318+
Contributing
319+
------------
343320

344-
Then you will get the request trace data from the start of executing the query
345-
to the end.
321+
Contributions to this library are always welcome and highly encouraged.
346322

347-
Status
348-
------
323+
See `CONTRIBUTING <CONTRIBUTING.md>`__ for more information on how to
324+
get started.
349325

350-
Currently under active development.
351326

352327
Development
353328
-----------
@@ -381,14 +356,6 @@ Tests
381356

382357
# Then just run the tracers normally as you want to test.
383358

384-
Contributing
385-
------------
386-
387-
Contributions to this library are always welcome and highly encouraged.
388-
389-
See `CONTRIBUTING <CONTRIBUTING.md>`__ for more information on how to
390-
get started.
391-
392359
License
393360
-------
394361

@@ -397,4 +364,4 @@ Apache 2.0 - See `LICENSE <LICENSE>`__ for more information.
397364
Disclaimer
398365
----------
399366

400-
This is not an official Google product.
367+
This is not an official Google product.

0 commit comments

Comments
 (0)