From 0f487a3427c5ac26a19ea8f0218adc9c0bb2e0f0 Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Wed, 1 Mar 2023 14:58:28 -0600 Subject: [PATCH] Add layer for Datadog extensions Closes #35 --- Readme.md | 5 +++++ docs/datadog.md | 41 ++++++++++++++++++++++++++++++++++++++ layers/datadog/Dockerfile | 35 ++++++++++++++++++++++++++++++++ layers/datadog/config.json | 7 +++++++ layers/datadog/test.php | 8 ++++++++ 5 files changed, 96 insertions(+) create mode 100644 docs/datadog.md create mode 100644 layers/datadog/Dockerfile create mode 100644 layers/datadog/config.json create mode 100644 layers/datadog/test.php diff --git a/Readme.md b/Readme.md index 84b4703c..58c7736b 100644 --- a/Readme.md +++ b/Readme.md @@ -44,6 +44,7 @@ functions: | Blackfire | `${bref-extra:blackfire-php-81}` | | Calendar | `${bref-extra:calendar-php-81}` | | Cassandra | `${bref-extra:cassandra-php-81}` | +| Datadog | `${bref-extra:datadog-php-81}` | | Decimal | `${bref-extra:decimal-php-81}` | | DS | `${bref-extra:ds-php-81}` | | Elastic APM | `${bref-extra:elastic-apm-php-81}` | @@ -136,6 +137,10 @@ in your `serverless.yaml` in order to tell unixODBC to load the required ini fil Read [the New Relic tutorial](docs/newrelic.md). +### Datadog + +Read [the notes on configuring the Datadog layer](docs/datadog.md). + ## Docker images There are Docker images for every layer. They are updated on every push to master diff --git a/docs/datadog.md b/docs/datadog.md new file mode 100644 index 00000000..4b202583 --- /dev/null +++ b/docs/datadog.md @@ -0,0 +1,41 @@ +# Configuring the Datadog Layer + +The Datadog layer provides the [Datadog Agent][] and [ddtrace extension for PHP][]. + +When using this layer, you must set the following environment variables: + +- `DD_API_KEY` + +It may be necessary to set the following environment variables when running +PHP in a serverless environment: + +- `DD_TRACE_CLI_ENABLED` - set this value to `1` (the default is `0`) + +The following environment variables may be useful or necessary, depending on +how you use Datadog: + +- `DD_SITE` - this defaults to `datadoghq.com` +- `DD_LOG_LEVEL` - e.g., `trace`, `debug`, `info`, `warn`, `error`, `critical`, `off` +- `DD_SERVICE` - the name of your service as it should appear in Datadog +- `DD_VERSION` - the version of your service, for display and filtering in Datadog +- `DD_ENV` - the environment your service is running in (i.e., staging, prod, etc.), + for display and filtering in Datadog + +Using environment variables, you may set any of the [Datadog Agent environment +variables][] or [PHP ddtrace environment variables][] in your `serverless.yml` +configuration, or you may configure them using a custom PHP INI file with Bref, +as described in the [Bref documentation][]. + +All values not set in your configuration will use the default [INI settings][] +for the extension. + +See the [Datadog documentation][] for more information about serverless +monitoring for AWS Lambda. + +[datadog agent]: https://docs.datadoghq.com/agent/ +[ddtrace extension for php]: https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/php/ +[datadog agent environment variables]: https://docs.datadoghq.com/containers/docker/apm/?tab=linux#docker-apm-agent-environment-variables +[php ddtrace environment variables]: https://docs.datadoghq.com/tracing/trace_collection/library_config/php/#environment-variable-configuration +[bref documentation]: https://bref.sh/docs/environment/php.html +[ini settings]: https://docs.datadoghq.com/tracing/trace_collection/library_config/php/ +[datadog documentation]: https://docs.datadoghq.com/serverless/aws_lambda diff --git a/layers/datadog/Dockerfile b/layers/datadog/Dockerfile new file mode 100644 index 00000000..1d556abe --- /dev/null +++ b/layers/datadog/Dockerfile @@ -0,0 +1,35 @@ +ARG PHP_VERSION +FROM bref/build-php-$PHP_VERSION:1.7.14 AS ext + +ENV DDTRACE_BUILD_DIR=${BUILD_DIR}/ddtrace + +RUN set -xe; \ + mkdir -p ${DDTRACE_BUILD_DIR}; \ + curl -Ls -o ${DDTRACE_BUILD_DIR}/datadog-setup.php \ + https://github.com/DataDog/dd-trace-php/releases/latest/download/datadog-setup.php + +WORKDIR ${DDTRACE_BUILD_DIR} + +RUN php datadog-setup.php --php-bin=all + +RUN cp "$(php-config --extension-dir)/ddtrace.so" /tmp/ddtrace.so +RUN cp "$(php-config --extension-dir)/ddappsec.so" /tmp/ddappsec.so +RUN cp "$(php-config --extension-dir)/datadog-profiling.so" /tmp/datadog-profiling.so +RUN cp "$(php-config --ini-dir)/98-ddtrace.ini" /tmp/ext.ini + +RUN sed -i 's/extension = ddtrace\.so/extension = \/opt\/bref-extra\/ddtrace.so/' /tmp/ext.ini +RUN sed -i 's/extension = ddappsec\.so/extension = \/opt\/bref-extra\/ddappsec.so/' /tmp/ext.ini +RUN sed -i 's/extension = datadog-profiling\.so/;extension = \/opt\/bref-extra\/datadog-profiling.so/' /tmp/ext.ini +RUN sed -i 's/datadog\.appsec\.enabled = On/datadog.appsec.enabled = Off/' /tmp/ext.ini + +FROM scratch + +COPY --from=ext /tmp/ddtrace.so /opt/bref-extra/ddtrace.so +COPY --from=ext /tmp/ddappsec.so /opt/bref-extra/ddappsec.so +COPY --from=ext /tmp/datadog-profiling.so /opt/bref-extra/datadog-profiling.so +COPY --from=ext /tmp/ext.ini /opt/bref/etc/php/conf.d/98-ddtrace.ini +COPY --from=ext /opt/datadog/ /opt/datadog + +# This adds the Datadog Agent to the layer. +# Refer to https://gallery.ecr.aws/datadog/lambda-extension +COPY --from=public.ecr.aws/datadog/lambda-extension:latest /opt/extensions/ /opt/extensions diff --git a/layers/datadog/config.json b/layers/datadog/config.json new file mode 100644 index 00000000..a5a35316 --- /dev/null +++ b/layers/datadog/config.json @@ -0,0 +1,7 @@ +{ + "php": [ + "80", + "81", + "82" + ] +} diff --git a/layers/datadog/test.php b/layers/datadog/test.php new file mode 100644 index 00000000..5168713c --- /dev/null +++ b/layers/datadog/test.php @@ -0,0 +1,8 @@ +