Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add container id to ApiServer.State and send in header #38

Merged
merged 3 commits into from
Oct 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -95,12 +96,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 @@ -133,8 +149,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})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


response =
traces
Expand Down