Monitor PHP with APM

Elastic APM for PHP Apps

Integrate Elastic APM into PHP Applications

Manas Peçenek
2 min readFeb 22, 2024
Elastic APM for PHP Apps

In this article, I will show how to monitor PHP applications with Elastic APM. To be able to send data to Elasticsearch you need to have either Elastic Agent or Open Telemetry Collector installed on your environment

1) Using PHP APM Agent to Send Data to Elastic Agent

Download the relevant elastic-apm-agent version from here

# For Debian-Ubuntu
curl -o "apm-agent.deb" -L "https://github.com/elastic/apm-agent-php/releases/download/v1.13.0/apm-agent-php_1.13.0_amd64.deb"

# For CentOS-Fedora
curl -o "apm-agent.rpm" -L "https://github.com/elastic/apm-agent-php/releases/download/v1.13.0/apm-agent-php-1.13.0-1.x86_64.rpm"

# For Alpine
curl -o "apm-agent.apk" -L "https://github.com/elastic/apm-agent-php/releases/download/v1.13.0/apm-agent-php_1.13.0_x86_64.apk"

Install the package

# For Debian-Ubuntu
dpkg -i apm-agent.deb

# For CentOS-Fedora
rpm -ivh apm-agent.rpm

# For Alpine
apk add --allow-untrusted apm-agent.apk

Add this line to php.ini file

elastic_apm.enabled=true

Then either use env variables or php.ini to specify the configs. For configuration details please check here

php.ini

elastic_apm.service_name=my-php-app
elastic_apm.environment=development
elastic_apm.server_url=http://elastic-agent:8200
elastic_apm.secret_token=<super-secret-token>
elastic_apm.capture_errors=true
elastic_apm.server_timeout=30s
elastic_apm.log_level=DEBUG
elastic_apm.log_level_syslog=TRACE
elastic_apm.log_level_stderr=WARNING
elastic_apm.service_version=${CI_COMMIT_SHORT_SHA}
elastic_apm.service_node_name=${HOSTNAME}

.env

ELASTIC_APM_SERVICE_NAME=my-php-app
ELASTIC_APM_ENVIRONMENT=development
ELASTIC_APM_SERVER_URL=http://elastic-agent:8200
ELASTIC_APM_SECRET_TOKEN=<super-secret-token>
ELASTIC_APM_CAPTURE_ERRORS=true
ELASTIC_APM_SERVER_TIMEOUT=30s
ELASTIC_APM_LOG_LEVEL=DEBUG
ELASTIC_APM_LOG_LEVEL_SYSLOG=TRACE
ELASTIC_APM_LOG_LEVEL_STDERR=WARNING
ELASTIC_APM_SERVICE_VERSION=${CI_COMMIT_SHORT_SHA}
ELASTIC_APM_SERVICE_NODE_NAME=${HOSTNAME}

2) Using PHP Extensions to Send Data to Open Telemetry

Install necessary tools and build opentelemetry extention

apt install gcc make autoconf -y
pecl install opentelemetry

Update php.ini file

extension=opentelemetry.so

Add additional related dependencies for the automatic instrumentation. For the whole list of packages please check here. The one below is for symfony integration

composer require \
open-telemetry/sdk \
open-telemetry/exporter-otlp \
open-telemetry/opentelemetry-instrumentation-installer \
open-telemetry/opentelemetry-auto-symfony

composer install

if your otel-collector config file has these lines at the top:

receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318

Then update .env file like below to use http to send request to otel-collector

OTEL_PHP_AUTOLOAD_ENABLED=true
OTEL_RESOURCE_ATTRIBUTES="service.name=my-php-app,deployment.environment=development"
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
OTEL_EXPORTER_OTLP_INSECURE=true
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf

Then update .env file like below to use grpc to send request to otel-collector

OTEL_PHP_AUTOLOAD_ENABLED=true
OTEL_RESOURCE_ATTRIBUTES="service.name=my-php-app,deployment.environment=development"
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4317
OTEL_EXPORTER_OTLP_INSECURE=true
OTEL_EXPORTER_OTLP_PROTOCOL=grpc

RESOURCES:

--

--