Skip to content

Commit

Permalink
Add support for configuring APM injection (#481)
Browse files Browse the repository at this point in the history
Co-authored-by: Jon Bodner <[email protected]>
Co-authored-by: Heston Hoffman <[email protected]>
Co-authored-by: Slavek Kabrda <[email protected]>
  • Loading branch information
4 people authored Jun 27, 2023
1 parent a8ddc45 commit 16e6af9
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 49 deletions.
77 changes: 40 additions & 37 deletions README.md

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ datadog_agent_version: ""
# Default Package name for APT and RPM installs - can override in playbook for IOT Agent
datadog_agent_flavor: "datadog-agent"

# Default Package name for APM Library Injection APT and RPM installs.
datadog_inject_apm_flavor: "datadog-apm*"

# Default apt repo and keyserver

# By default, the role uses the official apt Datadog repository for the chosen major version
Expand Down Expand Up @@ -170,6 +173,21 @@ datadog_windows_config_root: "{{ ansible_facts.env['ProgramData'] }}\\Datadog"
# arguments to supply to the windows installer.
win_install_args: " "

# Configure APM host injection. Possible values are: "all", "host" or "docker".
datadog_apm_instrumentation_enabled: ""

# List of APM libraries to install if host or Docker injection is enabled.
# You can see the available values in our official docs: https://docs.datadoghq.com/tracing/trace_collection/library_injection_local
datadog_apm_instrumentation_languages: ["all"]

# Configure Docker APM injection.
# See: https://docs.datadoghq.com/tracing/trace_collection/library_injection_local/?tab=agentandappinseparatecontainers#configure-docker-injection
datadog_apm_instrumentation_docker_config:
library_inject: true
log_level: info
output_paths:
- stderr
config_sources: BASIC

#
# Internal variables
Expand Down
73 changes: 73 additions & 0 deletions tasks/apm-inject-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
- name: Fail if the Datadog Agent version is not compatible with APM host injection
fail:
msg: APM Host Injection is not supported for this datadog-agent version.
when: agent_datadog_agent_major_version | int < 7

- name: Fail if APM Host injection is not supported on this host
fail:
msg: APM Host Injection is not supported in this platform.
when: ansible_facts.os_family not in ["Debian", "RedHat", "Rocky", "AlmaLinux"]

- name: Fail if APM Host injection type does not contain a supported value
fail:
msg: The provided value for datadog_apm_instrumentation_enabled is not valid. Valid values are "all", "host" and "docker"
when: datadog_apm_instrumentation_enabled not in ["all", "host", "docker"]

- name: Check if docker daemon config dir exists
stat:
path: /etc/docker
register: agent_docker_daemon_config_dir

- name: Fail if APM Host container injection requirements aren't met (Docker installed)
fail:
msg: >
/etc/docker does not exist. Please ensure docker is installed or disable the
datadog_apm_instrumentation_enabled="docker" flag.
when: >-
datadog_apm_instrumentation_enabled in ["all", "docker"]
and (
agent_docker_daemon_config_dir.stat.isdir is not defined
or not agent_docker_daemon_config_dir.stat.isdir
)
- name: Fail if datadog_manage_config is not enabled
fail:
msg: "APM Host Injection requires datadog_manage_config: true"
when: not datadog_manage_config

- name: Set internal values for APM host injection datadog_config
set_fact:
agent_dd_apm_host_inject_config:
apm_config:
receiver_socket: /opt/datadog/apm/inject/run/apm.socket
use_dogstatsd: true
dogstatsd_socket: /opt/datadog/apm/inject/run/dsd.socket

- name: Fail if provided config is not compatible with APM host injection
fail:
msg: |
The provided config is not compatible with APM host injection. The expected config parameters to be included are:
"{{ agent_dd_apm_host_inject_config | to_nice_yaml }}"
when: item.condition
loop:
- condition: >-
{{
'use_dogstatsd' in agent_datadog_config
and agent_datadog_config['use_dogstatsd'] != agent_dd_apm_host_inject_config['use_dogstatsd']
}}
- condition: >-
{{
'dogstatsd_socket' in agent_datadog_config
and agent_datadog_config['dogstatsd_socket'] != agent_dd_apm_host_inject_config['dogstatsd_socket']
}}
- condition: >-
{{
'apm_config' in agent_datadog_config
and 'receiver_socket' in agent_datadog_config['apm_config']
and agent_datadog_config['apm_config']['receiver_socket'] != agent_dd_apm_host_inject_config['apm_config']['receiver_socket']
}}
- name: Update datadog_config including config values needed for APM host injection
set_fact:
agent_datadog_config: "{{ agent_datadog_config | combine(agent_dd_apm_host_inject_config, list_merge='keep') }}"
41 changes: 41 additions & 0 deletions tasks/apm-inject-install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
- name: Include APM host injection Debian install tasks
include_tasks: pkg-debian/install-apm-inject-latest.yml
when: not ansible_check_mode and ansible_facts.os_family == "Debian"

- name: Include APM host injection RedHat install tasks
include_tasks: pkg-redhat/install-apm-inject-latest.yml
when: not ansible_check_mode and ansible_facts.os_family in ["RedHat", "Rocky", "AlmaLinux"]

- name: Check if dd-host-install needs to run
command: dd-host-install --no-config-change --no-agent-restart --dry-run
register: agent_dd_host_install_cmd
changed_when: false
failed_when: agent_dd_host_install_cmd.rc >= 2

- name: Run APM host injection setup script
command: dd-host-install --no-config-change --no-agent-restart
notify: restart datadog-agent
when: not ansible_check_mode and agent_dd_host_install_cmd.rc == 1 and datadog_apm_instrumentation_enabled in ["all", "host"]
changed_when: true

- name: Check if dd-container-install needs to run
command: dd-container-install --dry-run
register: agent_dd_container_install_cmd
changed_when: false
failed_when: agent_dd_container_install_cmd.rc >= 2

- name: Create Docker APM injection config file
template:
src: apm-inject-docker-config.yaml.j2
dest: /etc/datadog-agent/inject/docker_config.yaml
mode: "0640"
owner: root
group: "{{ datadog_group }}"
when: datadog_apm_instrumentation_docker_config

- name: Run APM host-docker injection (Docker) setup script
# this command will change /etc/docker/daemon.json and reload docker if changes are made.
command: dd-container-install
when: not ansible_check_mode and agent_dd_container_install_cmd.rc == 1 and datadog_apm_instrumentation_enabled in ["all", "docker"]
changed_when: true
12 changes: 12 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
include_tasks: facts-ansible9.yml
when: ansible_version.major == 2 and ansible_version.minor < 10

- name: Initialize internal datadog_config variable
set_fact:
agent_datadog_config: "{{ datadog_config }}"

- name: Check if OS is supported
include_tasks: os-check.yml

Expand All @@ -17,6 +21,10 @@
- name: Set Facts for Datadog Agent Major Version
include_tasks: set-parse-version.yml

- name: Check and resolve APM injection settings
include_tasks: apm-inject-check.yml
when: datadog_apm_instrumentation_enabled != ""

- name: Debian Install Tasks
include_tasks: pkg-debian.yml
when: ansible_facts.os_family == "Debian" and not agent_datadog_skip_install
Expand Down Expand Up @@ -69,3 +77,7 @@
- name: Integrations Tasks
include_tasks: integration.yml
when: datadog_integration is defined

- name: APM Host injection tasks
include_tasks: apm-inject-install.yml
when: datadog_apm_instrumentation_enabled != ""
12 changes: 12 additions & 0 deletions tasks/pkg-debian/install-apm-inject-latest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Set APM injection install packages
set_fact:
agent_dd_apm_install_pkgs: "{{ (agent_dd_apm_install_pkgs | default([], true)) + ['datadog-apm-library-' + item] }}"
with_items: "{{ datadog_apm_instrumentation_languages }}"

- name: Install APM inject libraries
apt:
name: "{{ ['datadog-apm-inject'] + (agent_dd_apm_install_pkgs | default([], true)) }}"
state: latest # noqa package-latest
update_cache: true
cache_valid_time: "{{ datadog_apt_cache_valid_time }}"
5 changes: 3 additions & 2 deletions tasks/pkg-redhat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@

- name: Set versioned includepkgs variable
set_fact:
agent_datadog_includepkgs: "{{ datadog_agent_flavor }}-{{ agent_datadog_agent_redhat_version | regex_replace('^\\d+:', '') }}"
agent_datadog_includepkgs:
"{{ datadog_agent_flavor }}-{{ agent_datadog_agent_redhat_version | regex_replace('^\\d+:', '') }} {{ datadog_inject_apm_flavor }}"
when: agent_datadog_agent_redhat_version is defined

- name: Set plain includepkgs variable
set_fact:
agent_datadog_includepkgs: "{{ datadog_agent_flavor }}"
agent_datadog_includepkgs: "{{ datadog_agent_flavor }} {{ datadog_inject_apm_flavor }}"
when: agent_datadog_agent_redhat_version is not defined

- name: Install Datadog Agent 5 yum repo
Expand Down
19 changes: 19 additions & 0 deletions tasks/pkg-redhat/install-apm-inject-latest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
- name: Set APM injection install packages
set_fact:
agent_dd_apm_install_pkgs: "{{ (agent_dd_apm_install_pkgs | default([], true)) + ['datadog-apm-library-' + item] }}"
with_items: "{{ datadog_apm_instrumentation_languages }}"

- name: Install APM inject libraries (dnf)
dnf:
name: "{{ ['datadog-apm-inject'] + (agent_dd_apm_install_pkgs | default([], true)) }}"
update_cache: true
state: latest # noqa package-latest
when: not ansible_check_mode and ansible_pkg_mgr == "dnf"

- name: Install APM inject libraries (yum)
yum:
name: "{{ ['datadog-apm-inject'] + (agent_dd_apm_install_pkgs | default([], true)) }}"
update_cache: true
state: latest # noqa package-latest
when: not ansible_check_mode and ansible_pkg_mgr == "yum"
3 changes: 3 additions & 0 deletions templates/apm-inject-docker-config.yaml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# {{ ansible_managed }}

{{ datadog_apm_instrumentation_docker_config | to_nice_yaml }}
10 changes: 5 additions & 5 deletions templates/datadog.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

[Main]

{% if datadog_config["dd_url"] is not defined -%}
{% if agent_datadog_config["dd_url"] is not defined -%}
dd_url: {{ datadog_url | default('https://app.datadoghq.com') }}
{% endif %}

{% if datadog_config["api_key"] is not defined -%}
{% if agent_datadog_config["api_key"] is not defined -%}
api_key: {{ datadog_api_key | default('youshouldsetthis') }}
{% endif %}

{% if datadog_config["use_mount"] is not defined -%}
{% if agent_datadog_config["use_mount"] is not defined -%}
use_mount: {{ datadog_use_mount | default('no') }}
{% endif %}

{# These variables are free-style, passed through a hash -#}
{% if datadog_config -%}
{% for key, value in datadog_config | dictsort -%}
{% if agent_datadog_config -%}
{% for key, value in agent_datadog_config | dictsort -%}
{{ key }}: {{ value }}
{% endfor -%}
{% endif %}
Expand Down
10 changes: 5 additions & 5 deletions templates/datadog.yaml.j2
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# {{ ansible_managed }}

{% if datadog_site is defined
and datadog_config["site"] is not defined -%}
and agent_datadog_config["site"] is not defined -%}
site: {{ datadog_site }}
{% endif %}

{% if datadog_config["dd_url"] is not defined
{% if agent_datadog_config["dd_url"] is not defined
and datadog_url is defined -%}
dd_url: {{ datadog_url }}
{% endif %}

{% if datadog_config["api_key"] is not defined -%}
{% if agent_datadog_config["api_key"] is not defined -%}
api_key: {{ datadog_api_key | default('youshouldsetthis') }}
{% endif %}

{% if datadog_config | default({}, true) | length > 0 -%}
{{ datadog_config | to_nice_yaml }}
{% if agent_datadog_config | default({}, true) | length > 0 -%}
{{ agent_datadog_config | to_nice_yaml }}
{% endif %}

0 comments on commit 16e6af9

Please sign in to comment.