DEV Community

Cover image for Implementing Datadog APM in ECS on Fargate Environment
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Implementing Datadog APM in ECS on Fargate Environment

This article was originally published on bmf-tech.com.

Overview

A casual note on implementing Datadog APM in an ECS on Fargate environment.

Adjusting PHP Container Image

We are using a custom image based on the php-fpm image. Since the datadog-php-tracer is required, it is incorporated into the image as follows:

ENV DDTRACE_VERSION=0.65.1

RUN curl -Lo datadog-php-tracer.apk https://github.com/DataDog/dd-trace-php/releases/download/${DDTRACE_VERSION}/datadog-php-tracer_${DDTRACE_VERSION}_noarch.apk \
    && apk add datadog-php-tracer.apk --allow-untrusted \
    && rm datadog-php-tracer.apk
Enter fullscreen mode Exit fullscreen mode

In the fpm settings, configure it to read environment variables.

// fpm.www.conf
clear_env = yes

; Datadog APM
env[DD_AGENT_HOST] = $DD_AGENT_HOST
env[DD_SERVICE] = $DD_SERVICE
env[DD_VERSION] = $DD_VERSION
env[DD_ENV] = $DD_ENV
env[DD_TRACE_PHP_BIN] = $DD_TRACE_PHP_BIN
Enter fullscreen mode Exit fullscreen mode

cf. https://docs.datadoghq.com/agent/amazon_ecs/apm/?tab=ec2metadataendpoint#php-fpm

ECS Task Definition

Set up the necessary environment for both PHP and Datadog.

[
  {
    // PHP
    "environment": [
      {
        "name": "DD_AGENT_HOST",
        "value": "${DATADOG_AGENT_HOST}" // Since Datadog is a sidecar, localhost is fine here
      },
      {
        "name": "DD_SERVICE",
        "value": "${DATADOG_SERVICE}"
      },
      {
        "name": "DD_VERSION",
        "value": "${DATADOG_VERSION}"
      },
      {
        "name": "DD_ENV",
        "value": "${DATADOG_ENV}"
      },
      {
        "name": "DD_TRACE_PHP_BIN",
        "value": "${DATADOG_TRACE_PHP_BIN}"
      }
    ]
  },
  {
    // Datadog
    "environment": [
      {
        "name": "ECS_FARGATE",
        "value": "true"
      },
      {
        "name": "DD_APM_ENABLED",
        "value": "true"
      }
    ]
  }
Enter fullscreen mode Exit fullscreen mode

Conclusion

With this, APM should be operational. This is probably the minimum configuration required. If it doesn't work well, it's a good idea to check the datadog section in phpinfo().

Top comments (0)