Python APM Setup
Traces | Metrics | App Logs | Custom Logs | Profiling |
---|---|---|---|---|
Prerequisites
- To monitor APM data on dashboard, Middleware Host agent needs to be installed.
- You can refer this demo project to refer use cases of APM.
Getting Started
Step 1: Install MW’s APM Package
Run the following command in your terminal:
Step 2: Auto-install Required Packages
The middleware-bootstrap -a install
command reads through the list of packages installed in your active site-packages folder, and installs the corresponding instrumentation libraries for these packages, if applicable.
Step 3: Import the Tracker
Add the following lines at the very start of your project:
Enable MW’s APM functionalities
1. Enable Distributed Tracing
Distributed-tracing is a default functionality of MW’s APM. This will be auto-enabled when you execute the command mentioned in Run Your Application section.
2. Enable Metrics
You can enable metrics by writing the following line just after the tracker initialization.
3. Enable Logs
Similar to metrics, you can enable logs by writing the following line.
4. Enable Profiling
For the Profiling feature, you need to write the following line.
Your Code will look like this
After adding all the above steps, your code will look like this:
Run Your Application
To run your application, you need to execute the following command:
For APM inside Kubernetes
If you are using APM in a Kubernetes cluster make sure to follow these two steps:
Step 1: Find your Middleware Service namespace
For older setup, your “mw-service” can be inside mw-agent-ns-{FIRST-5-LETTERS-OF-API-KEY}
namespace
For newer setup, we simplified the namespace name to mw-agent-ns
Step 2: Set this ENV variable in your application deployment YAML
Troubleshoot:
If you receive a warning similar to the following:
WARNING: The scripts middleware-bootstrap and middleware-instrument are installed in ‘/home/…/.local/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use —no-warn-script-location
You can add the binary to your path In linux, you can add this with
Error Handling :
If you want to record exception in traces then, you can use tracker.record_error(e)
method.