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 spansaction for regular DT #467

Merged
merged 2 commits into from
Jan 7, 2025
Merged
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
13 changes: 13 additions & 0 deletions examples/apps/phx_example/test/phx_example_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ defmodule PhxExampleTest do
assert event[:status] == 200
end

test "Phoenix spans generated" do
TestSupport.restart_harvest_cycle(Collector.SpanEvent.HarvestCycle)
{:ok, %{body: body}} = request("/phx/home", unquote(server))
assert body =~ "Some content"

span_events = TestSupport.gather_harvest(Collector.SpanEvent.Harvester)

tx_span = TestSupport.find_span(span_events, "/Phoenix/PhxExampleWeb.HomeLive/index")
process_span = TestSupport.find_span(span_events, "Transaction Root Process")

assert process_span[:parentId] == tx_span[:guid]
end

@tag :capture_log
test "Phoenix error" do
TestSupport.restart_harvest_cycle(Collector.Metric.HarvestCycle)
Expand Down
7 changes: 7 additions & 0 deletions examples/apps/test_support/lib/test_support.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ defmodule TestSupport do
end)
end

def find_span(spans, name) do
Enum.find_value(spans, fn
[%{name: ^name} = span, _, _] -> span
_span -> false
end)
end

def simulate_agent_enabled(_context) do
Process.whereis(Harvest.TaskSupervisor) ||
NewRelic.EnabledSupervisor.start_link(:ok)
Expand Down
4 changes: 2 additions & 2 deletions lib/new_relic/span/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ defmodule NewRelic.Span.Event do
component: category[:component] || "component",
"span.kind": "client"
})
|> NewRelic.Util.coerce_attributes()
|> Map.merge(custom)
|> NewRelic.Util.coerce_attributes()
end

def merge_category_attributes(%{category: "datastore"} = span, category_attributes) do
Expand All @@ -89,8 +89,8 @@ defmodule NewRelic.Span.Event do
component: category[:component] || "component",
"span.kind": "client"
})
|> NewRelic.Util.coerce_attributes()
|> Map.merge(custom)
|> NewRelic.Util.coerce_attributes()
end

def merge_category_attributes(span, category_attributes),
Expand Down
43 changes: 10 additions & 33 deletions lib/new_relic/transaction/complete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ defmodule NewRelic.Transaction.Complete do

defp extract_span_events(:sampling, %{sampled: true} = tx_attrs, pid, spawns, exits) do
spawned_process_span_events(tx_attrs, spawns, exits)
|> add_root_process_span_event(tx_attrs, pid)
|> add_spansactions(tx_attrs, pid)
end

defp extract_span_events(_trace_mode, _tx_attrs, _pid, _spawns, _exits) do
Expand All @@ -208,31 +208,6 @@ defmodule NewRelic.Transaction.Complete do
Util.Apdex.calculate(duration_s, apdex_t())
end

defp add_root_process_span_event(spans, tx_attrs, pid) do
[
%NewRelic.Span.Event{
trace_id: tx_attrs[:traceId],
transaction_id: tx_attrs[:guid],
sampled: tx_attrs[:sampled],
priority: tx_attrs[:priority],
category: "generic",
name: "Transaction Root Process",
guid: DistributedTrace.generate_guid(pid: pid),
parent_id: tx_attrs[:parentSpanId],
timestamp: tx_attrs[:start_time],
duration: tx_attrs[:duration_s],
entry_point: true,
category_attributes:
%{
pid: inspect(pid)
}
|> maybe_add(:tracingVendors, tx_attrs[:tracingVendors])
|> maybe_add(:trustedParentId, tx_attrs[:trustedParentId])
}
| spans
]
end

