Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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
31 changes: 29 additions & 2 deletions docs/examples/django/README.rst
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you also update https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/django/README.rst?plain=1#L113-L114 to say that the TracerProvider calls should also be commented out when doing auto-instrumentation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the code handling tracing already setup instead?

Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,35 @@ 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.
Once there, open the ``manage.py`` file. To see spans output to console, you need to:

1. Set up a ``TracerProvider``
2. Add a ``SpanProcessor`` with an exporter (e.g., ``ConsoleSpanExporter`` for stdout)
3. Call ``DjangoInstrumentor().instrument()`` to instrument the Django app

The ``manage.py`` example includes this setup:

.. code-block:: python

from opentelemetry import trace
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
BatchSpanProcessor,
ConsoleSpanExporter,
)

# Set up tracing with console exporter to see spans in stdout
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(ConsoleSpanExporter())
)

# This call is what makes the Django application be instrumented
DjangoInstrumentor().instrument()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR!

To avoid repeating, I'd suggest not including this code block in the Readme and just encourage readers to look at manage.py. Maybe: remove this block, then update L49 to say something like

Once there, open the ``manage.py`` file which uses the OpenTelemetry SDK to set up tracing with console exporter and manual instrumentation of Django:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed code block and added comment to encourage readers to look at manage.py


Without the ``TracerProvider`` and ``SpanProcessor`` setup, the instrumentation will
capture traces but they won't be exported anywhere (no output will be visible).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest removing this because manage.py already says

# Set up tracing with console exporter to see spans in stdout

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


Run the Django app with ``python manage.py runserver --noreload``.
The ``--noreload`` flag is needed to avoid Django from running ``main`` twice.
Expand Down
12 changes: 12 additions & 0 deletions docs/examples/django/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@
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 (
BatchSpanProcessor,
ConsoleSpanExporter,
)


def main():
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE", "instrumentation_example.settings"
)

# Set up tracing with console exporter to see spans in stdout
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(ConsoleSpanExporter())
)

# This call is what makes the Django application be instrumented
DjangoInstrumentor().instrument()

Expand Down
Loading