Skip to content

Commit ebc9ac1

Browse files
modanetsMaksym Odanets
and
Maksym Odanets
authored
Added datadog extension (#442)
Co-authored-by: Maksym Odanets <[email protected]>
1 parent 5e0a69c commit ebc9ac1

File tree

5 files changed

+186
-47
lines changed

5 files changed

+186
-47
lines changed

Readme.md

+52-47
Original file line numberDiff line numberDiff line change
@@ -42,53 +42,54 @@ functions:
4242
4343
### Available layers
4444
45-
| Name | Serverless config (php 8.1) |
46-
|:-------------------|:--------------------------------------|
47-
| AMQP | `${bref-extra:amqp-php-81}` |
48-
| Blackfire | `${bref-extra:blackfire-php-81}` |
49-
| Bsdiff | `${bref-extra:bsdiff-php-81}` |
50-
| Calendar | `${bref-extra:calendar-php-81}` |
51-
| Cassandra | `${bref-extra:cassandra-php-81}` |
52-
| Decimal | `${bref-extra:decimal-php-81}` |
53-
| DS | `${bref-extra:ds-php-81}` |
54-
| Elastic APM | `${bref-extra:elastic-apm-php-81}` |
55-
| GD | `${bref-extra:gd-php-81}` |
56-
| gnupg | `${bref-extra:gnupg-php-81}` |
57-
| GMP | `${bref-extra:gmp-php-81}` |
58-
| gRPC | `${bref-extra:grpc-php-81}` |
59-
| Igbinary | `${bref-extra:igbinary-php-81}` |
60-
| Imagick | `${bref-extra:imagick-php-81}` |
61-
| IMAP | `${bref-extra:imap-php-81}` |
62-
| LDAP | `${bref-extra:ldap-php-81}` |
63-
| Mailparse | `${bref-extra:mailparse-php-81}` |
64-
| MaxMind DB | `${bref-extra:maxminddb-php-81}` |
65-
| Memcache | `${bref-extra:memcache-php-81}` |
66-
| Memcached | `${bref-extra:memcached-php-81}` |
67-
| MongoDB | `${bref-extra:mongodb-php-81}` |
68-
| MsgPack | `${bref-extra:msgpack-php-81}` |
69-
| Newrelic | `${bref-extra:newrelic-php-81}` |
70-
| ODBC Snowflake | `${bref-extra:odbc-snowflake-php-81}` |
71-
| OpenSwoole | `${bref-extra:openswoole-php-81}` |
72-
| Oracle | `${bref-extra:oci8-php-80}` |
73-
| Pcov | `${bref-extra:pcov-php-81}` |
74-
| PostgreSQL | `${bref-extra:pgsql-php-81}` |
75-
| RdKafka | `${bref-extra:rdkafka-php-81}` |
76-
| Redis (phpredis) | `${bref-extra:redis-php-81}` |
77-
| Redis-Igbinary | `${bref-extra:redis-igbinary-php-81}` |
78-
| Relay | `${bref-extra:relay-php-81}` |
79-
| Scout APM | `${bref-extra:scoutapm-php-81}` |
80-
| Scrypt | `${bref-extra:scrypt-php-81}` |
81-
| SPX | `${bref-extra:spx-php-81}` |
82-
| SSH2 | `${bref-extra:ssh2-php-81}` |
83-
| Swoole | `${bref-extra:swoole-php-81}` |
84-
| Symfony Runtime | `${bref-extra:symfony-runtime-php-81}`|
85-
| Microsoft SQLSRV | `${bref-extra:sqlsrv-php-81}` |
86-
| Tideways | `${bref-extra:tideways-php-81}` |
87-
| Tidy | `${bref-extra:tidy-php-81}` |
88-
| UUID | `${bref-extra:uuid-php-81}` |
89-
| Xdebug | `${bref-extra:xdebug-php-81}` |
90-
| Xlswriter | `${bref-extra:xlswriter-php-81}` |
91-
| Yaml | `${bref-extra:yaml-php-81}` |
45+
| Name | Serverless config (php 8.1) |
46+
|:-----------------|:---------------------------------------|
47+
| AMQP | `${bref-extra:amqp-php-81}` |
48+
| Blackfire | `${bref-extra:blackfire-php-81}` |
49+
| Bsdiff | `${bref-extra:bsdiff-php-81}` |
50+
| Calendar | `${bref-extra:calendar-php-81}` |
51+
| Cassandra | `${bref-extra:cassandra-php-81}` |
52+
| Datadog | `${bref-extra:datadog-php-81}` |
53+
| Decimal | `${bref-extra:decimal-php-81}` |
54+
| DS | `${bref-extra:ds-php-81}` |
55+
| Elastic APM | `${bref-extra:elastic-apm-php-81}` |
56+
| GD | `${bref-extra:gd-php-81}` |
57+
| gnupg | `${bref-extra:gnupg-php-81}` |
58+
| GMP | `${bref-extra:gmp-php-81}` |
59+
| gRPC | `${bref-extra:grpc-php-81}` |
60+
| Igbinary | `${bref-extra:igbinary-php-81}` |
61+
| Imagick | `${bref-extra:imagick-php-81}` |
62+
| IMAP | `${bref-extra:imap-php-81}` |
63+
| LDAP | `${bref-extra:ldap-php-81}` |
64+
| Mailparse | `${bref-extra:mailparse-php-81}` |
65+
| MaxMind DB | `${bref-extra:maxminddb-php-81}` |
66+
| Memcache | `${bref-extra:memcache-php-81}` |
67+
| Memcached | `${bref-extra:memcached-php-81}` |
68+
| MongoDB | `${bref-extra:mongodb-php-81}` |
69+
| MsgPack | `${bref-extra:msgpack-php-81}` |
70+
| Newrelic | `${bref-extra:newrelic-php-81}` |
71+
| ODBC Snowflake | `${bref-extra:odbc-snowflake-php-81}` |
72+
| OpenSwoole | `${bref-extra:openswoole-php-81}` |
73+
| Oracle | `${bref-extra:oci8-php-80}` |
74+
| Pcov | `${bref-extra:pcov-php-81}` |
75+
| PostgreSQL | `${bref-extra:pgsql-php-81}` |
76+
| RdKafka | `${bref-extra:rdkafka-php-81}` |
77+
| Redis (phpredis) | `${bref-extra:redis-php-81}` |
78+
| Redis-Igbinary | `${bref-extra:redis-igbinary-php-81}` |
79+
| Relay | `${bref-extra:relay-php-81}` |
80+
| Scout APM | `${bref-extra:scoutapm-php-81}` |
81+
| Scrypt | `${bref-extra:scrypt-php-81}` |
82+
| SPX | `${bref-extra:spx-php-81}` |
83+
| SSH2 | `${bref-extra:ssh2-php-81}` |
84+
| Swoole | `${bref-extra:swoole-php-81}` |
85+
| Symfony Runtime | `${bref-extra:symfony-runtime-php-81}` |
86+
| Microsoft SQLSRV | `${bref-extra:sqlsrv-php-81}` |
87+
| Tideways | `${bref-extra:tideways-php-81}` |
88+
| Tidy | `${bref-extra:tidy-php-81}` |
89+
| UUID | `${bref-extra:uuid-php-81}` |
90+
| Xdebug | `${bref-extra:xdebug-php-81}` |
91+
| Xlswriter | `${bref-extra:xlswriter-php-81}` |
92+
| Yaml | `${bref-extra:yaml-php-81}` |
9293

9394
### Blackfire installation
9495

@@ -142,6 +143,10 @@ in your `serverless.yaml` in order to tell unixODBC to load the required ini fil
142143

143144
Read [the New Relic tutorial](docs/newrelic.md).
144145

146+
### Datadog
147+
148+
Read [the Datadog tutorial](docs/datadog.md).
149+
145150
## Docker images
146151

147152
There are Docker images for every layer. They are updated on every push to master

docs/datadog.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
### Datadog installation
2+
3+
This layer enables the installation of only the Datadog PHP extension.
4+
5+
To use the extension, you must also install the Datadog agent.
6+
You can achieve this by installing another Datadog Lambda Extension layer.
7+
For more information see https://docs.datadoghq.com/serverless/installation/python/?tab=custom the "Install the Datadog Lambda Extension" section.
8+
9+
If you are running an x86-based Lambda in AWS commercial regions, use the following ARN:
10+
`arn:aws:lambda:<AWS_REGION>:464622532012:layer:Datadog-Extension:<version>`
11+
12+
If you are running an ARM-based Lambda in AWS commercial regions, use the following ARN:
13+
`arn:aws:lambda:<AWS_REGION>:464622532012:layer:Datadog-Extension-ARM:<version>`
14+
15+
## Configuration
16+
17+
After installing the layer and the agent, you must configure the Datadog extension by adding the following key/value pair to a Lambda environment variable:
18+
19+
- `DD_ENV=<enviroment>`
20+
- `DD_SERVICE=<service>`
21+
- `DD_SITE=<datadoghq.eu|datadoghq.com>` (depending on your Datadog account region)
22+
- `DD_API_KEY=<api_key>`
23+
- `DD_SERVICE=<your service name>`
24+
- `DD_VERSION=<your service version>`
25+
26+
For more details about the configuration, see https://docs.datadoghq.com/tracing/trace_collection/library_config/php/
27+
28+
You should also consider adding similar AWS tags to your Lambda function to link APM and Infrastructure data together using the tag/value syntax:
29+
30+
- `Env=<the same as DD_ENV>`
31+
- `Service=<the same as DD_SERVICE>`
32+
- `Team=<the team name>` (use `a-z0-9\-` pattern)
33+
- `Tier=<A|B|C|...>` (to indicate how important the service is)
34+
35+
Find more about infrastructure tagging:
36+
- https://www.datadoghq.com/blog/tagging-best-practices/
37+
- https://learn.datadoghq.com/courses/tagging-best-practices (online course)
38+
- Enroll in free technical sessions at https://www.datadoghq.com/technical-enablement/sessions/
39+
40+
## Custom instrumentation
41+
42+
DataDog works out of the box with the php-xx-fpm runtime. However, if you are using the php-xx runtime and BREF_LOOP_MAX>1, you must add custom instrumentation.
43+
Otherwise, DataDog will wait until the end of a loop and only send one trace.
44+
To add custom instrumentation, create an `instrumentation.php` file and add the following code:
45+
46+
```php
47+
<?php
48+
49+
\DDTrace\trace_method(
50+
'Bref\Runtime\Invoker',
51+
'invoke',
52+
function (\DDTrace\SpanData $span, $args, $ret, $exception) {
53+
$span->service = getenv('DD_SERVICE');
54+
$span->type = \DDTrace\Type::CLI;
55+
$span->name = 'invoke';
56+
}
57+
);
58+
```
59+
60+
This code will enable you to see all traces.
61+
62+
*do not forget to add `instrumentation.php` to a `composer.json` file
63+
64+
For EventBridge Lambdas, add the following code to the function above:
65+
66+
```php
67+
<?php
68+
if ($args[0] instanceof \Bref\Event\EventBridge\EventBridgeEvent) {
69+
$span->resource = $args[0]->getDetailType();
70+
}
71+
```
72+
73+
This code will name your traces with the EventBridge event name.
74+
75+
For more information about custom instrumentation, refer to the following resources:
76+
77+
- https://github.com/DataDog/dd-trace-php/blob/master/examples/long-running/long-running-script.php
78+
- https://docs.datadoghq.com/tracing/trace_collection/custom_instrumentation/php/?tab=currentspan
79+
- to link traces with a parent one: https://docs.datadoghq.com/tracing/trace_collection/trace_context_propagation/php/
80+
81+
## service.datadog.yaml
82+
83+
Consider also creating a `service.datadog.yaml` file in the root of your project.
84+
It will give you more control over the traces and ability to add links to documentation and contact information.
85+
86+
To find more: https://www.datadoghq.com/blog/manage-service-catalog-categories-with-service-definition-json-schema/

layers/datadog/Dockerfile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
ARG PHP_VERSION
2+
ARG BREF_VERSION
3+
FROM bref/build-php-$PHP_VERSION:$BREF_VERSION AS ext
4+
5+
ENV DDTRACE_BUILD_DIR=${BUILD_DIR}/ddtrace
6+
7+
RUN set -xe; \
8+
mkdir -p ${DDTRACE_BUILD_DIR}; \
9+
curl -Ls -o ${DDTRACE_BUILD_DIR}/datadog-setup.php \
10+
https://github.com/DataDog/dd-trace-php/releases/latest/download/datadog-setup.php
11+
12+
WORKDIR ${DDTRACE_BUILD_DIR}
13+
14+
RUN php datadog-setup.php --php-bin=all --enable-profiling
15+
16+
RUN cp "$(php-config --extension-dir)/ddtrace.so" /tmp/ddtrace.so && \
17+
cp "$(php-config --extension-dir)/datadog-profiling.so" /tmp/datadog-profiling.so && \
18+
cp "$(php-config --ini-dir)/98-ddtrace.ini" /tmp/ext.ini
19+
20+
RUN sed -i 's/extension = ddtrace\.so/extension = \/opt\/bref-extra\/ddtrace.so/' /tmp/ext.ini && \
21+
sed -i 's/extension = datadog-profiling\.so/extension = \/opt\/bref-extra\/datadog-profiling.so/' /tmp/ext.ini && \
22+
sed -i 's/;datadog\.agent_host.*$/datadog\.agent_host = 127.0.0.1/' /tmp/ext.ini && \
23+
sed -i 's/^.*datadog\.appsec\.enabled.*$/datadog.appsec.enabled = Off/' /tmp/ext.ini && \
24+
sed -i 's/datadog\.profiling\.enabled = On/datadog.profiling.enabled = 1/' /tmp/ext.ini
25+
26+
# Build the final image with just the files we need
27+
FROM scratch
28+
29+
# Copy things we installed to the final image
30+
COPY --from=ext /tmp/ddtrace.so /opt/bref-extra/ddtrace.so
31+
COPY --from=ext /tmp/datadog-profiling.so /opt/bref-extra/datadog-profiling.so
32+
COPY --from=ext /tmp/ext.ini /opt/bref/etc/php/conf.d/98-ddtrace.ini
33+
COPY --from=ext /opt/datadog/ /opt/datadog

layers/datadog/config.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"php": [
3+
"80",
4+
"81",
5+
"82"
6+
]
7+
}

layers/datadog/test.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
if (!class_exists($class = \DDTrace\SpanData::class)) {
4+
echo sprintf('FAIL: Class "%s" does not exist.', $class).PHP_EOL;
5+
exit(1);
6+
}
7+
8+
exit(0);

0 commit comments

Comments
 (0)