Aging software applications often include many years of nuanced business logic. And the smallest of changes can easily cause unintended consequences.

Because of this, one thing you can do when working with these types of applications is to set up the all-in-one monitoring tool Datadog

If your goal is observability, Datadog offers a wide selection of tools to help monitor every aspect of your software applications and the hardware it runs on. 

In this context, its most useful aspects are log ingestion, Application Performance Monitoring (APM), and automated alerting through Synthetic Monitors.

Let’s go through the steps for your Datadog AWS config on a standard EC2 server, running a legacy PHP application.

Step 1: How To Install Datadog Agent on EC2 Instance

The first step is to install the Datadog Agent on your Linux server. The Agent is a lightweight program that runs on your server and sends data to your Datadog account.

To install the Agent, log-in to your server and run the command below.

And replace <YOUR_API_KEY> with your actual Datadog API key. Which you can find in the "Integrations" section of your DataDog account settings.

 

DD_API_KEY=<YOUR_API_KEY> bash -c "$(curl -L <https://s3.amazonaws.com/dd-agent/scripts/install_script.sh>)"

 

The installation process may take a few minutes. Once it's finished, the Agent should now be running in the background and sending data to your Datadog account.

Step 2: Configure the Datadog Agent

Now that your PHP application is logging to a file, you’ll need to configure the Datadog Agent to read this file and send the data to your Datadog account.

Open the Datadog Agent configuration file (usually /etc/dd-agent/datadog.conf) and add the following lines:

 

logs_enabled: true

logs_config:
  - type: file
    path: /var/log/php.log
    source: my_php_app
    service: my_php_app

 

This tells the Datadog Agent to monitor the /var/log/php.log file and send any new messages to your DataDog account with the source and service tags "my_php_app." 

Note: You can customize these tags to suit your needs.

Step 3: Configure Datadog PHP Logging

Next, you’ll need to configure your PHP application to log messages to a file that the DataDog Agent can access.

Open your PHP configuration file (usually php.ini) and add the following lines:

 

error_log = /var/log/php.log
log_errors = On

 

This tells PHP to log any errors or messages to the /var/log/php.log file.

Next, you’ll need to add some code to your application to log messages to this file. 

You can use the error_log function to do this by dropping a line of code, like the example below, inside any functions in your application.

 

error_log("This is a test message");

 

This will write the message "This is a test message" to the /var/log/php.log file. You can use this function to log any messages that you want to track in Datadog, such as errors, warnings, or custom events.

Step 4: Configure Datadog APM

In addition to log ingestion, DataDog also offers Application Performance Monitoring (APM) as a way to track the performance of your application. 

Datadog APM setup is a simple process. But the required steps vary widely based on the framework and configuration of your PHP application. 

We’ll walk through setting up the APM for an application that only uses the Zend framework. But you can find Datadog’s full documentation on setting up APM.

To get started with Datadog APM, install the Datadog PHP Tracer library by running the following command:

 

error_log("This is a test message");

 

Next, you'll need to include the Tracer library in your PHP code and configure it with your Datadog API key. You can do this by adding the following lines to your PHP application.

And remember to replace <YOUR_API_KEY>, <YOUR_APP_NAME>, and <YOUR_ENVIRONMENT> with your actual Datadog API key, application name, and environment.

 

use DDTrace\\Integrations\\PDO\\PDOIntegration;
use DDTrace\\Integrations\\PDO\\PDOStatementIntegration;

DDTrace\\Integrations\\PDO\\register();
DDTrace\\Integrations\\PDOStatement\\register();

$tracer = \\DDTrace\\GlobalTracer::get();
$tracer->init([
    'api_key' => '<YOUR_API_KEY>',
    'app_name' => '<YOUR_APP_NAME>',
    'env' => '<YOUR_ENVIRONMENT>',
]);

 

With these changes in place, Datadog APM will automatically track the performance of your PHP application, including request latency, database queries, and external service calls.

 You can use this data to identify and troubleshoot performance issues. And optimize the performance of your application overall.

Step 5: Set up Basic Alerts

With logging and APM in place, the final step is to set up basic synthetic monitors to track the availability and performance of your application. 

(Synthetic monitors are automated tests that simulate user actions and track the response of your application.)

To set up basic synthetic monitors,log in to your Datadog account and navigate to the "Synthetics" page. Then, create a new synthetic monitor by clicking the "Create Monitor" button.

Next, choose the type of synthetic monitor you want to create. Datadog offers several options, including HTTP monitors, browser monitors, and API monitors. 

For a basic setup, we recommend using an HTTP monitor.

To create an HTTP monitor, provide a URL to test. And specify the monitoring frequency and location. You can also add advanced options, such as custom headers and HTTP authentication.

Once you've set up your synthetic monitor, Datadog will automatically run the test at the specified frequency and alert you if it encounters any issues. This can help you proactively identify and fix issues with your application before they affect your users.

The number of synthetic monitors you need will vary based on the size and scope of your application. At NextLink Labs, we typically aim for at least 5-10 that cover the following areas:

  • Basic uptime check that ensures the application homepage is available
  • Checks for any critical UI flows or background jobs (e.g., checkout forms can be filled out, nightly sync is running, daily payment processor is running, etc.)
  • Check that application response time is within certain thresholds
  • Check that application error rate is within certain thresholds 

For a full discussion about the process of designing application monitors, checkout our blog.

Dealing with Technical Debt: Next Steps

Setting up Datadog for a PHP application on a Linux server is a simple and effective way to ensure the stability and performance of your application as you begin your technical debt and application modernization process.

If you’re looking for help tackling technical debt in your applications or are looking for help implementing and training your team to use Datadog, check out NextLink Lab’s DevOps Consulting Services.