Prerequisites
- Create an Axiom account.
- Create a dataset in Axiom where you send your data.
- Create an API token in Axiom with permissions to update the dataset you have created.
Install required dependencies
Install the necessary Python dependencies by running the following command in your terminal:requirements.txt file:
Get started with a Django project
- Create a new Django project if you don’t have one already:
- Go to your project directory:
- Create a Django app:
Set up OpenTelemetry Tracing
Update manage.py to initialize tracing
This code initializes OpenTelemetry instrumentation for Django when the project is run. Adding DjangoInstrumentor().instrument() ensures that all incoming HTTP requests are automatically traced, which helps in monitoring the app’s performance and behavior without manually adding trace points in every view.
Create exporter.py for tracer configuration
This file configures the OpenTelemetry tracing provider and exporter. By setting up a TracerProvider and configuring the OTLPSpanExporter, you define how and where the trace data is sent. The BatchSpanProcessor is used to batch and send trace spans efficiently. The tracer created at the end is used throughout the app to create new spans.
Use the tracer in your views
In this step, modify the Django views to use the tracer defined inexporter.py. By wrapping the view logic within tracer.start_as_current_span, you create spans that capture the execution of these views. This provides detailed insights into the performance of individual request handlers, helping to identify slow operations or errors.
Update settings.py for OpenTelemetry instrumentation
In your Django project’s settings.py, add the OpenTelemetry Django instrumentation. This setup automatically creates spans for HTTP requests handled by Django:
Update the app’s urls.py to include the views
Include your views in the URL routing by updatingurls.py Updating urls.py with these entries sets up the URL routing for the Django app. It connects the URL paths to the corresponding view functions. This ensures that when users visit the specified paths, the corresponding views are executed, and their spans are created and sent to Axiom for monitoring.
Run the project
Run the command to start the Django project:http://127.0.0.1:8000/rolldice to interact with your Django app. Each time you load the page, the app displays a message and sends the collected traces to Axiom.
Send data from an existing Django project
Manual instrumentation
Manual instrumentation in Python with OpenTelemetry involves adding code to create and manage spans around the blocks of code you want to trace. This approach allows for precise control over the trace data.- Install necessary OpenTelemetry packages to enable manual tracing capabilities in your Django app.
- Set up OpenTelemetry in your Django project to manually trace app activities.
- Configure OpenTelemetry to your Django settings to capture telemetry data upon app startup.
- Manually instrument views to create custom spans that trace specific operations within your Django app.
- Apply manual tracing to database operations by wrapping database cursor executions with OpenTelemetry spans.
Automatic instrumentation
Automatic instrumentation in Django with OpenTelemetry simplifies the process of adding telemetry data to your app. It uses pre-built libraries that automatically instrument the frameworks and libraries.- Install required packages that support automatic instrumentation.
- Automatically configure OpenTelemetry to trace Django app operations without manual span management.
- Initialize OpenTelemetry in Django to capture telemetry data from all HTTP requests automatically.
- Update
manage.pyto include OpenTelemetry initialization, ensuring that tracing is active before the Django app fully starts.
- (Optional) Combine automatic and custom manual spans in Django views to enhance trace details for specific complex operations.
Reference
List of OpenTelemetry trace fields
List of imported libraries
Theexporter.py file and other relevant parts of the Django OpenTelemetry setup import the following libraries:
exporter.py
This module creates and manages trace data in your app. It creates spans and tracers which track the execution flow and performance of your app.
manage.py
The DjangoInstrumentor module is used to automatically instrument Django applications. It integrates OpenTelemetry with Django, enabling automatic creation of spans for incoming HTTP requests handled by Django, and simplifying the process of adding telemetry to your app.
views.py
This import brings in the tracer instance defined in exporter.py, which is used to create spans for tracing the execution of Django views. By wrapping view logic within tracer.start_as_current_span, it captures detailed insights into the performance of individual request handlers.