@spansaction_exclude_attrs [
:guid,
:traceId,
Expand All @@ -253,23 +228,29 @@ defmodule NewRelic.Transaction.Complete do
trace_id: tx_attrs[:traceId],
parent_id: tx_attrs[:parentSpanId],
name: tx_attrs[:name],
category: "Transaction",
sampled: tx_attrs[:sampled],
priority: tx_attrs[:priority],
category: "generic",
entry_point: true,
timestamp: tx_attrs[:start_time],
duration: tx_attrs[:duration_s],
category_attributes:
tx_attrs
|> Map.drop(@spansaction_exclude_attrs)
|> Map.merge(NewRelic.Config.automatic_attributes())
|> maybe_add(:tracingVendors, tx_attrs[:tracingVendors])
|> maybe_add(:trustedParentId, tx_attrs[:trustedParentId])
|> Map.merge(%{
tracingVendors: tx_attrs[:tracingVendors],
trustedParentId: tx_attrs[:trustedParentId]
})
},
%NewRelic.Span.Event{
guid: DistributedTrace.generate_guid(pid: pid),
transaction_id: tx_attrs[:guid],
trace_id: tx_attrs[:traceId],
category: "generic",
name: "Transaction Root Process",
sampled: tx_attrs[:sampled],
priority: tx_attrs[:priority],
parent_id: tx_attrs[:guid],
timestamp: tx_attrs[:start_time],
duration: tx_attrs[:duration_s],
Expand All @@ -279,10 +260,6 @@ defmodule NewRelic.Transaction.Complete do
]
end

def maybe_add(attrs, _key, nil), do: attrs
def maybe_add(attrs, _key, ""), do: attrs
def maybe_add(attrs, key, value), do: Map.put(attrs, key, value)

defp spawned_process_span_events(tx_attrs, process_spawns, process_exits) do
process_spawns
|> collect_process_segments(process_exits)
Expand Down
3 changes: 3 additions & 0 deletions lib/new_relic/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ defmodule NewRelic.Util do
{_key, nil} ->
[]

{_key, ""} ->
[]

{key, value} when is_number(value) when is_boolean(value) ->
[{key, value}]

Expand Down
2 changes: 1 addition & 1 deletion test/infinite_tracing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ defmodule InfiniteTracingTest do

tx_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction"
attr[:"nr.entryPoint"] == true
end)

tx_root_process_span =
Expand Down
14 changes: 7 additions & 7 deletions test/instrumented/task_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule InstrumentedTaskTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction"
attr[:"nr.entryPoint"] == true
end)

refute spansaction.attributes[:not_instrumented]
Expand Down Expand Up @@ -108,7 +108,7 @@ defmodule InstrumentedTaskTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction"
attr[:"nr.entryPoint"] == true
end)

refute spansaction.attributes[:not_instrumented]
Expand Down Expand Up @@ -168,7 +168,7 @@ defmodule InstrumentedTaskTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction"
attr[:"nr.entryPoint"] == true
end)

refute spansaction.attributes[:not_instrumented]
Expand Down Expand Up @@ -250,7 +250,7 @@ defmodule InstrumentedTaskTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction"
attr[:"nr.entryPoint"] == true
end)

refute spansaction.attributes[:not_instrumented]
Expand Down Expand Up @@ -322,7 +322,7 @@ defmodule InstrumentedTaskTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction"
attr[:"nr.entryPoint"] == true
end)

refute spansaction.attributes[:not_instrumented]
Expand Down Expand Up @@ -385,7 +385,7 @@ defmodule InstrumentedTaskTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction"
attr[:"nr.entryPoint"] == true
end)

refute spansaction.attributes[:not_instrumented]
Expand Down Expand Up @@ -414,7 +414,7 @@ defmodule InstrumentedTaskTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction"
attr[:"nr.entryPoint"] == true
end)

refute spansaction.attributes[:not_instrumented]
Expand Down
6 changes: 3 additions & 3 deletions test/other_transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ defmodule OtherTransactionTest do
)

span_events = TestHelper.gather_harvest(Collector.SpanEvent.Harvester)
assert length(span_events) == 3
assert length(span_events) == 4

TestHelper.pause_harvest_cycle(Collector.TransactionEvent.HarvestCycle)
TestHelper.pause_harvest_cycle(Collector.TransactionTrace.HarvestCycle)
Expand Down Expand Up @@ -200,7 +200,7 @@ defmodule OtherTransactionTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction" && attr[:name] == "Test/Error"
attr[:"nr.entryPoint"] == true && attr[:name] == "Test/Error"
end)

