Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/5492.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`docs`: fix Django example so spans are printed to stdout for both manual and auto-instrumentation setups
15 changes: 10 additions & 5 deletions docs/examples/django/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 17 additions & 1 deletion docs/examples/django/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,31 @@
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():
os.environ.setdefault(
"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:
Expand Down
Loading