Skip to content

Commit e27d2ef

Browse files
authored
Merge pull request #5 from DataDog/gabedos/dsd-apm-base
feat: APM/DSD Base Implementation
2 parents 9f852e7 + 781e494 commit e27d2ef

File tree

4 files changed

+143
-8
lines changed

4 files changed

+143
-8
lines changed

examples/ecs_fargate/main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ module "ecs_task" {
1818
},
1919
]
2020

21+
dd_dogstatsd = {
22+
dogstatsd_cardinality = "high",
23+
origin_detection_enabled = true,
24+
}
25+
26+
dd_apm = {
27+
enabled = true,
28+
}
29+
2130
# Configure Task Definition
2231
family = "datadog-terraform-app"
2332
container_definitions = {

modules/ecs_fargate/datadog.tf

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,100 @@
11
locals {
2-
base_env = var.dd_environment
2+
# Application container modifications
3+
is_linux = var.runtime_platform == null || var.runtime_platform.operating_system_family == null || var.runtime_platform.operating_system_family == "LINUX"
4+
is_apm_socket_mount = var.dd_apm.enabled && var.dd_apm.socket_enabled && local.is_linux
5+
is_dsd_socket_mount = var.dd_dogstatsd.enabled && var.dd_dogstatsd.socket_enabled && local.is_linux
6+
is_apm_dsd_volume = local.is_apm_socket_mount || local.is_dsd_socket_mount
37

8+
apm_dsd_mount = local.is_apm_dsd_volume ? [
9+
{
10+
containerPath = "/var/run/datadog"
11+
sourceVolume = "dd-sockets"
12+
readOnly = false
13+
}
14+
] : []
15+
16+
apm_socket_var = local.is_apm_socket_mount ? [
17+
{
18+
name = "DD_TRACE_AGENT_URL"
19+
value = "unix:///var/run/datadog/apm.socket"
20+
}
21+
] : []
22+
23+
dsd_socket_var = local.is_dsd_socket_mount ? [
24+
{
25+
name = "DD_DOGSTATSD_URL"
26+
value = "unix:///var/run/datadog/dsd.socket"
27+
}
28+
] : []
29+
30+
dsd_port_var = !local.is_dsd_socket_mount && var.dd_dogstatsd.enabled ? [
31+
{
32+
name = "DD_AGENT_HOST"
33+
value = "127.0.0.1"
34+
}
35+
] : []
36+
37+
modified_container_definitions = [
38+
for container in var.container_definitions : merge(
39+
container,
40+
{
41+
# Append new environment variables to any existing ones.
42+
environment = concat(
43+
lookup(container, "environment", []),
44+
local.dsd_socket_var,
45+
local.apm_socket_var,
46+
local.dsd_port_var,
47+
),
48+
# Append new volume mounts to any existing mountPoints.
49+
mountPoints = concat(
50+
lookup(container, "mountPoints", []),
51+
local.apm_dsd_mount
52+
)
53+
}
54+
)
55+
]
56+
57+
# Volume configuration for task
58+
apm_dsd_volume = local.is_apm_dsd_volume ? [
59+
{
60+
name = "dd-sockets"
61+
}
62+
] : []
63+
64+
modified_volumes = concat(
65+
[for k, v in coalesce(var.volumes, {}) : v],
66+
local.apm_dsd_volume
67+
)
68+
69+
# Datadog Agent container environment variables
470
dynamic_env = [
571
for pair in [
672
{ key = "DD_API_KEY", value = var.dd_api_key },
773
{ key = "DD_SITE", value = var.dd_site },
874
{ key = "DD_SERVICE", value = var.dd_service },
975
{ key = "DD_ENV", value = var.dd_env },
1076
{ key = "DD_VERSION", value = var.dd_version },
77+
{ key = "DD_DOGSTATSD_TAG_CARDINALITY", value = var.dd_dogstatsd.dogstatsd_cardinality }
1178
# TODO: clusterName, ddTags, etc.
1279
] : { name = pair.key, value = pair.value } if pair.value != null
1380
]
1481

15-
dd_agent_env = concat(local.base_env, local.dynamic_env)
82+
origin_detection_vars = var.dd_dogstatsd.origin_detection_enabled ? [
83+
{
84+
name = "DD_DOGSTATSD_ORIGIN_DETECTION"
85+
value = "true"
86+
},
87+
{
88+
name = "DD_DOGSTATSD_ORIGIN_DETECTION_CLIENT"
89+
value = "true"
90+
}
91+
] : []
92+
93+
dd_agent_env = concat(
94+
var.dd_environment,
95+
local.dynamic_env,
96+
local.origin_detection_vars,
97+
)
1698

1799
# Datadog Agent container definition
18100
dd_agent_container = {
@@ -36,6 +118,7 @@ locals {
36118
hostPort = 8126
37119
protocol = "tcp"
38120
}
39-
]
121+
],
122+
mountPoints = local.apm_dsd_mount
40123
}
41124
}

