Configuring the Python collector
After the Instana Python collector is installed, you won't need to manually configure it for monitoring. It automatically starts collecting key metrics and distributed traces that are related to your Python processes. However, you can configure the individual components according to your specific requirements.
- General configuration
- Frameworks
- Tools
- Web servers
- Ignoring endpoints
- Configuring Kafka Trace correlation headers
- Additional information
General configuration
The Instana Python package aims to be a fully no-touch automatic solution for Python monitoring but still be fully configurable when needed. The following options are available to configure this package.
Enabling AutoProfile
AutoProfile generates and reports process profiles to Instana automatically and continuously. Learn more about profiles in the Analyze Profiles section.
To enable AutoProfile set the environment variable INSTANA_AUTOPROFILE=true. AutoProfile is supported for manual installation only. Make sure that the Instana
sensor is initialized in the main thread.
Establishing host agent communication
The Instana Python package tries to communicate with the Instana agent through IP 127.0.0.1 and as a fallback through the host's default gateway for containerized environments. If the agent is not available at either of these locations, you can use environment variables to configure where to look for the Instana host agent.
The environment variables must be set in the environment of the running process.
export INSTANA_AGENT_HOST = '127.0.0.1'
export INSTANA_AGENT_PORT = '42699'
See also:
Setting the service name
By default, the Instana makes the best effort in naming your services appropriately. If for any reason you want to customize how services are named, you can do so by setting an environment variable:
export INSTANA_SERVICE_NAME=myservice
See also the General Reference: Environment Variables for Language Sensors
Setting the process name
Use INSTANA_PROCESS_NAME to set a custom label for infrastructure entity that represents the Python process.
Package configuration
The Instana package includes a runtime configuration module that manages the configuration of various components.
As the package evolves, more options are added.
from instana.configurator import config
# To enable tracing context propagation across Asyncio ensure_future and create_task calls
# Default is false
config['asyncio_task_context_propagation']['enabled'] = True
Debugging and more verbosity
Setting INSTANA_DEBUG to a non-nil value enables more logging output that is generally useful for development and troubleshooting.
export INSTANA_DEBUG="true"
See also the General Reference: Environment Variables for Language Sensors
Disabling automatic instrumentation
This Instana package includes automatic instrumentation that is initialized on package load. This instrumentation provides distributed tracing information to your Instana dashboard. To see the complete list of automatic instrumentation, see the Supported versions document.
You can disable automatic instrumentation (tracing) by setting the environment variable INSTANA_DISABLE_AUTO_INSTR, which suppresses the loading of instrumentation that is built into the tracer.
export INSTANA_DISABLE_AUTO_INSTR="true"
Tracing root exit span without entry span
By default, the Instana Python Tracer captures only exit spans with an active entry span. However, in certain scenarios, you need to trace exit spans that occur without an entry span.
To configure the Python Tracer to trace standalone exit spans, set the INSTANA_ALLOW_ROOT_EXIT_SPAN environment variable to 1 or true as follows:
export INSTANA_ALLOW_ROOT_EXIT_SPAN=1
This feature is useful in the following scenarios:
- Incoming messages from unsupported messaging libraries.
- Requests that are made through unsupported protocols, such as raw Transmission Control Protocol (TCP) or WebSocket.
- Scheduled jobs that are initiated internally by other applications.
- Execution of Python scripts.
Kubernetes
In certain scenarios on this platform, the Python sensor might not be able to automatically locate and contact the Instana host agent. To resolve this issue, see the Configuring Agent Network Access for Kubernetes section in the documentation.
See also:
Frameworks
You can configure the Instana Python collector to monitor and collect data from the following frameworks:
Django (Manual)
When the AUTOWRAPT_BOOTSTRAP=instana environment variable is set, the Django framework must be automatically detected and instrumented. If for some reason, you prefer to or need to manually instrument Django, you can instead add
instana.instrumentation.django.middleware.InstanaMiddleware to your MIDDLEWARE list in settings.py:
import os
import instana
# ... <snip> ...
MIDDLEWARE = [
'instana.instrumentation.django.middleware.InstanaMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Pyramid
Starting from Instana Python 3.0.0, the Pyramid instrumentation is automatic.
Starting from version 1.22.0 to 2.5.3, the Instana Python package includes manual support for Pyramid. To add visibility to your Pyramid-based application, complete the following steps:
- Make sure that the
instanapackage is added to therequirements.txtand installed in the virtual environment or container. - Add
import instanato the beginning of your__init__.pyfile for your Pyramid application. - Add the Instana instrumentation Tween to your configuration.
import instana
with Configurator(settings=settings) as config:
# ...
config.include('instana.instrumentation.pyramid.tweens')
# ...
The following image displays an example of the Instana instrumentation in your Pyramid configuration:

In case you have the pyramid.tweens option set in your production.ini config, make sure that instana.instrumentation.pyramid.tweens.InstanaTweenFactory is the first entry in this list:
pyramid.tweens =
instana.instrumentation.pyramid.tweens.InstanaTweenFactory
# other tweens
WSGI and ASGI stacks
The Instana Python package includes Web Server Gateway Interface (WSGI) and Asynchronous Server Gateway Interface (ASGI) middleware that can be added to any compliant stack. Automation is available for various stacks, but manual addition is also possible for those stacks that don't have automatic support yet from Instana.
After you install the Instana Python package, use the following commands:
import instana
from instana.middleware import InstanaWSGIMiddleware
# or
from instana.middleware import InstanaASGIMiddleware
# Wrap the wsgi app in Instana middleware (InstanaWSGIMiddleware)
wsgiapp = InstanaWSGIMiddleware(MyWSGIApplication())
Instana is working to automate instrumentation for all major frameworks but in the meantime, check some specific quick starts for those stacks that don't have automatic support yet from Instana.
Bottle WSGI
Use the following commands for instrumenting Bottle WSGI:
# Import Instana and the Instana WSGI middleware wrapper
import instana
from instana.middleware import InstanaWSGIMiddleware
from bottle import Bottle, run
app = Bottle()
@app.route('/hello')
def hello():
return "Hello World!"
# Wrap the application with the Instana WSGI Middleware
app = InstanaWSGIMiddleware(app)
# Alternative method for reference
# app = InstanaWSGIMiddleware(bottle.default_app())
run(app, host='localhost', port=8080)
CherryPy WSGI
Use the following commands for instrumenting CherryPy WSGI:
import cherrypy
# Import Instana and the Instana WSGI middleware wrapper
import instana
from instana.middleware import InstanaWSGIMiddleware
# My CherryPy application
class Root(object):
@cherrypy.expose
def index(self):
return "hello world"
cherrypy.config.update({'engine.autoreload.on': False})
cherrypy.server.unsubscribe()
cherrypy.engine.start()
# Wrap the application with the Instana WSGI Middleware
wsgiapp = InstanaWSGIMiddleware(cherrypy.tree.mount(Root()))
In this example, we use uwsgi as the web server and booted with:
uwsgi --socket 127.0.0.1:8080 --enable-threads --protocol=http --wsgi-file mycherry.py --callable wsgiapp -H ~/.local/share/virtualenvs/cherrypyapp-C1BUba0z
Where ~/.local/share/virtualenvs/cherrypyapp-C1BUba0z is the path to my local virtualenv from pipenv
Falcon WSGI
The Falcon framework can also be instrumented through the WSGI wrapper as such:
import falcon
# Import Instana and the Instana WSGI middleware wrapper
import instana
from instana.middleware import InstanaWSGIMiddleware
app = falcon.API()
# ...
# Wrap the application with the Instana WSGI Middleware
app = InstanaWSGIMiddleware(app)
Then, booting your stack with uwsgi --http :9000 --enable-threads --module=myfalcon.app as an example
Gevent-based applications
Instana supports applications based on gevent 1.4 and later.
If you are manually importing the Instana Python package, make sure that the gevent import and monkey patching happen first.
from gevent import monkey
monkey.patch_all()
import instana # <--- after the gevent monkey patching of stdlib
Before Instana Python Tracer 2.5.0, the
gevent-based applications must not use the Activating without code changes method of package activation (that uses the AUTOWRAPT_BOOTSTRAP environment variable). This method doesn't work due to gevent's first-order monkey patching requirements as described earlier. In this case, use the Activating with code changes method.
Starting with Instana Python Tracer 2.5.0, the tracer automatically performs monkey.patch_all() when the AutoTrace webhook or the Activating without code changes method is used. You can fine-tune this monkey patching by setting the INSTANA_GEVENT_MONKEY_OPTIONS environment variable. With this comma-separated list, you can specify the modules to include or exclude from
monkey patching as MONKEY OPTIONS to gevent's gevent.monkey.main function.
The following examples show options that are available for customizing modules for monkey patching:
export INSTANA_GEVENT_MONKEY_OPTIONS='--no-socket, --dns, --no-time, --select, --no-ssl'
export INSTANA_GEVENT_MONKEY_OPTIONS='no-socket, dns, no-time, select, no-ssl'
export INSTANA_GEVENT_MONKEY_OPTIONS='no-socket,dns,no-time,select,no-ssl'
If you use Django together with gevent and Instana autotracing, then ensure that you set the DJANGO_SETTINGS_MODULE environment variable before the autotracing starts. For more information
about the DJANGO_SETTINGS_MODULE environment variable, see the Django documentation.
If this level of customization is still insufficient, then use the Activating with code changes method of package activation.
Tools
You can configure the Instana Python collector to monitor and collect data from different tools.
Web servers
The following configurations can be used to monitor different web servers:
uWSGI web server
Make sure enable-threads is enabled for uwsgi.
Threads
This Python instrumentation creates a lightweight background thread to periodically collect and report process metrics. By default, the GIL and threading are disabled under uWSGI. If you want to instrument your application that runs under
uWSGI, make sure that you enable threads by passing the --enable-threads command (or enable-threads = true in INI style). For more information, see the uWSGI documentation.
uWSGI example: Command line
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi -p 4 --enable-threads
uWSGI example: INI file
[uwsgi]
http = :5000
master = true
processes = 4
enable-threads = true # required
Gunicorn web server
To instrument your application that runs under Gunicorn, make sure that you give --preload as an argument.
Preload
This Python instrumentation creates a lightweight background thread to periodically collect and report process metrics. By default, the application code processes after workers are forked in Gunicorn. If you want to instrument your application
that runs under Gunicorn, make sure that you enable preloading by passing the --preload command. For more information, see the Gunicorn documentation.
Gunicorn example: Command line
To run Gunicorn with preloading, use the following command that is shown in this example:
gunicorn -w 4 --preload "file:app"
Gunicorn example: Config file
To use Gunicorn with a config file, use a Python file with the following variables. Add -c file_name.py to the Gunicorn command.
bind = "0.0.0.0:8000"
workers = 4
preload_app = true # required
End-user monitoring (EUM)
Instana provides deep end-user monitoring that links server-side traces with browser events to give you a complete view from server to browser.
See the end-user monitoring page for more details.
Ignoring endpoints
Starting from Python Tracer 3.3.0, you can use this feature to filter out unnecessary traces or calls to reduce the overall data ingestion. For example, you can exclude the tracing of specific endpoints such as redis.get.
This feature is available for the following supported libraries:
| Library | Instana Python package version |
|---|---|
| Redis | >= 3.3.0 |
| DynamoDB | >= 3.3.0 |
| Kafka | >= 3.5.0 |
Filtering rules
The rules for filtering spans are as follows:
- When a span is ignored, all subsequent downstream spans are also ignored.
- Use the wildcard
*to ignore all endpoints or methods. - Endpoint values (such as Kafka topic names) remain consistent across services.
- Method names might vary depending on the programming language and technology. Refer to the Instana UI to determine the correct method and endpoint for your service.
The following Instana UI screenshot provides a visual reference for identifying the correct method and endpoint for configuration:
The ignore endpoints feature works with only exact method names. Ensure that you use the correct method name to be filtered. This documentation uses the send method from the Kafka-Python package.
Configuring endpoint exclusion
You can enable the endpoint exclusion by using any of the following options:
If more than one approach is used at the same time, Python Tracer prioritizes in the following order: environment variable, in-code configuration, and agent configuration.
Environment variables
You can configure ignored endpoints by using the following environment variables to filter endpoints per service:
INSTANA_IGNORE_ENDPOINTS_PATH: Configure filtering though an external YAML file.INSTANA_IGNORE_ENDPOINTS: Exclude spans based only on method names.
Using INSTANA_IGNORE_ENDPOINTS_PATH
With this environment variable, you can configure filtering though an external YAML file as shown in the following example:
INSTANA_IGNORE_ENDPOINTS_PATH=/absolute/path/to/config.yaml
In the preceding example, the config.yaml file must have an absolute path and adhere to the specified configuration structure.
Filtering by method
To specify the endpoints that you want to exclude, set the INSTANA_IGNORE_ENDPOINTS environment variable as shown in the following example:
tracing:
ignore-endpoints:
redis:
- get
- type
dynamodb:
- query
kafka:
- send
This configuration filters the following spans:
GETandTYPEcommands for RedisQUERYcommand for DynamoDBSENDmethod for Kafka and all downstream spans
Filtering by method and endpoint
To specify the endpoints that you want to exclude, set the INSTANA_IGNORE_ENDPOINTS environment variable as shown in the following example:
tracing:
ignore-endpoints:
kafka:
- methods: ["consume"]
endpoints: ["topic1", "topic2"]
- methods: ["consume", "send"]
endpoints: ["topic3"]
- methods: ["*"] # Applied to all methods
endpoints: ["topic4"]
- methods: ["consume"]
endpoints: ["*"] # Applied to all endpoints
This configuration filters the following spans:
CONSUMEmethod fortopic1andtopic2in Kafka and all downstream spansCONSUMEandSENDmethods fortopic3in Kafka and all downstream spans- All (
*) methods fortopic4in Kafka and all downstream spans CONSUMEmethod for all (*) topics in Kafka and all downstream spans
You can combine both filtering options (method-only and method and endpoint) in the same configuration file.
This configuration is compatible with the agent configuration.yaml structure.
Using INSTANA_IGNORE_ENDPOINTS
With this environment variable, you can exclude spans based on only method names as shown in the following example:
INSTANA_IGNORE_ENDPOINTS=redis:get,type
This configuration filters the following spans:
GETandTYPEcommands for Redis
To filter Redis completely, you can use the following configuration:
INSTANA_IGNORE_ENDPOINTS=redis
To filter multiple services, you can use the following configuration:
INSTANA_IGNORE_ENDPOINTS=redis:get,type;dynamodb:query,scan;kafka:send
This configuration filters the following spans:
GETandTYPEcommands in RedisQUERYandSCANcommands in DynamoDBSENDcommand in Kafka
In-code configuration
To ignore the endpoints by using the in-code configuration approach, pass the following configuration to the src/instana/configurator.py file:
config["tracing"]["ignore_endpoints"] = { "redis": ["get", "type"] }
This configuration excludes the GET and TYPE commands in the Redis package from tracing.
To ignore all endpoints in the Redis package, pass the following configuration to the src/instana/configurator.py file:
config["tracing"]["ignore_endpoints"] = { "redis": [] }
To filter multiple services, you can use the following configuration:
config["tracing"]["ignore_endpoints"] = {
"redis": ["get", "type"],
"dynamodb": ["query", "scan"],
"kafka": ["send"]
}
This configuration filters the following spans:
GETandTYPEcommands in RedisQUERYandSCANcommands in DynamoDBSENDmethod in Kafka and all downstream spans
The following example shows filtering by method and endpoint:
config["tracing"]["ignore_endpoints"] = {
"kafka": [
{ methods: ["consume"], endpoints: ["topic1", "topic2"] },
{ methods: ["send"], endpoints: ["topic3"] }
]
}
This configuration filters the following spans:
CONSUMEmethod fortopic1andtopic2in Kafka and all downstream spansSENDmethod fortopic3in Kafka and all downstream spans
You can use both filtering options (method-only and method and endpoint) in the same configuration.
Agent configuration
To exclude the endpoints by using the agent configuration approach, add the ignore-endpoints configuration to the agent configuration.yaml file. For more information, see the Ignoring endpoints section.
Configuring Kafka Trace correlation headers
With trace correlation, spans maintain relationship between distributed span creations. For example, when trace correlation is enabled, the Trace ID is same for both producer span and consumer span. When the trace correlation is disabled, the producer and consumer spans have different Trace IDs, resulting in no correlation between them.
To disable Kafka Trace correlation entirely, you can use any of the following configuration options:
-
Environment variable: Set the environment variable
INSTANA_KAFKA_TRACE_CORRELATIONtofalse. -
System property: Use the following configuration:
config["tracing"]["kafka"] = { "trace_correlation": False } -
Agent configuration: Configure the Kafka Trace correlation options at the level of the Instana host agent.
For more information, see Kafka Header Migration.