diff --git a/.changelog/5492.fixed b/.changelog/5492.fixed new file mode 100644 index 00000000000..426a340696a --- /dev/null +++ b/.changelog/5492.fixed @@ -0,0 +1 @@ +`docs`: fix Django example so spans are printed to stdout for both manual and auto-instrumentation setups diff --git a/docs/examples/django/README.rst b/docs/examples/django/README.rst index 4f1771fbe68..4076029bff1 100644 --- a/docs/examples/django/README.rst +++ b/docs/examples/django/README.rst @@ -47,7 +47,8 @@ an ``opentelemetry.instrumentation.django.DjangoInstrumentor`` to instrument the Clone the ``opentelemetry-python`` repository and go to ``opentelemetry-python/docs/examples/django``. Once there, open the ``manage.py`` file. The call to ``DjangoInstrumentor().instrument()`` -in ``main`` is all that is needed to make the app be instrumented. +in ``main`` is what instruments the app, and the ``TracerProvider`` configured with a +``ConsoleSpanExporter`` right above it is what prints the generated spans to stdout. Run the Django app with ``python manage.py runserver --noreload``. The ``--noreload`` flag is needed to avoid Django from running ``main`` twice. @@ -110,9 +111,13 @@ Django's instrumentation can be disabled by setting the following environment va Auto Instrumentation -------------------- -This same example can be run using auto instrumentation. Comment out the call -to ``DjangoInstrumentor().instrument()`` in ``main``, then Run the django app -with ``opentelemetry-instrument python manage.py runserver --noreload``. +This same example can be run using auto instrumentation. Comment out the +``TracerProvider`` setup and the call to ``DjangoInstrumentor().instrument()`` +in ``main``, then run the Django app with +``opentelemetry-instrument --traces_exporter console python manage.py runserver --noreload``. +The ``--traces_exporter console`` option makes ``opentelemetry-instrument`` +print spans to stdout, matching the manual setup above; without it the default +OTLP exporter is used and nothing is shown in the console. Repeat the steps with the client, the result should be the same. Usage with Auto Instrumentation and uWSGI @@ -126,7 +131,7 @@ first install uWSGI in the previous virtual environment: Once that is done, run the server with ``uwsgi`` from the directory that contains ``instrumentation_example``: -``opentelemetry-instrument uwsgi --http :8000 --module instrumentation_example.wsgi`` +``opentelemetry-instrument --traces_exporter console uwsgi --http :8000 --module instrumentation_example.wsgi`` This should start one uWSGI worker in your console. Open up a browser and point it to ``localhost:8000``. This request should display a span exported in the diff --git a/docs/examples/django/manage.py b/docs/examples/django/manage.py index 2ddd5e8eb93..861c8a88f20 100755 --- a/docs/examples/django/manage.py +++ b/docs/examples/django/manage.py @@ -10,7 +10,13 @@ import os import sys +from opentelemetry import trace from opentelemetry.instrumentation.django import DjangoInstrumentor +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import ( + ConsoleSpanExporter, + SimpleSpanProcessor, +) def main(): @@ -18,7 +24,17 @@ def main(): "DJANGO_SETTINGS_MODULE", "instrumentation_example.settings" ) - # This call is what makes the Django application be instrumented + # Set up a tracer provider with a console exporter so that the spans + # generated below are printed to stdout. Comment out this block when + # running with auto instrumentation (``opentelemetry-instrument``), which + # configures the tracer provider and exporter for you. + trace.set_tracer_provider(TracerProvider()) + trace.get_tracer_provider().add_span_processor( + SimpleSpanProcessor(ConsoleSpanExporter()) + ) + + # This call is what makes the Django application be instrumented. + # Comment it out when running with auto instrumentation. DjangoInstrumentor().instrument() try: