Docs » Instrument back-end applications to send spans to Splunk APM » Instrument Python applications for Splunk Observability Cloud » Instrument Python frameworks for Splunk Observability Cloud

Instrument Python frameworks for Splunk Observability Cloud ๐Ÿ”—

If youโ€™re instrumenting a Python application or service that uses Django or uWSGI, follow these additional steps after youโ€™ve followed all the steps in Instrument your Python application for Splunk Observability Cloud.

Instrument your Django application ๐Ÿ”—

To automatically instrument Django applications, set the DJANGO_SETTINGS_MODULE environment variable to the same value in manage.py or wsgi.py.

For example, if your manage.py file sets the environment variable to mydjangoproject.settings and you start your project using ./manage.py runserver, you can run the following commands:

export DJANGO_SETTINGS_MODULE=mydjangoproject.settings
splunk-py-trace python3 ./manage.py runserver --noreload

Instrument your uWSGI application ๐Ÿ”—

When using uWSGI, you must configure tracing as a response to the post_fork signal:

import uwsgidecorators
from splunk_otel.tracing import start_tracing

@uwsgidecorators.postfork
def setup_tracing():
   start_tracing()

Customize and use the following snippet to run the application:

uwsgi --http :9090 --wsgi-file <your_app.py> --callable <your_wsgi_callable> --master --enable-threads

Place the snippet in the main Python script that uWSGI imports and loads.

Caution

Do not run your uWSGI application using splunk-py-trace, as it could have unintended consequences.

uWSGI and Flask ๐Ÿ”—

When using both uSWGI and Flask, calling start_tracing() only autoinstruments new Flask instances. To instrument an existing Flask app, import and call the Flask instrumentor:

# app.py
import uwsgidecorators
from splunk_otel.tracing import start_tracing
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from flask import Flask

app = Flask(__name__)

@uwsgidecorators.postfork
def setup_tracing():
   start_tracing()
   # Instrument the Flask app instance explicitly
   FlaskInstrumentor().instrument_app(app)

@app.route('/')
def hello_world():
   return 'Hello, World!'

Customize and use the following snippet to run the application:

uwsgi --http :9090 --wsgi-file <your_app.py> --callable <your_wsgi_callable> --master --enable-threads