Skip to content

Commit

Permalink
Merge pull request #227 from commercetools/datadog-sdk
Browse files Browse the repository at this point in the history
DEVX-390 - PHP sdk set up for Datadog
  • Loading branch information
barbara79 authored Sep 17, 2024
2 parents a86074d + f43b698 commit 2a74314
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ logs
/lambda/.serverless/
.idea/
docs/
.env
2 changes: 1 addition & 1 deletion examples/symfony-app/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ var/
vendor/
.editorconfig
.env.*.local
.env.local
docker/php/.env.sample
.env.local.php
.env.test
3 changes: 1 addition & 2 deletions examples/symfony-app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

###> symfony/framework-bundle ###
/.env.local
/docker/php/.env.local
/.env.local.php
/.env.*.local
/config/secrets/prod/prod.decrypt.private.php
/public/bundles/
/var/
/vendor/
/.env
###< symfony/framework-bundle ###
24 changes: 12 additions & 12 deletions examples/symfony-app/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Symfony demo app for New Relic

This Demo is to show how PHP SDK can be used in a Symfony application for a Docker environment with NewRelic monitoring enabled.
This Demo is to show how PHP SDK can be used in a Symfony application for a Docker environment with monitoring tools enabled.

For now, we configured the docker environment for Datadog and New Relic.

## Requirements

- PHP at least 8.1
- Symfony 6
- Docker
- Registration in New Relic
- Registration in New Relic/Datadog

## Installation