assert spansaction.attributes[:error]
Expand Down Expand Up @@ -241,7 +241,7 @@ defmodule OtherTransactionTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction" && attr[:name] == "Test/ExpectedError"
attr[:"nr.entryPoint"] == true && attr[:name] == "Test/ExpectedError"
end)

refute spansaction.attributes[:error]
Expand Down
4 changes: 2 additions & 2 deletions test/sidecar_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ defmodule SidecarTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction"
attr[:"nr.entryPoint"] == true
end)

assert spansaction.attributes[:root] == "YES"
Expand Down Expand Up @@ -352,7 +352,7 @@ defmodule SidecarTest do

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:category] == "Transaction" && attr[:name] == "Test/double_connect_test"
attr[:"nr.entryPoint"] == true && attr[:name] == "Test/double_connect_test"
end)

assert spansaction.attributes[:root] == "YES"
Expand Down
12 changes: 10 additions & 2 deletions test/span_event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,12 @@ defmodule SpanEventTest do

span_events = TestHelper.gather_harvest(Collector.SpanEvent.Harvester)

[tx_root_process_event, _, _] =
[spansaction_event, _, _] =
Enum.find(span_events, fn [ev, _, _] -> ev[:"nr.entryPoint"] == true end)

[tx_root_process_event, _, _] =
Enum.find(span_events, fn [ev, _, _] -> ev[:parentId] == spansaction_event[:guid] end)

[request_process_event, _, _] =
Enum.find(span_events, fn [ev, _, _] -> ev[:parentId] == tx_root_process_event[:guid] end)

Expand All @@ -220,13 +223,15 @@ defmodule SpanEventTest do
assert tx_event[:parentId] == "7d3efb1b173fecfa"

assert tx_event[:traceId] == "d6b4ba0c3a712ca"
assert spansaction_event[:traceId] == "d6b4ba0c3a712ca"
assert tx_root_process_event[:traceId] == "d6b4ba0c3a712ca"
assert request_process_event[:traceId] == "d6b4ba0c3a712ca"
assert function_event[:traceId] == "d6b4ba0c3a712ca"
assert nested_function_event[:traceId] == "d6b4ba0c3a712ca"
assert task_event[:traceId] == "d6b4ba0c3a712ca"
assert nested_external_event[:traceId] == "d6b4ba0c3a712ca"

assert spansaction_event[:transactionId] == tx_event[:guid]
assert tx_root_process_event[:transactionId] == tx_event[:guid]
assert request_process_event[:transactionId] == tx_event[:guid]
assert function_event[:transactionId] == tx_event[:guid]
Expand All @@ -235,6 +240,7 @@ defmodule SpanEventTest do
assert nested_external_event[:transactionId] == tx_event[:guid]

assert tx_event[:sampled] == true
assert spansaction_event[:sampled] == true
assert tx_root_process_event[:sampled] == true
assert request_process_event[:sampled] == true
assert function_event[:sampled] == true
Expand All @@ -243,6 +249,7 @@ defmodule SpanEventTest do
assert nested_external_event[:sampled] == true

assert tx_event[:priority] == 0.987654
assert spansaction_event[:priority] == 0.987654
assert tx_root_process_event[:priority] == 0.987654
assert request_process_event[:priority] == 0.987654
assert function_event[:priority] == 0.987654
Expand All @@ -255,7 +262,8 @@ defmodule SpanEventTest do
assert function_event[:"tracer.reductions"] |> is_number
assert function_event[:"tracer.reductions"] > 1

assert tx_root_process_event[:parentId] == "5f474d64b9cc9b2a"
assert spansaction_event[:parentId] == "5f474d64b9cc9b2a"
assert tx_root_process_event[:parentId] == spansaction_event[:guid]
assert request_process_event[:parentId] == tx_root_process_event[:guid]
assert function_event[:parentId] == request_process_event[:guid]
assert nested_function_event[:parentId] == function_event[:guid]
Expand Down
Loading