opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentati...
Leighton Chen 698acb835e
Release for 1.1.0/0.20b0 (#462)
2021-04-20 14:20:16 -07:00
..
src/opentelemetry/instrumentation/celery Release for 1.1.0/0.20b0 (#462) 2021-04-20 14:20:16 -07:00
tests Sync with auto generated semantic convention constants (#428) 2021-04-19 23:37:10 -07:00
LICENSE Rename remaining framework packages from "ext" to "instrumentation" (#969) 2020-08-04 19:10:51 -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 Release for 1.1.0/0.20b0 (#462) 2021-04-20 14:20:16 -07:00
setup.py Rename remaining framework packages from "ext" to "instrumentation" (#969) 2020-08-04 19:10:51 -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>`_