PHP

TracesMetricsApp LogsCustom LogsProfiling

This guide walks you through setting up Application Performance Monitoring (APM) on a PHP application. These instructions can also be found on the Installation page in your Middleware Account. View example code here.

The CakePHP web framework is not currently supported by the PHP APM.

Currently, PHP APM is only supported on Debian-based distributions.

Prerequisites

  1. Infra Agent: Infrastructure Agent (Infra Agent). To install the Infra Agent, see our Installation Guide.

  2. PHP: PHP version 8.0 or above. Verify with php -v.

  3. Composer: Ensure you have the Composer dependency manager installed to install our APM.

  4. PECL: PECL is required to install the appropriate extension.

Vanilla PHP

Setup

Before installing any Middleware APM package for PHP, the auto-instrumentation extension must be setup beforehand.

Install the Auto-Instrumentation Extension

Add Extension to php.ini

The php.ini file can be found using the following commands based on your Linux distribution:

Ubuntu/Debian
CentOS/RHEL
Arch Linux
Web-Serve

Verify Installation

Verify the extension is installed and enabled.

Ensure you have followed the steps in the Setup section before continuing

Install the APM Package

To install the Middleware APM package, run the following commands:

Import the Tracker

Add following code to the entry point of your application, and as the first import:

[Optional] Container Variables

Docker

For use with Docker, add the following environment variable to your application.

The DOCKER_BRIDGE_GATEWAY_ADDRESS is the IP address of the gateway between the Docker host and bridge network. This is 172.17.0.1 by default. Learn more about Docker bridge networking here

Kubernetes

For use with Kubernetes, first ensure the Middleware agent is installed in your cluster by using the following command:

Add the following environment variable to your application deployment YAML file:

Implement APM Collector

Start a tracing scope right before your main application code:

Define hooks with class and function names beneath the tracing scope:

End the tracing scope:

Enable Logging

To ingest custom logs, use the following functions based on desired log severity levels.

Wordpress

Get the script

Change your directory to your wordpress installation directory (i.e. /var/www/localhost/htdocs) and run following command:

You can also download the script by clicking here.

Installation

Run the script with administrator or root privileges:

Here, MW_SERVICE_NAME is optional, default service name will be service-<process id>.

Docker

For use with Docker, add the following environment variable to installation script.

The DOCKER_BRIDGE_GATEWAY_ADDRESS is the IP address of the gateway between the Docker host and bridge network. This is 172.17.0.1 by default. Learn more about Docker bridge networking here

Laravel

Laravel version 8.x or higher required

Installation

Change your directory to laravel project's root directory, and run following commands to install the apm:

This script installs necessary components and configures APM easily.

Usage

This script has a install command which Verifies prerequisites, Installs and configures required extensions and packages for instrumentation.

You can also download the script by clicking here.

Tracing

Traces are automatically enabled.

Metrics

Currently, support for metrics is limited

To enable trace-related metrics Add the LaravelApmServiceProvider to the providers array in config/app.php:

and add the MetricsMiddleware class to the $middleware array in app/Http/Kernel.php. If you have enabled tracing, be sure to add this after the tracing class:

Logging

The package automatically integrates with Laravel's logging system to send logs to Middleware. Ensure the correct log driver and settings are configured in your Laravel app, and the package will automatically handle log capture and export.

Run your app

Set following variables in your environment and restart your app to instrument your app:

To correlate frontend traces with backend traces, add following environment variable:

Docker

For use with Docker, add the following environment variable to installation script.

The DOCKER_BRIDGE_GATEWAY_ADDRESS is the IP address of the gateway between the Docker host and bridge network. This is 172.17.0.1 by default. Learn more about Docker bridge networking here

Azure Pipelines

The Azure Pipelines continuous integration and continuous delivery (CI/CD) infrastructure helps you build, deploy, and test your PHP projects. For information on setting up your PHP project with Azure Pipelines and Azure DevOps Services, learn more here.

Step 1: Configure OpenTelemetry Extension

Add the following code to your .ini file located in the /home/html/ini directory of your Azure account:

Step 2: Add Configuration to Azure

Navigate to Application Settings in your Azure PHP project account and select New application setting. Add the following Name and Value to the New application setting:

Your configuration should look like the following:

Configuration settings in Azure

Step 3: Create Your Build Pipelines

Update your build pipeline by adding the following code to the azure-pipelines.yml file:

The below build pipeline includes installation steps for Opentelemetry and the middleware/agent-apm-php

Step 4: Check Your Pipeline

The PHP Agent is now set up to execute a CI/CD approach, following the build pipeline configuration, whenever new code is pushed to the production environment.

Your output should look like the following:

Final output of PHP APM with Azure Pipelines

Troubleshooting

Below we cover a few common errors and their solutions.

pecl: command not found error

If you receive a pecl: command not found error, run the following command:

Broken Dependency Issue

If you receive any broken dependency issues, including errors like libpcre2-dev : Depends: libpcre2-8-0 / libpcre2-16-0 / libpcre2-32-0, run the following command:

ERROR: ‘phpize’ failed error

If you receive an ERROR: ‘phpize’ failed error, run the following commands:

Continuous Profiling

Continuous profiling captures real-time performance insights from your application to enable rapid identification of resource allocation, bottlenecks, and more.

Continuous Profiling for the PHP APM is not yet available; reach out to our support team if you'd like more information. Navigate to the Continuous Profiling section to learn more about using Continuous Profiling with our other APMs.

Need assistance or want to learn more about Middleware? Contact our support team in Slack.