From f2b55f6eeac16d7d78f1e9aa0c33aa80b30d947f Mon Sep 17 00:00:00 2001 From: Dallin Crane <10406163+dallincrane@users.noreply.github.com> Date: Wed, 10 Mar 2021 11:57:32 -0700 Subject: [PATCH 1/3] add container id to ApiServer.State and send in header --- lib/spandex_datadog/api_server.ex | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/spandex_datadog/api_server.ex b/lib/spandex_datadog/api_server.ex index 47694c4..dc223ba 100644 --- a/lib/spandex_datadog/api_server.ex +++ b/lib/spandex_datadog/api_server.ex @@ -26,7 +26,8 @@ defmodule SpandexDatadog.ApiServer do :waiting_traces, :batch_size, :sync_threshold, - :agent_pid + :agent_pid, + :container_id ] end @@ -95,12 +96,28 @@ defmodule SpandexDatadog.ApiServer do waiting_traces: [], batch_size: opts[:batch_size], sync_threshold: opts[:sync_threshold], - agent_pid: agent_pid + agent_pid: agent_pid, + container_id: get_container_id() } {:ok, state} end + @cgroup_uuid "[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}" + @cgroup_ctnr "[0-9a-f]{64}" + @cgroup_task "[0-9a-f]{32}-\\d+" + @cgroup_regex Regex.compile!("/(#{@cgroup_uuid}|#{@cgroup_ctnr}|#{@cgroup_task})(?:.scope)?$", "m") + + defp get_container_id() do + with {:ok, file_binary} <- File.read("/proc/self/cgroup"), + true <- String.valid?(file_binary), + [_, container_id] <- Regex.run(@cgroup_regex, file_binary) do + container_id + else + _ -> nil + end + end + @doc """ Send spans asynchronously to DataDog. """ @@ -133,8 +150,9 @@ defmodule SpandexDatadog.ApiServer do end @spec send_and_log([Trace.t()], State.t()) :: :ok - def send_and_log(traces, %{verbose?: verbose?} = state) do + def send_and_log(traces, %{container_id: container_id, verbose?: verbose?} = state) do headers = @headers ++ [{"X-Datadog-Trace-Count", length(traces)}] + headers = headers ++ List.wrap(if container_id, do: {"Datadog-Container-ID", container_id}) response = traces From bfb3ba279396931fc6306ad9499ea4231315160b Mon Sep 17 00:00:00 2001 From: Dallin Crane <10406163+dallincrane@users.noreply.github.com> Date: Wed, 14 Apr 2021 11:28:09 -0700 Subject: [PATCH 2/3] remove valid string check --- lib/spandex_datadog/api_server.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/spandex_datadog/api_server.ex b/lib/spandex_datadog/api_server.ex index dc223ba..a66a95e 100644 --- a/lib/spandex_datadog/api_server.ex +++ b/lib/spandex_datadog/api_server.ex @@ -110,7 +110,6 @@ defmodule SpandexDatadog.ApiServer do defp get_container_id() do with {:ok, file_binary} <- File.read("/proc/self/cgroup"), - true <- String.valid?(file_binary), [_, container_id] <- Regex.run(@cgroup_regex, file_binary) do container_id else From b80a876b63d0a3557e5fbfc8f4219de5e6504888 Mon Sep 17 00:00:00 2001 From: Dallin Crane <10406163+dallincrane@users.noreply.github.com> Date: Tue, 4 May 2021 10:45:55 -0600 Subject: [PATCH 3/3] update regex --- lib/spandex_datadog/api_server.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/spandex_datadog/api_server.ex b/lib/spandex_datadog/api_server.ex index a66a95e..0beca52 100644 --- a/lib/spandex_datadog/api_server.ex +++ b/lib/spandex_datadog/api_server.ex @@ -106,7 +106,7 @@ defmodule SpandexDatadog.ApiServer do @cgroup_uuid "[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}" @cgroup_ctnr "[0-9a-f]{64}" @cgroup_task "[0-9a-f]{32}-\\d+" - @cgroup_regex Regex.compile!("/(#{@cgroup_uuid}|#{@cgroup_ctnr}|#{@cgroup_task})(?:.scope)?$", "m") + @cgroup_regex Regex.compile!(".*(#{@cgroup_uuid}|#{@cgroup_ctnr}|#{@cgroup_task})(?:\\.scope)?$", "m") defp get_container_id() do with {:ok, file_binary} <- File.read("/proc/self/cgroup"),