Skip to content

Commit 9dd5894

Browse files
Merge pull request #1 from BrianPetkovsek/copilot/fix-bug-in-current-issue
fix(docs): add TracerProvider setup to Django instrumentation example
2 parents 0b7c1d8 + a4b5812 commit 9dd5894

File tree

3 files changed

+41
-82
lines changed

3 files changed

+41
-82
lines changed

current_issue.md

Lines changed: 0 additions & 80 deletions
This file was deleted.

docs/examples/django/README.rst

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,35 @@ an ``opentelemetry.instrumentation.django.DjangoInstrumentor`` to instrument the
4646

4747
Clone the ``opentelemetry-python`` repository and go to ``opentelemetry-python/docs/examples/django``.
4848

49-
Once there, open the ``manage.py`` file. The call to ``DjangoInstrumentor().instrument()``
50-
in ``main`` is all that is needed to make the app be instrumented.
49+
Once there, open the ``manage.py`` file. To see spans output to console, you need to:
50+
51+
1. Set up a ``TracerProvider``
52+
2. Add a ``SpanProcessor`` with an exporter (e.g., ``ConsoleSpanExporter`` for stdout)
53+
3. Call ``DjangoInstrumentor().instrument()`` to instrument the Django app
54+
55+
The ``manage.py`` example includes this setup:
56+
57+
.. code-block:: python
58+
59+
from opentelemetry import trace
60+
from opentelemetry.instrumentation.django import DjangoInstrumentor
61+
from opentelemetry.sdk.trace import TracerProvider
62+
from opentelemetry.sdk.trace.export import (
63+
BatchSpanProcessor,
64+
ConsoleSpanExporter,
65+
)
66+
67+
# Set up tracing with console exporter to see spans in stdout
68+
trace.set_tracer_provider(TracerProvider())
69+
trace.get_tracer_provider().add_span_processor(
70+
BatchSpanProcessor(ConsoleSpanExporter())
71+
)
72+
73+
# This call is what makes the Django application be instrumented
74+
DjangoInstrumentor().instrument()
75+
76+
Without the ``TracerProvider`` and ``SpanProcessor`` setup, the instrumentation will
77+
capture traces but they won't be exported anywhere (no output will be visible).
5178

5279
Run the Django app with ``python manage.py runserver --noreload``.
5380
The ``--noreload`` flag is needed to avoid Django from running ``main`` twice.

docs/examples/django/manage.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@
1818
import os
1919
import sys
2020

21+
from opentelemetry import trace
2122
from opentelemetry.instrumentation.django import DjangoInstrumentor
23+
from opentelemetry.sdk.trace import TracerProvider
24+
from opentelemetry.sdk.trace.export import (
25+
BatchSpanProcessor,
26+
ConsoleSpanExporter,
27+
)
2228

2329

2430
def main():
2531
os.environ.setdefault(
2632
"DJANGO_SETTINGS_MODULE", "instrumentation_example.settings"
2733
)
2834

35+
# Set up tracing with console exporter to see spans in stdout
36+
trace.set_tracer_provider(TracerProvider())
37+
trace.get_tracer_provider().add_span_processor(
38+
BatchSpanProcessor(ConsoleSpanExporter())
39+
)
40+
2941
# This call is what makes the Django application be instrumented
3042
DjangoInstrumentor().instrument()
3143

0 commit comments

Comments
 (0)