Troubleshoot no data after connecting the Python agent
If the console does not display monitoring data for your application after you connect the Python agent to Application Monitoring of Application Real-Time Monitoring Service (ARMS), the issue can be in agent installation, initialization, or telemetry data reporting. This topic describes how to troubleshoot four scenario types: basic configuration, network connectivity, Kubernetes automatic instrumentation, and manual instrumentation. It provides check commands that you can run directly and explains how to interpret the results, so that you can identify the cause and restore data reporting.
Symptoms
After you connect the Python agent, the console does not display monitoring data for the application. The application itself may start normally or fail to start.
Causes
Connecting the Python agent involves three steps:
Pull the agent package from the remote repository and install it in the path that the application uses. In Kubernetes scenarios, an init container pulls the package; in manual instrumentation scenarios, the
aliyun-bootstrapcommand downloads and installs it.After the application starts, it uses
PYTHONPATH(automatic instrumentation) or thealiyun-instrumentprefix to find the initialization function in the agent package and run the agent initialization. During initialization, the agent uses Monkey Patch to replace the key library functions of the application and implement instrumentation.After the application starts successfully, calls to specific functions enter the instrumentation wrapper before and after execution. The wrapper generates telemetry data and reports it asynchronously to the server.
An issue in any of these steps can result in no data after connection. The following table maps common causes to where you start troubleshooting:
Possible cause | Where to start |
The ARMS application collection start/stop setting or the agent master switch for the application is disabled | |
The application uses a free trial and the trial traffic quota is exhausted | |
| |
The network of the reporting data link is unavailable | |
The ack-onepilot component cannot access the server, the agent environment variables are not injected correctly, the init container fails to run, or the Pod fails to start | |
The agent package is not installed in the Python dependency path of the application, or the environment variables are not configured as expected | |
The Python version or a related framework is not supported | The agent log check steps in both connection scenarios |
Prerequisites
Before you start troubleshooting, confirm where the application is deployed and prepare the corresponding tools and credentials:
Deployed in a Kubernetes cluster (Alibaba Cloud ACK or a self-managed cluster): make sure that kubectl is available in your environment and prepare the kubeconfig file of the cluster.
Deployed on ECS: prepare the ECS instance ID, which is used to send and run check commands on the ECS instance.
Other deployment methods: confirm in advance how to run check commands in the application runtime environment.
Run the following command to obtain the kubeconfig file of an Alibaba Cloud cluster:
aliyun cs DescribeClusterUserKubeconfig \
--ClusterId <Cluster ID> |
python3 -c 'import json,sys; print(json.load(sys.stdin)["config"], end="")' \
> kubeconfig.yaml
export KUBECONFIG="$PWD/kubeconfig.yaml"
kubectl cluster-info
kubectl get nodesSolution
Troubleshoot in the following order: first complete the basic checks and network connectivity checks that apply to all connection methods, then determine the connection method and continue with the in-depth checks for that scenario. Determine the connection method as follows:
In Kubernetes scenarios, the ack-onepilot component automatically pulls the agent package through an init container and injects
PYTHONPATHinto the Pod. Go to Checks for Kubernetes automatic instrumentation.If you manually run the
aliyun-bootstrapcommand to download and install the agent package and start the application with thealiyun-instrumentprefix, go to Checks for manual instrumentation.If you are unsure about the connection method, check whether the
PYTHONPATHenvironment variable is injected into the target Pod. If it is injected, follow the automatic instrumentation scenario. If it is not injected, manual instrumentation was forced, so follow the manual instrumentation scenario.
After you resolve the issue identified by these checks, verify in the console that monitoring data is displayed again.
Basic checks
Check each of the following settings:
Whether the ARMS application collection start/stop setting is disabled.
Whether the agent master switch for the application is disabled.
Whether the application uses a free trial and whether the trial traffic quota is exhausted.
Whether the application sets the
OTEL_SDK_DISABLEDenvironment variable totrue.
Run the following commands to assist with these checks. The comments in the commands describe the purpose of each check:
# Check the ARMS APM service status
aliyun arms CheckCommercialStatus \
--RegionId <Region ID> \
--Service apm
# Query an ARMS application by application name
aliyun arms SearchTraceAppByName \
--RegionId <Region ID> \
--TraceAppName <Application name>
# Query the agent master switch; omit --Enable to query only
aliyun arms ConfigApp \
--RegionId <Region ID> \
--AppIds <Application PID> \
--Type TRACE
# Kubernetes scenario: check the switch in the Pod configuration
kubectl get pod -n <Namespace> <Pod name> -o yaml
# Kubernetes scenario: check the environment variables that the process actually inherits
kubectl exec -n <Namespace> <Pod name> -c <Container name> -- \
sh -c 'tr "\0" "\n" </proc/1/environ'
# ECS or host scenario: check the environment variables of the Python process
PID=<Python process ID>
tr '\0' '\n' <"/proc/$PID/environ"Network connectivity checks
See Application Monitoring network settings to check network connectivity to the server in the environment where the application runs. If the reporting data link is unavailable, the console cannot display monitoring data.
ARMS environment variable checks
Both automatic and manual instrumentation scenarios require you to confirm the ARMS environment variables: ARMS_APP_NAME, ARMS_LICENSE_KEY, ARMS_REGION_ID, and ARMS_WORKSPACE (optional) are set and contain the expected values. If the values differ from the configuration that you previously confirmed, these environment variables were forcibly overridden. Remove these environment variable settings.
In Kubernetes scenarios, first check whether the relevant environment variables are injected into the Pod Spec and check their valueFrom sources, and then confirm the environment variables that the process actually inherits. In ECS or host scenarios, check the environment variables of the Python process directly:
# View the related environment variables in the Pod Spec and their valueFrom sources
kubectl get pod -n <Namespace> <Pod name> \
-o jsonpath='{range .spec.containers[*]}{"container="}{.name}{"\n"}{range .env[*]}{.name}{"="}{.value}{" secret="}{.valueFrom.secretKeyRef.name}{"/"}{.valueFrom.secretKeyRef.key}{" field="}{.valueFrom.fieldRef.fieldPath}{"\n"}{end}{end}'
# View the environment variables that the process actually inherits
kubectl exec -n <Namespace> <Pod name> -c <Container name> -- \
sh -c 'tr "\0" "\n" </proc/1/environ'# ECS or host
PID=<Python process ID>
tr '\0' '\n' <"/proc/$PID/environ"
readlink -f "/proc/$PID/exe"
tr '\0' ' ' <"/proc/$PID/cmdline"; echoChecks for Kubernetes automatic instrumentation
Follow Prerequisites to connect to the target cluster, and then perform the following checks in order:
Check the version of ack-onepilot in the cluster.
# Find ack-onepilot-related resources kubectl get deployment,daemonset,pod -A -o wide # View the images and versions used by ack-onepilot workloads kubectl get deployment,daemonset -A \ -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,IMAGES:.spec.template.spec.containers[*].image'Continue based on the check result:
ack-onepilot is not installed in the cluster: install the component first. For the installation method, go to References and open "Install the ARMS Python agent in ACK and ACS clusters".
The ack-onepilot version is earlier than 5.0.0: go to Checks for manual instrumentation.
The ack-onepilot version is 5.0.0 or later: continue with the following steps.
Use the
kubectl logscommand to view the ack-onepilot logs. Usekubectl get pod -Ato view information such as the ack-onepilot namespace, and focus on the logs related togetArmsLicenseKeyandGetServiceObservabilityInfoByWorkspace.# View the component Pod and container names kubectl get pod -A -o wide kubectl get pod -n <ack-onepilot namespace> <ack-onepilot-Pod> \ -o jsonpath='{range .spec.containers[*]}{.name}{"\n"}{end}' # View recent logs; search and narrow the scope based on the actual log content kubectl logs -n <ack-onepilot namespace> <ack-onepilot-Pod> \ --all-containers=true --since=2h --timestamps # If the component restarted, view the previous container logs kubectl logs -n <ack-onepilot namespace> <ack-onepilot-Pod> \ -c <Container name> --previous --tail=500 kubectl describe pod -n <ack-onepilot namespace> <ack-onepilot-Pod> kubectl get events -n <ack-onepilot namespace> \ --sort-by='.metadata.creationTimestamp'If the relevant logs contain errors, the component cannot access the server. Record the specific error and analyze the possible causes, such as insufficient permissions for the corresponding AccessKey pair or unavailable network connectivity.
Locate the specific Pod that you want to instrument and run kubectl commands to view the YAML of the Pod. Check whether the
PYTHONPATHenvironment variable is injected. If it is not injected, manual instrumentation was forced. Go to Checks for manual instrumentation.kubectl get pod -n <Namespace> <Pod name> -o yaml kubectl get pod -n <Namespace> <Pod name> \ -o jsonpath='{range .spec.containers[*]}{"container="}{.name}{"\n"}{range .env[?(@.name=="PYTHONPATH")]}{.name}{"="}{.value}{"\n"}{end}{end}'If
PYTHONPATHis injected, confirm that the Pod also hasARMS_APP_NAME,ARMS_LICENSE_KEY,ARMS_REGION_ID, andARMS_WORKSPACE(optional) injected with the expected values. For the check commands and how to interpret the values, see ARMS environment variable checks.Use the
kubectl logscommand to view the logs of theone-pilot-initcontainerinit container of the application Pod. If the init container reports a message such aspip3 not found, the runtime image does not contain pip3 and cannot install Python packages. Install pip3 in the runtime image, or use manual instrumentation instead.# List the init container names, images, and execution states kubectl get pod -n <Namespace> <Pod name> \ -o jsonpath='{range .spec.initContainers[*]}name={.name}{" image="}{.image}{"\n"}{end}' kubectl get pod -n <Namespace> <Pod name> \ -o jsonpath='{range .status.initContainerStatuses[*]}name={.name}{" ready="}{.ready}{" restarts="}{.restartCount}{" state="}{.state}{" lastState="}{.lastState}{"\n"}{end}' # View the current and previous init container logs kubectl logs -n <Namespace> <Pod name> \ -c <Agent init-container name> --timestamps kubectl logs -n <Namespace> <Pod name> \ -c <Agent init-container name> --previous --tail=500Check whether the application Pod starts successfully:
The Pod starts successfully but no data is reported after connection: Locate the Python agent logs in the Pod. In automatic instrumentation scenarios, the agent logs are generally stored in the
/home/admin/.opt/.apsara-apm/python/logsdirectory. Reviewaliyun-python-agent-*.loganddiagnose-*.logto identify potential issues. Use the logs together with Python components and frameworks supported by ARMS Application Monitoring to determine whether the Python version or a related framework is supported.# View the agent log directory and recent logs kubectl exec -n <Namespace> <Pod name> -c <Container name> -- \ sh -c ' LOG_DIR=/home/admin/.opt/.apsara-apm/python/logs ls -lah "$LOG_DIR" for file in "$LOG_DIR"/*.log; do [ -f "$file" ] && { echo "== $file ==" tail -n 200 "$file" } done ' # Check the Python interpreter, sys.path, and dependencies that the application actually uses kubectl exec -n <Namespace> <Pod name> -c <Container name> -- \ sh -c ' command -v python || command -v python3 python -V 2>&1 || python3 -V python -m site 2>&1 || python3 -m site python -m pip freeze 2>&1 || python3 -m pip freeze 'The Pod fails to start: Use the
kubectl logs -pcommand to view the logs from the previous container exit and analyze the cause of the startup failure.kubectl logs -n <Namespace> <Pod name> \ -c <Container name> --tail=500 --timestamps kubectl logs -n <Namespace> <Pod name> \ -c <Container name> --previous --tail=500 --timestamps kubectl get pod -n <Namespace> <Pod name> \ -o jsonpath='{range .status.containerStatuses[*]}name={.name}{" ready="}{.ready}{" restarts="}{.restartCount}{" state="}{.state}{" lastState="}{.lastState}{"\n"}{end}' kubectl describe pod -n <Namespace> <Pod name> kubectl get events -n <Namespace> \ --field-selector involvedObject.name=<Pod name> \ --sort-by='.metadata.creationTimestamp'
Raise the priority of installed dependencies (optional)
Connecting the agent automatically through Kubernetes can change the versions of some dependencies, such as Protobuf. To avoid unexpected errors caused by changed dependency versions, if you already installed a dependency such as Protobuf elsewhere, run the following commands to give the installed dependency a higher priority:
# View the package installation path
python3 -m site
# Check dependency conflicts and the actual loaded location
python3 -m pip check
python3 -c '
import google.protobuf
print(google.protobuf.__version__)
print(google.protobuf.__file__)
'
# Set the PYTHONPATH environment variable to prefer the dependency version installed by the user
# Separate different paths with a colon
export PYTHONPATH=/app:/usr/local/lib/python3.9:/usr/local/lib/python3.9/site-packagesexport PYTHONPATH contains paths that are related to the Python version. Replace them with the actual paths in the output of python3 -m site.
Checks for manual instrumentation
Follow Prerequisites to confirm where the application is deployed and to prepare the corresponding credentials. For an application deployed on ECS, use the following commands to send and run check commands on the Alibaba Cloud ECS instance:
COMMAND='
ps -ef
command -v python || command -v python3
command -v aliyun-bootstrap || true
command -v aliyun-instrument || true
python -V 2>&1 || python3 -V 2>&1
python -m site 2>&1 || python3 -m site 2>&1
'
aliyun ecs RunCommand \
--RegionId <Region ID> \
--InstanceId.1 <ECS instance ID> \
--Type RunShellScript \
--ContentEncoding Base64 \
--Timeout 60 \
--CommandContent "$(printf %s "$COMMAND" | base64 | tr -d '\n')"
# Use the InvokeId returned by RunCommand to query the execution result
aliyun ecs DescribeInvocationResults \
--RegionId <Region ID> \
--InvokeId <InvokeId>For an application deployed in an Alibaba Cloud ACK Pod, obtain the kubeconfig file and then run the subsequent check commands with kubectl. Check in the following order:
Confirm that the
ARMS_APP_NAME,ARMS_LICENSE_KEY,ARMS_REGION_ID, andARMS_WORKSPACE(optional) environment variables of the corresponding Python process are set and contain the expected values. For the check commands and how to interpret the values, see ARMS environment variable checks.Check whether the
aliyun-bootstrap -a installcommand ran successfully and whether the related packages are installed in thesys.pathof the application.ImportantAlways run
aliyun-bootstrap -a installin the Python dependency path of the application. Otherwise, the application cannot find the package at runtime. For example, if the application runs in a Python venv, the agent must also be installed in that Python venv.# Confirm the current interpreter and installation location command -v python python -V python -m pip --version python -m site command -v aliyun-bootstrap command -v aliyun-instrument # View the related Python packages in the current environment python -m pip freeze # Virtual environment scenario source /path/to/venv/bin/activate which python python -m pip --version # Run the installation only after confirming that the environment is correct aliyun-bootstrap -a install # Check the actual application startup command PID=<Python process ID> tr '\0' ' ' <"/proc/$PID/cmdline"; echoIf the installation succeeds and the application also starts successfully, see Python agent FAQ to find the Python agent logs. Use the logs together with Python components and frameworks supported by ARMS Application Monitoring to troubleshoot the issue and determine whether the Python version or a related framework is supported, or whether the logs indicate another cause.
# Common log directory LOG_DIR=/home/admin/.opt/.apsara-apm/python/logs ls -lah "$LOG_DIR" # Read the logs first, then search based on the actual context for file in "$LOG_DIR"/*.log; do [ -f "$file" ] && { echo "== $file ==" tail -n 200 "$file" } done # If the log directory is unknown, search by file name find /home /opt /tmp -type f \ \( -name 'aliyun-python-agent-*.log' -o -name 'diagnose-*.log' \) \ 2>/dev/null
diagnose-*.log contains detailed process information:
The process startup command, including the server and its parameters.
The pip dependencies of the process, obtained through
importlib.metadata.packages_distributions().The installation location used by aliyun-bootstrap.
The version of the Python interpreter.
Network connectivity of the reporting data link.
Whether the plugins were instrumented successfully.
Whether the scenario uses gevent.
The app name, app id, and workspace of the agent.
Lists of the plugins that were instrumented successfully and unsuccessfully.
References
If the preceding troubleshooting process does not resolve the issue, see Python agent FAQ. You can also refer to the following documents during connection and troubleshooting: