Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newrelic APM support #171

Open
gaudinjeremy opened this issue Feb 16, 2024 · 4 comments
Open

Newrelic APM support #171

gaudinjeremy opened this issue Feb 16, 2024 · 4 comments

Comments

@gaudinjeremy
Copy link

gaudinjeremy commented Feb 16, 2024

Hello,

I tried to add the newrelic APM for php but I get this error:

 => ERROR [ 4/12] RUN   curl -L https://download.newrelic.com/php_agent/release/newrelic-php5-10.16.0.5-linux.tar.gz | tar -C /tmp -zx &&   export NR_INSTALL_USE_CP_NOT_LN=1 &&   export NR_INSTALL_SILE  0.8s
------                                                                                                                                                                                                          
 > [ 4/12] RUN   curl -L https://download.newrelic.com/php_agent/release/newrelic-php5-10.16.0.5-linux.tar.gz | tar -C /tmp -zx &&   export NR_INSTALL_USE_CP_NOT_LN=1 &&   export NR_INSTALL_SILENT=1 &&   /tmp/newrelic-php5-*/newrelic-install install &&   rm -rf /tmp/newrelic-php5-* /tmp/nrinstall* &&   sed -i       -e 's/"REPLACE_WITH_REAL_KEY"/"MY_LICENSE_KEY"/'       -e 's/newrelic.appname = "PHP Application"/newrelic.appname = "PHP APP"/'       -e 's/;newrelic.daemon.app_connect_timeout =.*/newrelic.daemon.app_connect_timeout=15s/'       -e 's/;newrelic.daemon.start_timeout =.*/newrelic.daemon.start_timeout=5s/'       /usr/local/etc/php82/conf.d/newrelic.ini:
0.100   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
0.100                                  Dload  Upload   Total   Spent    Left  Speed
100 24.9M  100 24.9M    0     0  38.6M      0 --:--:-- --:--:-- --:--:-- 38.6M
0.760 sed: /usr/local/etc/php82/conf.d/newrelic.ini: No such file or directory
------
Dockerfile:34
--------------------
  33 |     
  34 | >>> RUN \
  35 | >>>   curl -L https://download.newrelic.com/php_agent/release/newrelic-php5-10.16.0.5-linux.tar.gz | tar -C /tmp -zx && \
  36 | >>>   export NR_INSTALL_USE_CP_NOT_LN=1 && \
  37 | >>>   export NR_INSTALL_SILENT=1 && \
  38 | >>>   /tmp/newrelic-php5-*/newrelic-install install && \
  39 | >>>   rm -rf /tmp/newrelic-php5-* /tmp/nrinstall* && \
  40 | >>>   sed -i \
  41 | >>>       -e 's/"REPLACE_WITH_REAL_KEY"/"MY_LICENSE_KEY"/' \
  42 | >>>       -e 's/newrelic.appname = "PHP Application"/newrelic.appname = "PHP APP"/' \
  43 | >>>       -e 's/;newrelic.daemon.app_connect_timeout =.*/newrelic.daemon.app_connect_timeout=15s/' \
  44 | >>>       -e 's/;newrelic.daemon.start_timeout =.*/newrelic.daemon.start_timeout=5s/' \
  45 | >>>       /usr/local/etc/php82/conf.d/newrelic.ini
  46 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c curl -L https://download.newrelic.com/php_agent/release/newrelic-php5-10.16.0.5-linux.tar.gz | tar -C /tmp -zx &&   export NR_INSTALL_USE_CP_NOT_LN=1 &&   export NR_INSTALL_SILENT=1 &&   /tmp/newrelic-php5-*/newrelic-install install &&   rm -rf /tmp/newrelic-php5-* /tmp/nrinstall* &&   sed -i       -e 's/\"REPLACE_WITH_REAL_KEY\"/\"MY_LICENSE_KEY\"/'       -e 's/newrelic.appname = \"PHP Application\"/newrelic.appname = \"PHP APP\"/'       -e 's/;newrelic.daemon.app_connect_timeout =.*/newrelic.daemon.app_connect_timeout=15s/'       -e 's/;newrelic.daemon.start_timeout =.*/newrelic.daemon.start_timeout=5s/'       /usr/local/etc/php82/conf.d/newrelic.ini" did not complete successfully: exit code: 1

It seems to be a file path problem linked to this line "0.760 sed: /usr/local/etc/php82/conf.d/newrelic.ini: No such file or directory" but even by changing the directories I haven't managed to finalize the installation.

I followed the newrelic doc to install (https://docs.newrelic.com/docs/apm/agents/php-agent/advanced-installation/docker-other-container-environments-install-php-agent/#install-diff-containers)

Thanks

@TrafeX
Copy link
Owner

TrafeX commented Feb 23, 2024

Hi @gaudinjeremy,

Try this, this works for me:

    # New Relic settings
    ARG NEW_RELIC_LICENSE_KEY="your-license-key"
    ARG NEW_RELIC_APP_NAME="your-app-name"

    # Install New Relic
    RUN mkdir -p /var/log/newrelic /var/run/newrelic && \
        touch /var/log/newrelic/php_agent.log /var/log/newrelic/newrelic-daemon.log && \
        chmod -R g+ws /tmp /var/log/newrelic/ /var/run/newrelic/ && \
        chown -R 1001:0 /tmp /var/log/newrelic/ /var/run/newrelic/ && \
        # Download and install Newrelic binary
        export NEWRELIC_VERSION=$(curl -sS https://download.newrelic.com/php_agent/release/ | sed -n 's/.*>\(.*linux\).tar.gz<.*/\1/p') && \
        cd /tmp && curl -sS "https://download.newrelic.com/php_agent/release/${NEWRELIC_VERSION}.tar.gz" | gzip -dc | tar xf - && \
        cd "${NEWRELIC_VERSION}" && \
        NR_INSTALL_SILENT=true ./newrelic-install install && \
        rm -f /var/run/newrelic-daemon.pid && \
        rm -f /tmp/.newrelic.sock && \
        sed -i \
        -e "s/newrelic.license =.*/newrelic.license = ${NEW_RELIC_LICENSE_KEY}/" \
        -e "s/newrelic.appname =.*/newrelic.appname = ${NEW_RELIC_APP_NAME}/" \
        $PHP_INI_DIR/conf.d/newrelic.ini

@gaudinjeremy
Copy link
Author

Thanks @TrafeX,

The installation has been completed, but I haven't received any data from Newrelic.

I modified the code by adding this line to activate newrelic : "-e "s/;newrelic.enabled =.*/newrelic.enabled = true/" "

but this had no effect.

Here's my complete dockerfile

`ARG ALPINE_VERSION=3.18
FROM --platform=linux/amd64 alpine:${ALPINE_VERSION}
LABEL Maintainer="Tim de Pater code@trafex.nl"
LABEL Description="Lightweight container with Nginx 1.24 & PHP 8.2 based on Alpine Linux."

Setup document root

WORKDIR /var/www/html

Install packages and remove default server definition

RUN apk add --no-cache
curl
nginx
php82
php82-ctype
php82-curl
php82-dom
php82-fileinfo
php82-fpm
php82-gd
php82-intl
php82-mbstring
php82-mysqli
php82-opcache
php82-openssl
php82-phar
php82-session
php82-tokenizer
php82-xml
php82-xmlreader
php82-xmlwriter
php82-redis
supervisor

Configure nginx - http

COPY config/nginx.conf /etc/nginx/nginx.conf

Configure nginx - default server

COPY config/conf.d /etc/nginx/conf.d/

Configure PHP-FPM

ENV PHP_INI_DIR /etc/php82
COPY config/fpm-pool.conf ${PHP_INI_DIR}/php-fpm.d/www.conf
COPY config/php.ini ${PHP_INI_DIR}/conf.d/custom.ini

Configure supervisord

COPY config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

Make sure files/folders needed by the processes are accessable when they run under the nobody user

RUN chown -R nobody.nobody /var/www/html /run /var/lib/nginx /var/log/nginx

Create symlink for php

RUN ln -s /usr/bin/php82 /usr/bin/php

ARG NEW_RELIC_LICENSE_KEY="xxxxxxxxxx"
ARG NEW_RELIC_APP_NAME="app_name"

Install New Relic

RUN mkdir -p /var/log/newrelic /var/run/newrelic &&
touch /var/log/newrelic/php_agent.log /var/log/newrelic/newrelic-daemon.log &&
chmod -R g+ws /tmp /var/log/newrelic/ /var/run/newrelic/ &&
chown -R 1001:0 /tmp /var/log/newrelic/ /var/run/newrelic/ &&
# Download and install Newrelic binary
export NEWRELIC_VERSION=$(curl -sS https://download.newrelic.com/php_agent/release/ | sed -n 's/.>(.linux).tar.gz<./\1/p') &&
cd /tmp && curl -sS "https://download.newrelic.com/php_agent/release/${NEWRELIC_VERSION}.tar.gz" | gzip -dc | tar xf - &&
cd "${NEWRELIC_VERSION}" &&
NR_INSTALL_SILENT=true ./newrelic-install install &&
rm -f /var/run/newrelic-daemon.pid &&
rm -f /tmp/.newrelic.sock &&
sed -i
-e "s/newrelic.license =.
/newrelic.license = ${NEW_RELIC_LICENSE_KEY}/"
-e "s/newrelic.appname =./newrelic.appname = ${NEW_RELIC_APP_NAME}/"
-e "s/;newrelic.enabled =.
/newrelic.enabled = true/"
$PHP_INI_DIR/conf.d/newrelic.ini

Switch to use a non-root user from here on

USER nobody

Add application

COPY --chown=nobody src/ /var/www/html/

Expose the port nginx is reachable on

EXPOSE 8080

Let supervisord start nginx & php-fpm

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

Configure a healthcheck to validate that everything is up&running

HEALTHCHECK --timeout=10s CMD curl --silent --fail http://127.0.0.1:8080/fpm-ping
`

Thanks

@enricodeleo
Copy link

@gaudinjeremy this is the relevant code you need to add to your Dockerfile (discarding your previously implementation of APM). It is tested working on trafex/php-nginx:3.4.0.

Of course you need to build the image with the right --args and make sure the NEW_RELIC_DAEMON_ADDRESS is correct and reachable from the container once running (e.g. being on the same docker network or both being bound to host).

# Configure New Relic
ARG NEW_RELIC_LICENSE_KEY
ARG NEW_RELIC_APP_NAME
ARG NEW_RELIC_DAEMON_ADDRESS

ENV NEW_RELIC_LICENSE_KEY ${NEW_RELIC_LICENSE_KEY}
ENV NEW_RELIC_APP_NAME ${NEW_RELIC_APP_NAME}
ENV NEW_RELIC_DAEMON_ADDRESS ${NEW_RELIC_DAEMON_ADDRESS}

# Install New Relic PHP Agent BEFORE SWITCHING USER to nobody
RUN mkdir -p /var/log/newrelic /var/run/newrelic && \
    touch /var/log/newrelic/php_agent.log /var/log/newrelic/newrelic-daemon.log && \
    chmod -R g+ws /tmp /var/log/newrelic/ /var/run/newrelic/ && \
    chown -R 1001:0 /tmp /var/log/newrelic/ /var/run/newrelic/ && \
    # Download and install Newrelic binary
    export NEWRELIC_VERSION=$(curl -sS https://download.newrelic.com/php_agent/release/ | sed -n 's/.*>\(.*linux\).tar.gz<.*/\1/p') && \
    cd /tmp && curl -sS "https://download.newrelic.com/php_agent/release/${NEWRELIC_VERSION}.tar.gz" | gzip -dc | tar xf - && \
    cd "${NEWRELIC_VERSION}" && \
    NR_INSTALL_SILENT=true ./newrelic-install install && \
    rm -f /var/run/newrelic-daemon.pid && \
    rm -f /tmp/.newrelic.sock && \
    sed -i \
    -e "s/newrelic.license =.*/newrelic.license = ${NEW_RELIC_LICENSE_KEY}/" \
    -e "s/newrelic.appname =.*/newrelic.appname = ${NEW_RELIC_APP_NAME}/" \
    -e "s/;newrelic.enabled = true/newrelic.enabled = true/" \
    -e "s/;newrelic.daemon.address =.*/newrelic.daemon.address = \"${NEW_RELIC_DAEMON_ADDRESS}:31339\"/" \
    $PHP_INI_DIR/conf.d/newrelic.ini

@gaudinjeremy
Copy link
Author

@enricodeleo

Thanks for the reply, in the meantime I'd found a solution by putting the deamon in another container, which works perfectly.

Best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants