opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentati...
Leighton Chen ad1ef6e920 changelog + version 2021-10-12 20:47:14 -07:00
..
src/opentelemetry/instrumentation/celery changelog + version 2021-10-12 20:47:14 -07:00
tests use f-strings instead of format (#693) 2021-09-28 19:12:47 +00:00
LICENSE Fix error in license files (#521) 2021-05-28 09:16:58 -07:00
MANIFEST.in Rename remaining framework packages from "ext" to "instrumentation" (#969) 2020-08-04 19:10:51 -07:00
README.rst Added link to examples folder in each instrumentation (#438) 2021-04-13 14:56:30 -07:00
setup.cfg changelog + version 2021-10-12 20:47:14 -07:00
setup.py update open calls to pass encoding (#684) 2021-09-22 10:16:14 -07:00

README.rst

OpenTelemetry Celery Instrumentation
====================================

|pypi|

.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-celery.svg
   :target: https://pypi.org/project/opentelemetry-instrumentation-celery/

Instrumentation for Celery.


Installation
------------

::

    pip install opentelemetry-instrumentation-celery

Usage
-----

* Start broker backend

::
    docker run -p 5672:5672 rabbitmq


* Run instrumented task

.. code-block:: python

    from opentelemetry import trace
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import BatchSpanProcessor
    from opentelemetry.instrumentation.celery import CeleryInstrumentor

    from celery import Celery
    from celery.signals import worker_process_init

    @worker_process_init.connect(weak=False)
    def init_celery_tracing(*args, **kwargs):
        trace.set_tracer_provider(TracerProvider())
        span_processor = BatchSpanProcessor(ConsoleSpanExporter())
        trace.get_tracer_provider().add_span_processor(span_processor)
        CeleryInstrumentor().instrument()

    app = Celery("tasks", broker="amqp://localhost")

    @app.task
    def add(x, y):
        return x + y

    add.delay(42, 50)


Setting up tracing 
--------------------

When tracing a celery worker process, tracing and instrumention both must be initialized after the celery worker
process is initialized. This is required for any tracing components that might use threading to work correctly
such as the BatchSpanProcessor. Celery provides a signal called ``worker_process_init`` that can be used to
accomplish this as shown in the example above.

References
----------
* `OpenTelemetry Celery Instrumentation <https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/celery/celery.html>`_
* `OpenTelemetry Project <https://opentelemetry.io/>`_
* `OpenTelemetry Python Examples <https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples>`_