Skip to main content

Datadog Agent Setup for Python App on EC2

Purpose: Install and configure the Datadog Agent to collect host metrics and APM traces from a Python FastAPI application. This setup works for autoscaling environments and ensures host metrics and traces are reported automatically.

1. Prerequisites

  • EC2 instance with Python 3.8+ and a virtual environment (venv) installed.
  • Datadog account with API key.
  • Security group allowing outbound HTTPS (port 443) for agent reporting.
  • Python app installed at /home/<user>/<app-name> (example uses example-app).

2. Install Datadog Agent

  1. Set environment variables for your Datadog API key, site, and environment:
  1. Install Datadog Agent v7:
  1. Verify installation:

3. Configure Datadog Agent

  1. Edit the main configuration file:
Ensure the following entries exist:
  1. Restart the agent:
  1. Verify APM listener:
  • Should show listener running on 127.0.0.1:8126.

4. Python Application Setup (example-app)

Directory structure example:
  1. Activate virtual environment and install dependencies:

5. Create Systemd Service for Python App

  1. Create the unit file /etc/systemd/system/example-app.service:
  1. Enable and start the service:
ddtrace-run ensures traces from the Python app are sent to the local Datadog Agent. Logs are captured in the systemd journal and can be viewed using journalctl.

6. Verification

  1. Host Metrics:
    • Go to Datadog Dashboard → Host Metrics
    • Verify CPU, memory, disk, and network metrics from the EC2 instance.
  2. APM Traces:
    • Go to Datadog APM → Tracing Dashboard
    • Requests to your FastAPI app should appear automatically with traces.
  3. Agent Status:
    Ensure:
    • Forwarder API key is valid.
    • Core checks (system.cpu, system.mem, etc.) show OK.
    • APM Agent listener is running.

✅ References