diff --git a/lib/spandex_datadog/api_server.ex b/lib/spandex_datadog/api_server.ex index acbc288..5d00eb7 100644 --- a/lib/spandex_datadog/api_server.ex +++ b/lib/spandex_datadog/api_server.ex @@ -210,7 +210,7 @@ defmodule SpandexDatadog.ApiServer do def format(%Span{} = span), do: format(span, 1, []) @spec format(Span.t(), integer(), Keyword.t()) :: map() - def format(%Span{} = span, priority, _baggage) do + def format(%Span{} = span, priority, baggage) do %{ trace_id: span.trace_id, span_id: span.id, @@ -222,7 +222,7 @@ defmodule SpandexDatadog.ApiServer do resource: span.resource, service: span.service, type: span.type, - meta: meta(span), + meta: meta(span, baggage), metrics: %{ _sampling_priority_v1: priority } @@ -231,14 +231,15 @@ defmodule SpandexDatadog.ApiServer do # Private Helpers - @spec meta(Span.t()) :: map - defp meta(span) do + @spec meta(Span.t(), Keyword.t()) :: map + defp meta(span, baggage) do %{} |> add_datadog_meta(span) |> add_error_data(span) |> add_http_data(span) |> add_sql_data(span) |> add_tags(span) + |> add_baggage(baggage) |> Enum.reject(fn {_k, v} -> is_nil(v) end) |> Enum.into(%{}) end @@ -308,6 +309,11 @@ defmodule SpandexDatadog.ApiServer do Map.merge(meta, Enum.into(tags, %{})) end + @spec add_baggage(map, Keyword.t()) :: map + defp add_baggage(meta, baggage) do + Enum.into(baggage, meta) + end + @spec error(nil | Keyword.t()) :: integer defp error(nil), do: 0 diff --git a/test/support/api_server_test.exs b/test/support/api_server_test.exs index 5b8e349..bcb7977 100644 --- a/test/support/api_server_test.exs +++ b/test/support/api_server_test.exs @@ -162,4 +162,13 @@ defmodule SpandexDatadog.ApiServerTest do assert_received {:put_datadog_spans, ^formatted, ^url, _} end end + + describe "ApiServer.format/3" do + test "it fills in Datadog metadata using the trace baggage", %{trace: trace} do + span = trace.spans |> Enum.at(0) + payload = ApiServer.format(span, 1, user_id: "U53R1D") + + assert get_in(payload, [:meta, :user_id]) == "U53R1D" + end + end end