Setting Up Server Monitoring with Datadog
Datadog is a SaaS monitoring platform with an agent on servers. It collects infrastructure metrics, APM (request tracing), logs, and synthetic tests in one interface.
Installing the Agent
# Ubuntu/Debian
DD_API_KEY="your-api-key" DD_SITE="datadoghq.eu" \
bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script_agent7.sh)"
# Docker
docker run -d --name datadog-agent \
-e DD_API_KEY="your-api-key" \
-e DD_SITE="datadoghq.eu" \
-e DD_LOGS_ENABLED=true \
-e DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL=true \
-e DD_APM_ENABLED=true \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-v /proc/:/host/proc/:ro \
-v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro \
gcr.io/datadoghq/agent:7
Agent Configuration
# /etc/datadog-agent/datadog.yaml
api_key: your-api-key
site: datadoghq.eu
hostname: web01.example.com
tags:
- env:production
- app:myapp
- region:eu-west-1
logs_enabled: true
apm_config:
enabled: true
process_config:
enabled: true
# Integrations
integrations:
nginx:
- nginx_status_url: http://localhost/nginx_status
php_fpm:
- status_url: http://localhost/status
ping_url: http://localhost/ping
postgres:
- host: localhost
username: datadog
password: ENC[k8s_secret,v1.0/namespace/secret/pass]
dbname: myapp
PostgreSQL: Monitoring User
CREATE USER datadog WITH PASSWORD 'secure-password';
GRANT pg_monitor TO datadog;
GRANT SELECT ON pg_stat_database TO datadog;
Laravel: Datadog APM
// composer.json
// "datadog/dd-trace": "^0.90"
// Custom operation tracing
use DDTrace\GlobalTracer;
class OrderService
{
public function processOrder(Order $order): void
{
$tracer = GlobalTracer::get();
$span = $tracer->startActiveSpan('order.process');
try {
$span->setTag('order.id', $order->id);
$span->setTag('order.total', $order->total);
$this->validateInventory($order);
$this->chargePayment($order);
$this->sendConfirmation($order);
} catch (\Throwable $e) {
$span->setError($e);
throw $e;
} finally {
$span->finish();
}
}
}
Alerts via Datadog Monitor
resource "datadog_monitor" "cpu_high" {
name = "High CPU on web servers"
type = "metric alert"
query = "avg(last_5m):avg:system.cpu.user{env:production} by {host} > 85"
message = <<-EOT
CPU usage exceeded 85% on {{host.name}}.
@slack-monitoring
EOT
thresholds = {
critical = 85
warning = 75
}
notify_no_data = true
renotify_interval = 60
tags = ["env:production", "app:myapp"]
}
resource "datadog_monitor" "error_rate" {
name = "High error rate"
type = "metric alert"
query = "sum(last_5m):sum:trace.web.request.errors{env:production}.as_count() / sum:trace.web.request.hits{env:production}.as_count() * 100 > 5"
message = "Error rate > 5% @pagerduty-oncall"
thresholds = { critical = 5, warning = 2 }
}
Implementation Timeline
Installing Datadog Agent with basic integrations (Nginx, PHP-FPM, PostgreSQL): 1–2 days. With APM tracing for Laravel/Node.js and monitor configuration: 2–3 days.