Expand All @@ -19,19 +21,17 @@ APP_CTP_CLIENT_ID= your CTP_CLIENT_ID
APP_CTP_CLIENT_SECRET= your CTP_CLIENT_SECRET
APP_CTP_PROJECT_KEY= your CTP_PROJECT_KEY
```
4. In the [docker-compose.yml](https://github.com/commercetools/commercetools-sdk-php-v2/blob/622c664ef69b93d96f11ac2ed26d24446d45dd0c/examples/symfony-app/docker-compose.yml) add the New Relic license key in the NEWRELIC_KEY: "<newrelic license key>" field
```yaml
args:
NEWRELIC_KEY: "replace this field with your New Relic license key"
```
4. Complete the .env file with your New Relic or Datadog credentials.

## Using the Symfony Demo app in a Docker Environment

### Configuring the Demo App

1. Open the Terminal
2. Run `composer dump-env prod`
3. Run `composer install` or `composer update`
2. Run `composer install` or `composer update`
3. Copy the [.env.sample](docker/php/.env.sample) file to the root folder renaming it to `.env`. So in the root folder run `cp docker/php/.env.sample .env`.
4. Fill the `.env` file with the missing data.
5. Run `composer dump-env prod`

### Preparing the Docker environment

Expand All @@ -43,7 +43,7 @@ APP_CTP_PROJECT_KEY= your CTP_PROJECT_KEY

1. Navigate to [http://localhost:8080/products](http://localhost:8080/products) or [http://localhost:8080/categories](http://localhost:8080/categories)
2. The result would be an array objects containing products or categories.
3. If it's not please make sure that in Merchant Center in the project key selected (see the environment variable for the client credentials in the point 3 of the Installation section), there would be some data for products and categories.
4. Go in the New Relic UI to see the monitoring of the API calls.

3. If it's not make sure that in Merchant Center in the project key selected (see the environment variable for the client credentials in the point 3 of the Installation section), there would be some data for products and categories.
4. Go in the New Relic/Datadog UI to see the monitoring of the API calls.

The docker configuration files will be found in the `docker/php` folder and the `docker-compose.yml` in the `symfony-app/` folder so they can be used in your application.
49 changes: 33 additions & 16 deletions examples/symfony-app/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
version: "3.4"

version: '3.8'

services:
application:
php-datadog:
build:
context: docker/php
dockerfile: Dockerfile
args:
NEWRELIC_KEY: "<newrelic license key>"
NEWRELIC_APP_NAME: "PHP_SDK"
tty: true
ports:
- 9000:9000
context: ./docker/php
dockerfile: Dockerfile.datadog
environment:
- DD_SERVICE=symfony-app
- DD_AGENT_HOST=datadog-agent
- DD_TRACE_AGENT_PORT=8126
- DD_API_KEY=${DD_API_KEY}
- DD_TRACE_DEBUG=true
- DD_LOGS_ENABLED=true
- DD_PROCESS_AGENT_ENABLED=true
volumes:
- .:/var/www/symfony
- ./var/logs/newrelic:/var/log/newrelic
- ./docker/php/.env.sample:/var/www/symfony/.env
expose:
- "9000"

nginx:
build:
context: docker/nginx
context: ./docker/nginx
dockerfile: Dockerfile
ports:
- 8080:80
links:
- application
- "8080:80"
depends_on:
- php-datadog
volumes:
- ./var/logs/nginx:/var/log/nginx
- .:/var/www/symfony

datadog-agent:
image: datadog/agent:latest
environment:
- DD_API_KEY=${DD_API_KEY}
- DD_SITE=datadoghq.com
- DD_APM_ENABLED=true
ports:
- "127.0.0.1:8126:8126/tcp"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "/proc/:/host/proc/:ro"
- "/sys/fs/cgroup/:/host/sys/fs/cgroup:ro"
9 changes: 7 additions & 2 deletions examples/symfony-app/docker/nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ FROM nginx

COPY symfony.conf /etc/nginx/conf.d/default.conf

RUN echo "upstream php-upstream { server application:9000; }" > /etc/nginx/conf.d/upstream.conf
# Update the upstream configuration to point to the correct PHP service
RUN echo "upstream php-upstream { server php-datadog:9000; \
# server php-newrelic:9001; \
}" > /etc/nginx/conf.d/upstream.conf

RUN usermod -u 1000 www-data

HEALTHCHECK --interval=30s --timeout=5s \
CMD curl -f http://localhost/ || exit 1
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=
APP_CTP_CLIENT_ID= your CTP_CLIENT_ID
APP_CTP_CLIENT_SECRET= your CTP_CLIENT_SECRET
APP_CTP_PROJECT_KEY= your CTP_PROJECT_KEY
APP_CTP_CLIENT_ID=your client id
APP_CTP_CLIENT_SECRET=your client secret
APP_CTP_PROJECT_KEY=your project key
DD_AGENT_HOST=datadog-agent
DD_TRACE_AGENT_PORT=8126
DD_SERVICE=symfony-app
DD_API_KEY=
DD_API_KEY=your datadog api key
DD_APM_ENABLED=true
DD_TRACE_CLI_ENABLED=true
DD_TRACE_ENABLED=true
NEWRELIC_KEY=
DD_SITE=datadoghq.com
NEWRELIC_KEY=your new relic key
NEWRELIC_APP_NAME=PHP-SDK
NEW_RELIC_AGENT_VERSION= # Specify the New Relic agent version you want

# APP_NAME=PHP-SDK
###< symfony/framework-bundle ###
39 changes: 0 additions & 39 deletions examples/symfony-app/docker/php/Dockerfile

This file was deleted.

30 changes: 30 additions & 0 deletions examples/symfony-app/docker/php/Dockerfile.datadog
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM php:8.2-fpm

# Install necessary packages and PHP extensions
RUN apt-get update \
&& apt-get install -y apt-utils wget git curl libicu-dev zlib1g-dev libonig-dev libzip-dev gnupg2 \
&& docker-php-ext-install intl mbstring zip \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Download Datadog setup script from the latest release and move it in /usr/local/bin
RUN curl -LO https://github.com/DataDog/dd-trace-php/releases/latest/download/datadog-setup.php \
&& mv datadog-setup.php /usr/local/bin/datadog-setup.php \
&& chmod +x /usr/local/bin/datadog-setup.php

# Copy Datadog configuration and setup script
COPY conf.d/datadog.ini /usr/local/etc/php/conf.d/

# Copy the .env.sample file from host to the container
COPY .env.sample /var/www/symfony/.env.sample

# Run the setup script
RUN php /usr/local/bin/datadog-setup.php --php-bin=all --enable-appsec --enable-profiling

HEALTHCHECK --interval=30s --timeout=5s CMD curl -f http://localhost/ || exit 1

WORKDIR /var/www/symfony/public

EXPOSE 9000

CMD ["php-fpm", "-F"]
32 changes: 32 additions & 0 deletions examples/symfony-app/docker/php/Dockerfile.newrelic
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Dockerfile for New Relic
FROM php:8.2-fpm

ARG NEWRELIC_KEY
ARG NEWRELIC_APP_NAME

# Install necessary packages and New Relic
RUN apt-get update \
&& apt-get install -y apt-utils wget git curl libicu-dev zlib1g-dev libonig-dev libzip-dev gnupg2 \
&& docker-php-ext-install intl mbstring zip \
&& echo 'deb http://apt.newrelic.com/debian/ newrelic non-free' | tee /etc/apt/sources.list.d/newrelic.list \
&& wget -qO - https://download.newrelic.com/548C16BF.gpg | apt-key add - \
&& apt-get update \
&& apt-get install -y newrelic-php5 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Copy New Relic configuration files
COPY conf.d/newrelic.ini /usr/local/etc/php/conf.d/
COPY conf.d/60-user.ini /usr/local/etc/php/conf.d/

# Configure New Relic
RUN sed -i \
-e 's/"REPLACE_WITH_REAL_KEY"/'"$NEWRELIC_KEY"'/' \
-e 's/newrelic.appname = "PHP Application"/newrelic.appname = '"$NEWRELIC_APP_NAME"'/' \
/usr/local/etc/php/conf.d/newrelic.ini

WORKDIR /var/www/symfony/public

EXPOSE 9000

CMD ["php-fpm", "-F"]
7 changes: 7 additions & 0 deletions examples/symfony-app/docker/php/conf.d/datadog.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[datadog]
ddtrace.request_init_hook=/opt/datadog-php/ddtrace-sources/bridge/dd_wrap_autoloader.php
ddtrace.enabled=true
ddtrace.agent_host=${DD_AGENT_HOST}
ddtrace.agent_port=${DD_TRACE_AGENT_PORT}
ddtrace.service=symfony-app
ddtrace.env=${DD_ENV}

0 comments on commit 2a74314

Please sign in to comment.