PHP 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.
- PHP requires at least PHP 8+ and a PHP-Extension to run this agent.
Guide
Initial Setup:
Before installing this agent, you need to install PHP-Extension(named otel_instrumentation) to run this agent. You can follow below steps to install & enable it:
- Run follow cmd to install the extension:
- Then, Mention the extension as
extension=opentelemetry.so
to yourphp.ini
file (depending on which Linux distribution you’re using):Ubuntu/Debian:
CentOS/RHEL:
Arch Linux:
Specific to the Web-Serve:
- And verify that the extension is installed and enabled using:
Troubleshoot
While installing PHP-Extension:
- If you are facing
pecl:command not found
, then you need to run follow cmd: - If you are facing any kind of broken dependencies issues like:
libpcre2-dev : Depends: libpcre2-8-0 / libpcre2-16-0 / libpcre2-32-0
, then you need to run follow cmd: - If you are facing
ERROR: 'phpize' failed
, then you need to run follow cmd:
Step 1: Install APM-PHP package
Make sure you have a PHP project set up and a composer.json
file in your project’s root directory. If you don’t have one, you can create it using composer init
. Run below command in your terminal to install Middleware’s APM-PHP package.
Step 2: Prepend APM script
Add these lines given below at the very start of your project.
Step 3: Use APM Collector & Start the Tracing-scope
By using the APM Collector, you will start tracing-scope before your code, Also you need to register your hooks along with initial declaration. In each hook, you need to define your Classes & Functions name, so whenever they run, agent will track them auto.
Step 4 : End the Tracing-scope
After your code-flow, you need to end the tracing scope, so that agent can send the data to Middleware’s APM dashboard.
Step 5 : To enable Logging feature
If you want to enable Logging feature along with tracing in your project, then you can use below code snippet.
Final code snippet will be
Sample Code:
Note for APM inside Kubernetes
If you are using APM in a Kubernetes cluster make sure to follow these 2 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