Skip to content

Commit

Permalink
Parse datadog distributed headers properly (#51)
Browse files Browse the repository at this point in the history
I made a boo-boo in the original distributed tracing; datadog expects the trace ID and parent span ID to be integers (i.e. not in string format). As headers are strings across the network, we need to parse 'em back out to integers. Have tested this now and am seeing lovely distributed traces.
  • Loading branch information
aspett authored and zachdaniel committed Jun 1, 2018
1 parent 4e74433 commit 15b4c2d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/adapters/datadog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ defmodule Spandex.Adapters.Datadog do
"""
@impl Spandex.Adapters.Adapter
@spec continue_trace(String.t(), term, term, Keyword.t()) :: {:ok, term} | {:error, term}
def continue_trace(name, trace_id, span_id, opts) do
def continue_trace(name, trace_id, span_id, opts) when is_integer(trace_id) and is_integer(span_id) do
trace = get_trace(:undefined)

cond do
Expand Down Expand Up @@ -306,8 +306,18 @@ defmodule Spandex.Adapters.Datadog do
conn
|> Plug.Conn.get_req_header(header_name)
|> List.first()
|> parse_header()
end

defp parse_header(header) when is_bitstring(header) do
case Integer.parse(header) do
{int, _} -> int
_ -> nil
end
end

defp parse_header(_header), do: nil

@spec get_trace(term) :: term
defp get_trace(default \\ nil) do
Process.get(:spandex_trace, default)
Expand Down
4 changes: 2 additions & 2 deletions test/plug/start_trace_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ defmodule Spandex.Plug.StartTraceTest do

new_conn = StartTrace.call(conn, ignored_routes: [], ignored_methods: [], tracer: Tracer)

assert %{trace_id: "12345", parent_id: "67890"} = Tracer.current_span()
assert %{trace_id: 12345, parent_id: 67890} = Tracer.current_span()

refute Tracer.current_span_id() == "67890"
refute Tracer.current_span_id() == 67890
refute is_nil(Tracer.current_span_id())

assert new_conn.assigns[:spandex_trace_request?]
Expand Down

0 comments on commit 15b4c2d

Please sign in to comment.