modules/ecs_fargate/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ resource "aws_ecs_task_definition" "this" {
77
container_definitions = jsonencode(
88
concat(
99
[local.dd_agent_container],
10-
[for k, v in var.container_definitions : v],
10+
[for k, v in local.modified_container_definitions : v],
1111
)
1212
)
1313

@@ -79,7 +79,7 @@ resource "aws_ecs_task_definition" "this" {
7979
task_role_arn = var.task_role_arn != null ? var.task_role_arn : (length(aws_iam_role.new_ecs_task_role) > 0 ? aws_iam_role.new_ecs_task_role[0].arn : null)
8080

8181
dynamic "volume" {
82-
for_each = var.volume
82+
for_each = local.modified_volumes
8383

8484
content {
8585
dynamic "docker_volume_configuration" {

modules/ecs_fargate/variables.tf

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,46 @@ variable "dd_version" {
5656
default = null
5757
}
5858

59+
variable "dd_dogstatsd" {
60+
description = "Configuration for Datadog DogStatsD"
61+
type = object({
62+
enabled = optional(bool, true)
63+
origin_detection_enabled = optional(bool, true)
64+
dogstatsd_cardinality = optional(string, "orchestrator")
65+
socket_enabled = optional(bool, true)
66+
})
67+
default = {
68+
enabled = true
69+
origin_detection_enabled = true
70+
dogstatsd_cardinality = "orchestrator"
71+
socket_enabled = true
72+
}
73+
validation {
74+
condition = var.dd_dogstatsd != null
75+
error_message = "The Datadog Dogstatsd configuration must be defined."
76+
}
77+
validation {
78+
condition = var.dd_dogstatsd.dogstatsd_cardinality == "low" || var.dd_dogstatsd.dogstatsd_cardinality == "orchestrator" || var.dd_dogstatsd.dogstatsd_cardinality == "high"
79+
error_message = "The Datadog Dogstatsd cardinality must be one of 'low', 'orchestrator', or 'high'."
80+
}
81+
}
82+
83+
variable "dd_apm" {
84+
description = "Configuration for Datadog APM"
85+
type = object({
86+
enabled = optional(bool, true)
87+
socket_enabled = optional(bool, true)
88+
})
89+
default = {
90+
enabled = true
91+
socket_enabled = true
92+
}
93+
validation {
94+
condition = var.dd_apm != null
95+
error_message = "The Datadog APM configuration must be defined."
96+
}
97+
}
98+
5999
################################################################################
60100
# Task Definition
61101
################################################################################
@@ -156,7 +196,10 @@ variable "requires_compatibilities" {
156196

157197
variable "runtime_platform" {
158198
description = "Configuration block for `runtime_platform` that containers in your task may use"
159-
type = any
199+
type = object({
200+
cpu_architecture = string
201+
operating_system_family = string
202+
})
160203
default = {
161204
operating_system_family = "LINUX"
162205
cpu_architecture = "X86_64"
@@ -187,8 +230,8 @@ variable "track_latest" {
187230
default = false
188231
}
189232

190-
variable "volume" {
233+
variable "volumes" {
191234
description = "Configuration block for volumes that containers in your task may use"
192235
type = any
193-
default = {}
236+
default = null
194237
}

0 commit comments

Comments
 (0)