Search…
Python
The topic provides information to instrument your application with Traceable's Python tracing agent.
Traceable's Python tracing agent is an in-app tracing agent. It collects information from your Python application and sends it to Traceable's platform agent. The platform agent sends the information to Traceable's SaaS platform for further analysis. The topic covers deployment, verification, troubleshooting, upgrade, and uninstall information.
High-level Python tracing agent deployment architecture

Before you begin

Make a note of the following points before you proceed with the deployment:
    Make sure that Traceable's platform agent is already installed. For more information, see Platform agent.
    Make a note of the address of the platform agent as it is configured as an endpoint in the deployment process.
    Choose a service name by which you would like your Python application to be identified in the Traceable platform.
    Traceable supports Python 3.6+

Support for request blocking

Traceable provides request blocking support on the following operating system versions:
    Debian 10+
    Ubuntu 18.04+
    Amazon Linux 2+
    CentOS 7+
You can install the Traceable Python agent on other operating systems and continue to collect telemetry data. However, request blocking is not supported on any other operating system.

Support Matrix

Module/Framework
Description
Python Versions Tested/Supported
flask
A micro web framework.
Python 3.6, 3.7, 3.8, 3.9
django
A python web framework.
Python 3.6, 3.7, 3.8, 3.9
grpc
Python GRPC library.
Python 3.6, 3.7, 3.8, 3.9
Python MySQL database client library.
Python 3.6, 3.7, 3.8, 3.9
Python Postgresql database client library.
Python 3.6, 3.7, 3.8, 3.9
requests
Python HTTP client library.
Python 3.6, 3.7, 3.8, 3.9
aiohttp
Python async HTTP client library.
Python 3.6, 3.7, 3.8, 3.9

Deployment process

The Python agent deployment process has three parts:
    1.
    Installation: Install Python tracing agent using pip.
    2.
    Configuration: Configuration can be done using one of the following options:
      YAML
      Environment variable
      Code-based
    3.
    Instrumentation: You can instrument the Python tracing agent using one of the two instrumentation options:
      Auto instrumentation - Auto Instrumentation option requires zero code for installation.
      Manual instrumentation - The manual instrumentation option requires minimal code for installation.
    Traceable recommends using Auto instrumentation for most cases because it keeps the instrumentation logic and your application logic separate. However, if you want more granular control over how the Python agent gets initialized, you can choose the manual installation method.

Installation

You can install Traceable's Python tracing agent using pip. Enter the following command:
1
pip install traceable-agent
Copied!
The pre-compiled blocking module installation is dependent on the supported operating system version.

Configuration

Option 1: YAML

Create a file named config.yaml with a service_name. The python application is identified by the service_name in the Traceable Dashboard. Following is a sample config.yaml file. Change the endpoint configuration to point to your deployed Traceable platform agent.
1
# Observability Configuration
2
service_name: "blocking-python-svc"
3
reporting:
4
endpoint: http://agent.traceableai:4317 # update to point to your deployed traceable-agent
5
# Blocking Configuration
6
opa:
7
endpoint: http://agent.traceableai:8181 # update to point to your deployed traceable-agent
8
blocking_config:
9
enabled: true
10
remote_config:
11
endpoint: agent.traceableai:5441 # update to point to your deployed traceable-agent
Copied!
Initialize an environment variable which points to the location of the configuration file. This environment variable will be automatically detected by the Traceable python agent.
1
export TA_CONFIG_FILE=./config.yaml
Copied!
If you have a django app deployed in gunicorn, set the following environment variable:
1
export DJANGO_SETTINGS_MODULE="<project-name>.settings"
Copied!

Option 2: Environment variables

Following is a list of environment variables that you can configure.
Environment variables take precedence over values set in config.yaml file.
1
export TA_SERVICE_NAME="example-service-name"
2
export TA_REPORTING_ENDPOINT="<http://hostname:4317>"
3
export TA_REPORTING_TRACE_REPORTER_TYPE="OTLP"
4
export TA_BLOCKING_CONFIG_ENABLED=true
5
export TA_BLOCKING_CONFIG_EVALUATE_BODY=true
6
export TA_BLOCKING_CONFIG_MODSECURITY_ENABLED=TRUE
7
export TA_OPA_ENDPOINT=http://agent.traceableai:8181
8
export TA_BLOCKING_CONFIG_REMOTE_CONFIG_ENDPOINT=agent.traceableai:5441
Copied!
If you have a django app deployed in gunicorn, set the following environment variable:
1
export DJANGO_SETTINGS_MODULE="<project-name>.settings"
Copied!

Option 3: Code

Set the following to edit configurations using code.
Code-based configuration takes the highest precedence.
1
from traceableai.agent import Agent
2
from hypertrace.agent.config import config_pb2 as hypertrace_config
3
4
agent = Agent()
5
with agent.edit_config() as config:
6
config.service_name = "example-service-name"
7
config.reporting.endpoint = "http://hostname:4317"
8
config.reporting.trace_reporter_type = hypertrace_config.TraceReporterType.OTLP
Copied!
See manual instrumentation for further details.

Auto Instrumentation

To auto instrument your application with Traceable's tracing agent, prefix your startup command with traceableai-instrument. For example:
Python
flask CLI
gunicorn
For Python, enter the following command:
1
traceableai-instrument python app.py
Copied!
If you are using flask CLI, enter the following command:
1
traceableai-instrument flask run
Copied!
If you are using a config.yaml, you can set the config file as follows:
1
TA_CONFIG_FILE=./config.yaml traceableai-instrument flask run
Copied!
If you are using gunicorn, you can prepend your gunicorn startup command. For example:
1
traceableai-instrument gunicorn --workers=2 test:app
Copied!

Manual Instrumentation

To use manual instrumentation, you have to make some modifications to your application's entry file. For example:
1
from traceableai.agent import Agent
2
3
...
4
5
agent = Agent() # initialize the agent
6
# .instrument() will instrument all available libraries
7
agent.instrument()
8
9
# if you are using flask, you must pass your app instance to the instrument call
10
agent.instrument(your_flask_app)
11
agent.add_traceable_filter() # Adds the traceableai blocking filter
12
...
13
Copied!

Startup

Start your application by entering the following command:
1
python app.py
Copied!
Or, if you are using the flask CLI, then enter the following command:
1
flask run
Copied!

Troubleshooting

If you do not see any data in Traceable's platform after a few minutes:
    1.
    Make sure that there is load on the instrumented Python application.
    2.
    Check whether Traceable's platform agent has the correct access token configured. You can copy the access token by logging into your Traceable platform and navigating to Administration (
    ) > Account > Access token.
    3.
    Check the Traceable platform agent logs for any connectivity issues or errors. You can view the logs by navigating to /var/traceable/log/.
    4.
    Check the Python tracing agent logs for any connectivity issues or errors. You will have to change the log level to DEBUG by setting, ENV TA_LOG_LEVEL DEBUG, and restarting the application. For example, in the case of a K8s application, you can configure this in the dockerfile.
If you need further assistance, reach out to Traceable support.

Upgrade

To upgrade to the latest version of Traceable's tracing agent, enter the following command. Make sure that the current configuration files and environment variables are not changed before you run the upgrade command:
1
pip install traceable-agent --upgrade
Copied!

Uninstall

To uninstall the tracing agent, enter the following command:
1
pip uninstall traceable-agent
Copied!
Last modified 3d ago