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

Fix fill Datadog trace meta from Trace baggage #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 10 additions & 4 deletions lib/spandex_datadog/api_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions test/support/api_server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Choose a reason for hiding this comment

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

Use a function call when a pipeline is only one function long

payload = ApiServer.format(span, 1, user_id: "U53R1D")

assert get_in(payload, [:meta, :user_id]) == "U53R1D"
end
end
end