|
1 | | -OpenCensus for Python - A stats collection and distributed tracing framework |
2 | | -============================================================================ |
3 | | - |
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. |
7 | | - |
8 | | - .. _Census: https://github.com/census-instrumentation |
| 1 | +OpenCensus - A stats collection and distributed tracing framework |
| 2 | +================================================================= |
9 | 3 |
|
| 4 | +|gitter| |
10 | 5 | |circleci| |
| 6 | +|pypi| |
11 | 7 |
|
12 | 8 | .. |circleci| image:: https://circleci.com/gh/census-instrumentation/opencensus-python.svg?style=shield |
13 | 9 | :target: https://circleci.com/gh/census-instrumentation/opencensus-python |
| 10 | +.. |gitter| image:: https://badges.gitter.im/census-instrumentation/lobby.svg |
| 11 | + :target: https://gitter.im/census-instrumentation/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge |
| 12 | +.. |pypi| image:: https://badge.fury.io/py/opencensus.svg |
| 13 | + :target: https://pypi.org/project/opencensus/ |
| 14 | + |
| 15 | +`OpenCensus`_ for Python. OpenCensus provides a framework to measure a |
| 16 | +server's resource usage and collect performance stats. This repository |
| 17 | +contains Python related utilities and supporting software needed by |
| 18 | +OpenCensus. |
| 19 | + |
| 20 | +.. _OpenCensus: https://github.com/census-instrumentation |
14 | 21 |
|
15 | 22 | - `API Documentation`_ |
16 | 23 |
|
@@ -74,7 +81,7 @@ You can collect traces using the ``Tracer`` `context manager`_: |
74 | 81 | with tracer.span(name='span2') as span2: |
75 | 82 | do_something_to_trace() |
76 | 83 |
|
77 | | -Census will collect everything within the ``with`` statement as a single span. |
| 84 | +OpenCensus will collect everything within the ``with`` statement as a single span. |
78 | 85 |
|
79 | 86 | Alternatively, you can explicitly start and end a span: |
80 | 87 |
|
@@ -120,7 +127,7 @@ the traces are printed to stdout in JSON format. Other options include |
120 | 127 | writing to a file, sending to Python logging, or reporting to |
121 | 128 | Stackdriver. |
122 | 129 |
|
123 | | -This example shows how to configure Census to save the traces to a |
| 130 | +This example shows how to configure OpenCensus to save the traces to a |
124 | 131 | file: |
125 | 132 |
|
126 | 133 | .. code:: python |
@@ -154,7 +161,7 @@ By default, traces are exported synchronously, which introduces latency during |
154 | 161 | your code's execution. To avoid blocking code execution, you can initialize |
155 | 162 | your exporter to use a background thread. |
156 | 163 |
|
157 | | -This example shows how to configure Census to use a background thread: |
| 164 | +This example shows how to configure OpenCensus to use a background thread: |
158 | 165 |
|
159 | 166 | .. code:: python |
160 | 167 |
|
@@ -235,230 +242,38 @@ For Django, you can configure the blacklist in the ``OPENCENSUS_TRACE_PARAMS`` i |
235 | 242 | .. note:: By default, the health check path for the App Engine flexible environment is not traced, |
236 | 243 | but you can turn it on by excluding it from the blacklist setting. |
237 | 244 |
|
238 | | -Framework Integration |
239 | | ---------------------- |
240 | | - |
241 | | -Census supports integration with popular web frameworks including Django, |
242 | | -Flask, and Pyramid. When the application receives a HTTP request, the tracer |
243 | | -will automatically generate a span context using the trace information |
244 | | -extracted from the request headers and propagated to the child spans. |
245 | | - |
246 | | -Flask |
247 | | -~~~~~ |
248 | | - |
249 | | -In your application, use the middleware to wrap your app and the |
250 | | -requests will be automatically traced. |
251 | | - |
252 | | -.. code:: python |
253 | | -
|
254 | | - from opencensus.trace.ext.flask.flask_middleware import FlaskMiddleware |
255 | | -
|
256 | | - app = flask.Flask(__name__) |
257 | | -
|
258 | | - # You can also specify the sampler, exporter, propagator in the middleware, |
259 | | - # default is using `AlwaysOnSampler` as sampler, `PrintExporter` as exporter, |
260 | | - # `GoogleCloudFormatPropagator` as propagator. |
261 | | - middleware = FlaskMiddleware(app) |
262 | | -
|
263 | | -Django |
264 | | -~~~~~~ |
265 | | - |
266 | | -For tracing Django requests, you will need to add the following line to |
267 | | -the ``MIDDLEWARE_CLASSES`` section in the Django ``settings.py`` file. |
268 | | - |
269 | | -.. code:: python |
270 | | -
|
271 | | - MIDDLEWARE_CLASSES = [ |
272 | | - ... |
273 | | - 'opencensus.trace.ext.django.middleware.OpencensusMiddleware', |
274 | | - ] |
275 | | -
|
276 | | -And add this line to the ``INSTALLED_APPS`` section: |
277 | | - |
278 | | -.. code:: python |
279 | | -
|
280 | | - INSTALLED_APPS = [ |
281 | | - ... |
282 | | - 'opencensus.trace.ext.django', |
283 | | - ] |
284 | | -
|
285 | | -You can configure the sampler, exporter, propagator using the ``OPENCENSUS_TRACE`` setting in |
286 | | -``settings.py``: |
287 | | - |
288 | | -.. code:: python |
289 | | -
|
290 | | - OPENCENSUS_TRACE = { |
291 | | - 'SAMPLER': 'opencensus.trace.samplers.probability.ProbabilitySampler', |
292 | | - 'EXPORTER': 'opencensus.trace.exporters.print_exporter.PrintExporter', |
293 | | - 'PROPAGATOR': 'opencensus.trace.propagation.google_cloud_format.' |
294 | | - 'GoogleCloudFormatPropagator', |
295 | | - } |
296 | | -
|
297 | | -You can configure the sampling rate and other parameters using the ``OPENCENSUS_TRACE_PARAMS`` |
298 | | -setting in ``settings.py``: |
299 | | - |
300 | | -.. code:: python |
301 | | -
|
302 | | - OPENCENSUS_TRACE_PARAMS = { |
303 | | - 'BLACKLIST_PATHS': ['/_ah/health'], |
304 | | - 'GCP_EXPORTER_PROJECT': None, |
305 | | - 'SAMPLING_RATE': 0.5, |
306 | | - 'SERVICE_NAME': 'my_service', |
307 | | - 'ZIPKIN_EXPORTER_HOST_NAME': 'localhost', |
308 | | - 'ZIPKIN_EXPORTER_PORT': 9411, |
309 | | - 'ZIPKIN_EXPORTER_PROTOCOL': 'http', |
310 | | - 'JAEGER_EXPORTER_HOST_NAME': None, |
311 | | - 'JAEGER_EXPORTER_PORT': None, |
312 | | - 'JAEGER_EXPORTER_AGENT_HOST_NAME': 'localhost', |
313 | | - 'JAEGER_EXPORTER_AGENT_PORT': 6831 |
314 | | - } |
315 | | -
|
316 | | -
|
317 | | -Pyramid |
318 | | -~~~~~~~ |
319 | | - |
320 | | -In your application, add the pyramid tween and your requests will be |
321 | | -traced. |
322 | | - |
323 | | -.. code:: python |
324 | | -
|
325 | | - def main(global_config, **settings): |
326 | | - config = Configurator(settings=settings) |
327 | | -
|
328 | | - config.add_tween('opencensus.trace.ext.pyramid' |
329 | | - '.pyramid_middleware.OpenCensusTweenFactory') |
330 | | -
|
331 | | -To configure the sampler, exporter, and propagator, pass the instances |
332 | | -into the pyramid settings |
333 | | - |
334 | | -.. code:: python |
335 | | -
|
336 | | - from opencensus.trace.exporters import print_exporter |
337 | | - from opencensus.trace.propagation import google_cloud_format |
338 | | - from opencensus.trace.samplers import probability |
339 | | -
|
340 | | - settings = {} |
341 | | - settings['OPENCENSUS_TRACE'] = { |
342 | | - 'EXPORTER': print_exporter.PrintExporter(), |
343 | | - 'SAMPLER': probability.ProbabilitySampler(rate=0.5), |
344 | | - 'PROPAGATOR': google_cloud_format.GoogleCloudFormatPropagator(), |
345 | | - } |
346 | | -
|
347 | | - config = Configurator(settings=settings) |
348 | | -
|
349 | | -gRPC Integration |
350 | | ----------------- |
351 | | - |
352 | | -OpenCensus provides the implementation of interceptors for both the client side |
353 | | -and server side to instrument the gRPC requests and responses. The client |
354 | | -interceptors are used to create a decorated channel that intercepts client |
355 | | -gRPC calls and server interceptors act as decorators over handlers. |
356 | | - |
357 | | -gRPC interceptor is a new feature in the grpcio1.8.0 release, please upgrade |
358 | | -your grpcio to the latest version to use this feature. |
359 | | - |
360 | | -For sample usage, please refer to the hello world example in the examples |
361 | | -directory. |
362 | | - |
363 | | -More information about the gRPC interceptors please see the `proposal`_. |
364 | | - |
365 | | -.. _proposal: https://github.com/mehrdada/proposal/blob/python-interceptors/L13-Python-Interceptors.md |
366 | | - |
367 | | -Service Integration |
368 | | -------------------- |
369 | | - |
370 | | -Opencensus supports integration with various popular outbound services such as |
371 | | -SQL packages, Requests and Google Cloud client libraries. To enable integration |
372 | | -services to census: you will need to pass the list of services to census: |
373 | | - |
374 | | -.. code:: python |
375 | | -
|
376 | | - from opencensus.trace import config_integration |
377 | | - from opencensus.trace import tracer as tracer_module |
378 | | -
|
379 | | - import mysql.connector |
380 | | -
|
381 | | - # Trace both mysql-connection and psycopg2 |
382 | | - integration = ['mysql', 'postgresql'] |
383 | | -
|
384 | | - config_integration.trace_integrations(integration) |
385 | | -
|
386 | | -
|
387 | | -MySQL |
388 | | -~~~~~ |
389 | | - |
390 | | -The integration with MySQL supports the `mysql-connector`_ library and is specified |
391 | | -to ``trace_integrations`` using ``'mysql'``. |
392 | | - |
393 | | -.. _mysql-connector: https://pypi.org/project/mysql-connector |
394 | | - |
395 | | -PostgreSQL |
396 | | -~~~~~~~~~~ |
397 | | - |
398 | | -The integration with PostgreSQL supports the `psycopg2`_ library and is specified |
399 | | -to ``trace_integrations`` using ``'postgresql'``. |
400 | | - |
401 | | -.. _psycopg2: https://pypi.org/project/psycopg2 |
402 | | - |
403 | | - |
404 | | -SQLAlchemy |
405 | | -~~~~~~~~~~ |
406 | | - |
407 | | -You can trace usage of the `sqlalchemy package`_, regardless of the underlying |
408 | | -database, by specifying ``'sqlalchemy'`` to ``trace_integrations``. |
409 | | - |
410 | | -.. _SQLAlchemy package: https://pypi.org/project/SQLAlchemy |
411 | | - |
412 | | -.. note:: If you enable tracing of SQLAlchemy as well as the underlying database |
413 | | - driver, you will get duplicate spans. Instead, just trace SQLAlchemy. |
414 | | - |
415 | | -Requests |
416 | | -~~~~~~~~ |
417 | | - |
418 | | -Census can trace HTTP requests made with the `Requests package`_. The request URL, |
419 | | -method, and status will be collected. |
420 | | - |
421 | | -You can enable Requests integration by specifying ``'requests'`` to ``trace_integrations``. |
422 | | - |
423 | | -It's possible to configure a list of URL you don't want traced. By default the request to exporter |
424 | | -won't be traced. It's configurable by giving an array of hostname/port to the attribute |
425 | | -``blacklist_hostnames`` in OpenCensus context's attributes: |
426 | | - |
427 | | -.. code:: python |
428 | | -
|
429 | | - execution_context.set_opencensus_attr('blacklist_hostnames',['hostname:port']) |
430 | | -
|
431 | | -Only the hostname must be specified if only the hostname is specified in the URL request. |
432 | | - |
433 | | -.. _Requests package: https://pypi.python.org/pypi/requests |
434 | | - |
435 | | -Httplib |
436 | | -~~~~~~~~ |
437 | | - |
438 | | -Census can trace HTTP requests made with the httplib library. |
439 | | - |
440 | | -You can enable Requests integration by specifying ``'httplib'`` to ``trace_integrations``. |
441 | | - |
442 | | -It's possible to configure a list of URL you don't want traced. See requests integration |
443 | | -for more information. The only difference is that you need to specify hostname and port |
444 | | -every time. |
445 | | - |
446 | | -Google Cloud Client Libraries |
447 | | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
448 | | - |
449 | | -Census can trace HTTP and gRPC requests made with the `Cloud client libraries`_. |
450 | | -The request URL, method, and status will be collected. |
451 | | - |
452 | | -You can enable Google Cloud client libraries integration by specifying ``'google_cloud_clientlibs'`` to ``trace_integrations``. |
453 | | - |
454 | | -.. _Cloud client libraries: https://github.com/GoogleCloudPlatform/google-cloud-python#google-cloud-python-client |
455 | | - |
456 | | -Threading |
457 | | -~~~~~~~~~ |
458 | | - |
459 | | -Census can propagate trace across threads when using the Threading package. |
| 245 | +Integration |
| 246 | +----------- |
460 | 247 |
|
461 | | -You can enable Threading integration by specifying ``'threading'`` to ``trace_integrations``. |
| 248 | +OpenCensus supports integration with popular web frameworks, client libraries and built-in libraries. |
| 249 | + |
| 250 | +- `Django`_ |
| 251 | +- `Flask`_ |
| 252 | +- `Google Cloud Client Libraries`_ |
| 253 | +- `gRPC`_ |
| 254 | +- `httplib`_ |
| 255 | +- `MySQL`_ |
| 256 | +- `PostgreSQL`_ |
| 257 | +- `pymongo`_ |
| 258 | +- `PyMySQL`_ |
| 259 | +- `Pyramid`_ |
| 260 | +- `requests`_ |
| 261 | +- `SQLAlchemy`_ |
| 262 | +- `threading`_ |
| 263 | + |
| 264 | +.. _Django: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-django |
| 265 | +.. _Flask: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-flask |
| 266 | +.. _Google Cloud Client Libraries: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-google-cloud-clientlibs |
| 267 | +.. _gRPC: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-grpc |
| 268 | +.. _httplib: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-httplib |
| 269 | +.. _MySQL: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-mysql |
| 270 | +.. _PostgreSQL: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-postgresql |
| 271 | +.. _pymongo: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-pymongo |
| 272 | +.. _PyMySQL: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-pymysql |
| 273 | +.. _Pyramid: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-pyramid |
| 274 | +.. _requests: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-requests |
| 275 | +.. _SQLAlchemy: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-sqlalchemy |
| 276 | +.. _threading: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-threading |
462 | 277 |
|
463 | 278 | ------ |
464 | 279 | Stats |
|
0 commit comments