Instrument your Python application for Splunk Observability Cloud π
The Python agent from the Splunk Distribution of OpenTelemetry Python can automatically instrument your Python application by dynamically patching supported libraries.
To get started, use the guided setup or follow the instructions manually.
Generate customized instructions using the guided setup π
To generate all the basic installation commands for your environment and application, use the Python guided setup. To access the Python guided setup, follow these steps:
Log in to Splunk Observability Cloud.
Open the Python guided setup . Optionally, you can navigate to the guided setup on your own:
In the navigation menu, select
.Go to the Available integrations tab, or select Add Integration in the Deployed integrations tab.
In the integration filter menu, select By Product.
Select the APM product.
Select the Python tile to open the Python guided setup.
Install the Splunk Distribution of OpenTelemetry Python manually π
If you donβt use the guided setup, follow these instructions to manually install the Splunk Distribution of OpenTelemetry Python:
Install and activate the Python agent π
Follow these steps to automatically instrument your application using the Python agent:
Check that you meet the requirements. See Python agent compatibility and requirements.
Install the
splunk-opentelemetry[all]
package:pip install "splunk-opentelemetry[all]"
If youβre using a requirements.txt or pyproject.toml file, add
splunk-opentelemetry[all]
to it.Run the bootstrap script to install instrumentation for every supported package in your environment:
splunk-py-trace-bootstrap
To print the instrumentation packages to the console instead of installing them, run
splunk-py-trace-bootstrap --action=requirements
. You can then add the output to your requirements or Pipfile.Set the
OTEL_SERVICE_NAME
environment variable:export OTEL_SERVICE_NAME=<yourServiceName>
$env:OTEL_SERVICE_NAME=<yourServiceName>
(Optional) Set the endpoint URL if the Splunk Distribution of OpenTelemetry Collector is running on a different host:
export OTEL_EXPORTER_OTLP_ENDPOINT=<yourCollectorEndpoint>:<yourCollectorPort>
$env:OTEL_EXPORTER_OTLP_ENDPOINT=<yourCollectorEndpoint>:<yourCollectorPort>
(Optional) Set the deployment environment and service version:
export OTEL_RESOURCE_ATTRIBUTES='deployment.environment=<envtype>,service.version=<version>'
$env:OTEL_RESOURCE_ATTRIBUTES='deployment.environment=<envtype>,service.version=<version>'
Activate the Splunk OTel Python agent by editing your Python service command.
For example, if you open your Python application as follows:
python3 main.py --port=8000
prefix the command with
splunk-py-trace
:splunk-py-trace python3 main.py --port=8000
Note
To instrument uWSGI applications, see Manually instrument Python applications for Splunk Observability Cloud.
(Optional) Perform these additional steps if youβre using the Django framework:
Application metrics are collected by default. See Metrics and attributes collected by the Splunk Distribution of OpenTelemetry Python for more information.
If no data appears in APM, see Troubleshoot Python instrumentation for Splunk Observability Cloud.
Activate AlwaysOn Profiling π
Note
AlwaysOn Profiling for Python is in beta development. This feature is provided by Splunk to you βas isβ without any warranties, maintenance and support, or service-level commitments. Use of this feature is subject to the Splunk General Terms .
To activate AlwaysOn Profiling, set the SPLUNK_PROFILER_ENABLED
environment variable to true
or call the start_profiling
function in your application code.
The following example shows how to activate the profiler from your application code:
from splunk_otel.profiling import start_profiling
# Activates CPU profiling
# All arguments are optional
start_profiling(
service_name='my-python-service',
resource_attributes={
'service.version': '3.1'
'deployment.environment': 'production',
}
endpoint='http://localhost:4317'
)
See Get data into Splunk APM AlwaysOn Profiling for more information. For additional settings, see Python settings for AlwaysOn Profiling.
Configure the Python agent π
In most cases, the only configuration setting you need to enter is the service name. You can also define other basic settings, like the deployment environment, the service version, and the endpoint, among others.
For advanced configuration of the Python agent, like changing trace propagation formats, correlating traces and logs, or configuring server trace data, see Configure the Python agent for Splunk Observability Cloud.
Deploy the Python agent in Kubernetes π
To deploy the Python agent in Kubernetes, configure the Kubernetes Downward API to expose environment variables to Kubernetes resources.
The following example shows how to update a deployment to expose environment variables by adding the agent configuration under the .spec.template.spec.containers.env
section:
apiVersion: apps/v1
kind: Deployment
spec:
selector:
matchLabels:
app: your-application
template:
spec:
containers:
- name: myapp
env:
- name: SPLUNK_OTEL_AGENT
valueFrom:
fieldRef:
fieldPath: status.hostIP
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: "http://$(SPLUNK_OTEL_AGENT):4317"
- name: OTEL_SERVICE_NAME
value: "<serviceName>"
- name: OTEL_RESOURCE_ATTRIBUTES
value: "deployment.environment=<environmentName>"
Send data directly to Splunk Observability Cloud π
By default, the agent sends all telemetry to the local instance of the Splunk Distribution of OpenTelemetry Collector.
To send data directly to Splunk Observability Cloud, set the following environment variables:
export SPLUNK_ACCESS_TOKEN=<access_token>
export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://ingest.<realm>.signalfx.com/v2/trace/otlp
$env:SPLUNK_ACCESS_TOKEN=<access_token>
$env:OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/protobuf
$env:OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://ingest.<realm>.signalfx.com/v2/trace/otlp
To obtain an access token, see Retrieve and manage user API access tokens using Splunk Observability Cloud.
To find your Splunk realm, see Note about realms.
Note
For more information on the ingest API endpoints, see Send APM traces .
Specify the source host π
To override the host used by the agent, use the environment variable OTEL_RESOURCE_ATTRIBUTES
to set your hostβs name to the desired source:
$env:OTEL_RESOURCE_ATTRIBUTES=host.name=<host_name>
export OTEL_RESOURCE_ATTRIBUTES=host.name=<host_name>
Instrument Lambda functions π
You can instrument AWS Lambda functions using the Splunk OpenTelemetry Lambda Layer. See Instrument your AWS Lambda function for Splunk Observability Cloud for more information.