Skip to content

Commit

Permalink
add container id to ApiServer.State and send in header (#38)
Browse files Browse the repository at this point in the history
* add container id to ApiServer.State and send in header
  • Loading branch information
dallincrane authored Oct 23, 2021
1 parent 3d08a7f commit 7fb6c34
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/spandex_datadog/api_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ defmodule SpandexDatadog.ApiServer do
:waiting_traces,
:batch_size,
:sync_threshold,
:agent_pid
:agent_pid,
:container_id
]
end

Expand Down Expand Up @@ -78,12 +79,27 @@ 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"),
[_, container_id] <- Regex.run(@cgroup_regex, file_binary) do
container_id
else
_ -> nil
end
end

@doc """
Send spans asynchronously to DataDog.
"""
Expand Down Expand Up @@ -116,8 +132,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
Expand Down

0 comments on commit 7fb6c34

Please sign in to comment.