README.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ##################
  2. Django Opentracing
  3. ##################
  4. .. image:: https://travis-ci.org/opentracing-contrib/python-django.svg?branch=master
  5. :target: https://travis-ci.org/opentracing-contrib/python-django
  6. .. image:: https://img.shields.io/pypi/v/django_opentracing.svg
  7. :target: https://pypi.org/project/django_opentracing/
  8. .. image:: https://img.shields.io/pypi/pyversions/django_opentracing.svg
  9. :target: https://pypi.org/project/django_opentracing/
  10. .. image:: https://img.shields.io/pypi/dm/django_opentracing.svg
  11. :target: https://pypi.org/project/django_opentracing/
  12. This package enables distributed tracing in Django projects via `The OpenTracing Project`_. Once a production system contends with real concurrency or splits into many services, crucial (and formerly easy) tasks become difficult: user-facing latency optimization, root-cause analysis of backend errors, communication about distinct pieces of a now-distributed system, etc. Distributed tracing follows a request on its journey from inception to completion from mobile/browser all the way to the microservices.
  13. As core services and libraries adopt OpenTracing, the application builder is no longer burdened with the task of adding basic tracing instrumentation to their own code. In this way, developers can build their applications with the tools they prefer and benefit from built-in tracing instrumentation. OpenTracing implementations exist for major distributed tracing systems and can be bound or swapped with a one-line configuration change.
  14. If you want to learn more about the underlying python API, visit the python `source code`_.
  15. If you are migrating from the 0.x series, you may want to read the list of `breaking changes`_.
  16. .. _The OpenTracing Project: http://opentracing.io/
  17. .. _source code: https://github.com/opentracing/opentracing-python
  18. .. _breaking changes: #breaking-changes-from-0-x
  19. Installation
  20. ============
  21. Run the following command::
  22. $ pip install django_opentracing
  23. Setting up Tracing
  24. ==================
  25. In order to implement tracing in your system, add the following lines of code to your site's settings.py file:
  26. .. code-block:: python
  27. import django_opentracing
  28. # OpenTracing settings
  29. # if not included, defaults to True.
  30. # has to come before OPENTRACING_TRACING setting because python...
  31. OPENTRACING_TRACE_ALL = True
  32. # defaults to []
  33. # only valid if OPENTRACING_TRACE_ALL == True
  34. OPENTRACING_TRACED_ATTRIBUTES = ['arg1', 'arg2']
  35. # Callable that returns an `opentracing.Tracer` implementation.
  36. OPENTRACING_TRACER_CALLABLE = 'opentracing.Tracer'
  37. # Parameters for the callable (Depending on the tracer implementation chosen)
  38. OPENTRACING_TRACER_PARAMETERS = {
  39. 'example-parameter-host': 'collector',
  40. }
  41. If you want to directly override the ``DjangoTracing`` used, you can use the following. This may cause import loops (See #10)
  42. .. code-block:: python
  43. # some_opentracing_tracer can be any valid OpenTracing tracer implementation
  44. OPENTRACING_TRACING = django_opentracing.DjangoTracing(some_opentracing_tracer)
  45. **Note:** Valid request attributes to trace are listed `here`_. When you trace an attribute, this means that created spans will have tags with the attribute name and the request's value.
  46. .. _here: https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest
  47. Tracing All Requests
  48. ====================
  49. In order to trace all requests, ``OPENTRACING_TRACE_ALL`` needs to be set to ``True`` (the default). If you want to trace any attributes for all requests, then add them to ``OPENTRACING_TRACED_ATTRIBUTES``. For example, if you wanted to trace the path and method, then set ``OPENTRACING_TRACED_ATTRIBUTES = ['path', 'method']``.
  50. Tracing all requests uses the middleware django_opentracing.OpenTracingMiddleware, so add this to your settings.py file's ``MIDDLEWARE_CLASSES`` at the top of the stack.
  51. .. code-block:: python
  52. MIDDLEWARE_CLASSES = [
  53. 'django_opentracing.OpenTracingMiddleware',
  54. ... # other middleware classes
  55. ]
  56. Tracing Individual Requests
  57. ===========================
  58. If you don't want to trace all requests to your site, set ``OPENTRACING_TRACE_ALL`` to ``False``. Then you can use function decorators to trace individual view functions. This can be done by adding the following lines of code to views.py (or any other file that has url handler functions):
  59. .. code-block:: python
  60. from django.conf import settings
  61. tracing = settings.OPENTRACING_TRACING
  62. @tracing.trace(optional_args)
  63. def some_view_func(request):
  64. ... # do some stuff
  65. This tracing method doesn't use middleware, so there's no need to add it to your settings.py file.
  66. The optional arguments allow for tracing of request attributes. For example, if you want to trace metadata, you could pass in ``@tracing.trace('META')`` and ``request.META`` would be set as a tag on all spans for this view function.
  67. **Note:** If ``OPENTRACING_TRACE_ALL`` is set to ``True``, this decorator will be ignored, including any traced request attributes.
  68. Accessing Spans Manually
  69. ========================
  70. In order to access the span for a request, we've provided an method ``DjangoTracing.get_span(request)`` that returns the span for the request, if it is exists and is not finished. This can be used to log important events to the span, set tags, or create child spans to trace non-RPC events.
  71. Tracing an RPC
  72. ==============
  73. If you want to make an RPC and continue an existing trace, you can inject the current span into the RPC. For example, if making an http request, the following code will continue your trace across the wire:
  74. .. code-block:: python
  75. @tracing.trace()
  76. def some_view_func(request):
  77. new_request = some_http_request
  78. current_span = tracing.get_span(request)
  79. text_carrier = {}
  80. opentracing_tracer.inject(span, opentracing.Format.TEXT_MAP, text_carrier)
  81. for k, v in text_carrier.items():
  82. request.add_header(k,v)
  83. ... # make request
  84. Example
  85. =======
  86. Here is an `example`_ of a Django application that acts as both a client and server,
  87. with integrated OpenTracing tracers.
  88. .. _example: https://github.com/opentracing-contrib/python-django/tree/master/example
  89. Breaking changes from 0.x
  90. =========================
  91. Starting with the 1.0 version, a few changes have taken place from previous versions:
  92. * ``DjangoTracer`` has been renamed to ``DjangoTracing``, although ``DjangoTracer``
  93. can be used still as a deprecated name. Likewise for
  94. ``OPENTRACING_TRACER`` being renamed to ``OPENTRACING_TRACING``.
  95. * When using the middleware layer, ``OPENTRACING_TRACE_ALL`` defaults to ``True``.
  96. * When no ``opentracing.Tracer`` is provided, ``DjangoTracing`` will rely on the
  97. global tracer.
  98. Further Information
  99. ===================
  100. If you’re interested in learning more about the OpenTracing standard, please visit `opentracing.io`_ or `join the mailing list`_. If you would like to implement OpenTracing in your project and need help, feel free to send us a note at `community@opentracing.io`_.
  101. .. _opentracing.io: http://opentracing.io/
  102. .. _join the mailing list: http://opentracing.us13.list-manage.com/subscribe?u=180afe03860541dae59e84153&id=19117aa6cd
  103. .. _community@opentracing.io: community@opentracing.io