Skip to content

Commit

Permalink
Add layer for Datadog extensions
Browse files Browse the repository at this point in the history
Closes #35
  • Loading branch information
ramsey committed Mar 6, 2023
1 parent c65591e commit 0f487a3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}` |
Expand Down Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions docs/datadog.md
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions layers/datadog/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions layers/datadog/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"php": [
"80",
"81",
"82"
]
}
8 changes: 8 additions & 0 deletions layers/datadog/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

if (!function_exists($func = '\DDTrace\trace_method')) {
echo sprintf('FAIL: Function "%s" does not exist.', $func).PHP_EOL;
exit(1);
}

exit(0);

0 comments on commit 0f487a3

Please sign in to comment.