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 telemetry to api server #28

Merged
merged 7 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
77 changes: 42 additions & 35 deletions lib/spandex_datadog/api_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ defmodule SpandexDatadog.ApiServer do
"""
@spec send_trace(Trace.t(), Keyword.t()) :: :ok
def send_trace(%Trace{} = trace, opts \\ []) do
timeout = Keyword.get(opts, :timeout, 30_000)
GenServer.call(__MODULE__, {:send_trace, trace}, timeout)
:telemetry.span([:spandex_datadog, :send_trace], %{trace: trace}, fn ->
timeout = Keyword.get(opts, :timeout, 30_000)
GregMefford marked this conversation as resolved.
Show resolved Hide resolved
result = GenServer.call(__MODULE__, {:send_trace, trace}, timeout)
{result, %{result: result}}
end)
end

@deprecated "Please use send_trace/2 instead"
Expand Down Expand Up @@ -144,47 +147,51 @@ defmodule SpandexDatadog.ApiServer do
agent_pid: agent_pid
} = state
) do
all_traces = [trace | waiting_traces]
:telemetry.span([:spandex_datadog, :handle_call, :send_trace], %{trace: trace, state: state}, fn ->
GregMefford marked this conversation as resolved.
Show resolved Hide resolved
all_traces = [trace | waiting_traces]

if verbose? do
trace_count = length(all_traces)
if verbose? do
trace_count = length(all_traces)

span_count = Enum.reduce(all_traces, 0, fn trace, acc -> acc + length(trace.spans) end)
span_count = Enum.reduce(all_traces, 0, fn trace, acc -> acc + length(trace.spans) end)

Logger.info(fn -> "Sending #{trace_count} traces, #{span_count} spans." end)
Logger.info(fn -> "Sending #{trace_count} traces, #{span_count} spans." end)

Logger.debug(fn -> "Trace: #{inspect(all_traces)}" end)
end
Logger.debug(fn -> "Trace: #{inspect(all_traces)}" end)
end

if asynchronous? do
below_sync_threshold? =
Agent.get_and_update(agent_pid, fn count ->
if count < sync_threshold do
{true, count + 1}
else
{false, count}
end
end)

if below_sync_threshold? do
Task.start(fn ->
try do
send_and_log(all_traces, state)
after
Agent.update(agent_pid, fn count -> count - 1 end)
end
end)
if asynchronous? do
below_sync_threshold? =
Agent.get_and_update(agent_pid, fn count ->
if count < sync_threshold do
{true, count + 1}
else
{false, count}
end
end)

if below_sync_threshold? do
Task.start(fn ->
try do
send_and_log(all_traces, state)
after
Agent.update(agent_pid, fn count -> count - 1 end)
end
end)
else
# We get benefits from running in a separate process (like better GC)
# So we async/await here to mimic the behavour above but still apply backpressure
task = Task.async(fn -> send_and_log(all_traces, state) end)
Task.await(task)
end
else
# We get benefits from running in a separate process (like better GC)
# So we async/await here to mimic the behavour above but still apply backpressure
task = Task.async(fn -> send_and_log(all_traces, state) end)
Task.await(task)
send_and_log(all_traces, state)
end
else
send_and_log(all_traces, state)
end

{:reply, :ok, %{state | waiting_traces: []}}
state = %{state | waiting_traces: []}

{{:reply, :ok, state}, %{state: state}}
end)
end

@spec send_and_log([Trace.t()], State.t()) :: :ok
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ defmodule SpandexDatadog.MixProject do
{:git_ops, "~> 2.0", only: [:dev]},
{:inch_ex, "~> 2.0", only: [:dev, :test]},
{:spandex, "~> 3.0"},
{:telemetry, "~> 0.4"},
{:httpoison, "~> 0.13", only: :test},
{:msgpax, "~> 2.2.1"}
]
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
"plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"},
"spandex": {:hex, :spandex, "3.0.1", "1979e96372cef16dd7a9b079da5b21647d6d7f2d3e6aa544d9ce5638e9350cb5", [:mix], [{:decorator, "~> 1.2", [hex: :decorator, repo: "hexpm", optional: true]}, {:optimal, "~> 0.3.3", [hex: :optimal, repo: "hexpm", optional: false]}, {:plug, ">= 1.0.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "22a5046b67d751cf4bbebf1ee23d977d8e05ad750ea578070ba6e5b293b64ff2"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm", "603561dc0fd62f4f2ea9b890f4e20e1a0d388746d6e20557cafb1b16950de88c"},
"telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
}