Monitoring Your Backend with New Relic: A Step-by-Step Integration Guide

Overview

In this blog post, I'll walk you through integrating New Relic into your backend application for monitoring purposes. This process involves three key tasks:

  1. Modifying the Dockerfile to install the New Relic PHP agent.

  2. Updating the Jenkins job to build and deploy the application with the New Relic agent.

  3. Configuring your Kubernetes deployment to run the New Relic daemon.

By the end of this guide, you'll have your backend app monitored in real time using New Relic, with insights into response times, throughput, and errors. Let’s get started!

Steps to Implement New Relic

1. Setting Up New Relic

First, log in to your New Relic account. If you don’t have one, sign up and follow the setup process.

  • Navigate to Integrations & Agents in your New Relic dashboard.
  • Select your language.

  • Choose Docker as our integration method since our backend is Dockerized.

2. Create New Relic License Key

  • Generate a new license key. You’ll need this key for the following steps.

  • Save this key, as you'll need it for both the Dockerfile and Jenkins configuration.

3. Modifying the Dockerfile

Now, we modify the Dockerfile to install the New Relic PHP agent and set up the environment variables.

Give Name to your Application which same as in the dockerfile

Here’s how to update your Dockerfile:

# New Relic agent installation
ARG NEW_RELIC_AGENT_VERSION
ARG NEW_RELIC_LICENSE_KEY
ARG NEW_RELIC_APPNAME
RUN curl -L https://download.newrelic.com/php_agent/archive/${NEW_RELIC_AGENT_VERSION}/newrelic-php5-${NEW_RELIC_AGENT_VERSION}-linux.tar.gz | tar -C /tmp -zx \
   && export NR_INSTALL_USE_CP_NOT_LN=1 \
   && export NR_INSTALL_SILENT=1 \
   && /tmp/newrelic-php5-${NEW_RELIC_AGENT_VERSION}-linux/newrelic-install install \
   && rm -rf /tmp/newrelic-php5-* /tmp/nrinstall*
RUN sed -i \
   -e "s/newrelic.license[[:space:]]*=[[:space:]]*.*/newrelic.license = ${NEW_RELIC_LICENSE_KEY}/" \
   -e "s/newrelic.appname[[:space:]]*=[[:space:]]*.*/newrelic.appname = ${NEW_RELIC_APPNAME}/" \
   -e "\$a newrelic.daemon.address=/tmp/.newrelic/newrelic.sock" \
   /etc/php/7.4/cli/conf.d/newrelic.ini

In the above Dockerfile snippet:

  • NEW_RELIC_AGENT_VERSION: The version of the New Relic agent you wish to install.

  • NEW_RELIC_LICENSE_KEY: Your New Relic license key.

  • NEW_RELIC_APPNAME: Your application name, which will be displayed in the New Relic dashboard.

4. Jenkins Job Modification

Next, we need to update our Jenkins job to build the Docker image with New Relic enabled. Modify the Jenkins build step to pass the New Relic parameters:

docker build -t <Account.id>.dkr.ecr.<Region>.amazonaws.com/<Image Name>/<env>:-$GIT_COMMIT \
  --build-arg NEW_RELIC_AGENT_VERSION=<version> \
  --build-arg NEW_RELIC_LICENSE_KEY=<license-key> \
  --build-arg NEW_RELIC_APPNAME="<application-name>"

Replace:

  • <version> with the correct agent version.

  • <license-key> with your New Relic license key.

  • <application-name> with the name of your application, e.g., Equip9_PHP_Backend_Dev.

5. Kubernetes Deployment Configuration

Now, we need to modify the Kubernetes deployment to integrate the New Relic daemon. Update your deployment.yaml to include the New Relic volume and daemon configuration:

volumeMounts:
  - name: newrelic-socket
    mountPath: /tmp/.newrelic
- name: newrelic-php-daemon
  image: newrelic/php-daemon
  ports:
    - containerPort: <port no>
  volumeMounts:
    - name: newrelic-socket
      mountPath: /tmp/.newrelic
volumes:
  - name: newrelic-socket
    emptyDir: {}

6. Trigger the Build

Once all the configuration is complete:

  1. Push the changes to your <Branch Name>branch.

  2. Trigger the Jenkins job to build and deploy the application with the New Relic agent.

Once the build is complete, use Lens or any Kubernetes management tool to check the status of the backend server:

7. Verifying New Relic Agent

After deployment, you can SSH into the pod and check the logs to ensure the New Relic agent is functioning properly.

cd /var/log
tail -f php_agent.log

You should see logs indicating that the New Relic agent is running.

8. Final Steps: New Relic Dashboard

After the setup is complete, go to the New Relic dashboard to monitor your application. You should see the performance data, including response times, transaction throughput, and errors:

Congratulations, your PHP backend is now successfully integrated with New Relic for application performance monitoring!


With these steps, your backend application is now under New Relic's monitoring umbrella. This setup provides deep insights into performance metrics, helping you ensure your application runs smoothly.

Happy coding!

#newrelic #monitoring #php #docker #jenkins #kubernetes #devops #applicationperformance #cloudmonitoring #Jenkins#CICD