Skip to content

Commit

Permalink
Cleanup use of test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryseed committed Feb 12, 2025
1 parent b1c8d48 commit 92db998
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 217 deletions.
39 changes: 8 additions & 31 deletions examples/apps/absinthe_example/test/absinthe_example_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,16 @@ defmodule AbsintheExampleTest do
[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

spansaction =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "Absinthe/AbsintheExample.Schema/query/one.two.three"
end)

tx_root_process =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "Transaction Root Process"
end)

process =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "Process"
end)

operation =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "query:TestQuery"
end)

one_resolver =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "&AbsintheExample.Resolvers.one/3"
end)

three_resolver =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "&AbsintheExample.Resolvers.three/3"
end)
TestHelper.find_infinite_span(spans, "Absinthe/AbsintheExample.Schema/query/one.two.three")

tx_root_process = TestHelper.find_infinite_span(spans, "Transaction Root Process")
process = TestHelper.find_infinite_span(spans, "Process")
operation = TestHelper.find_infinite_span(spans, "query:TestQuery")
one_resolver = TestHelper.find_infinite_span(spans, "&AbsintheExample.Resolvers.one/3")
three_resolver = TestHelper.find_infinite_span(spans, "&AbsintheExample.Resolvers.three/3")

do_three_fn_trace =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "AbsintheExample.Resolvers.do_three/1"
end)
TestHelper.find_infinite_span(spans, "AbsintheExample.Resolvers.do_three/1")

assert operation.attributes[:"absinthe.operation.name"] == "TestQuery"
assert operation.attributes[:"absinthe.operation.type"] == "query"
Expand Down
16 changes: 4 additions & 12 deletions examples/apps/redix_example/test/redix_example_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ defmodule RedixExampleTest do

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

[get_event, _, _] =
Enum.find(span_events, fn [ev, _, _] -> ev[:name] == "Datastore/operation/Redis/GET" end)
get_event = TestHelper.find_span(span_events, "Datastore/operation/Redis/GET")

assert get_event[:"peer.address"] == "localhost:6379"
assert get_event[:"db.statement"] == "GET mykey"
Expand All @@ -65,18 +64,14 @@ defmodule RedixExampleTest do
assert get_event[:timestamp] |> is_number
assert get_event[:duration] > 0.0

[pipeline_event, _, _] =
Enum.find(span_events, fn [ev, _, _] ->
ev[:name] == "Datastore/operation/Redis/PIPELINE"
end)
pipeline_event = TestHelper.find_span(span_events, "Datastore/operation/Redis/PIPELINE")

assert pipeline_event[:"peer.address"] == "localhost:6379"

assert pipeline_event[:"db.statement"] ==
"DEL counter; INCR counter; INCR counter; GET counter"

[hset_event, _, _] =
Enum.find(span_events, fn [ev, _, _] -> ev[:name] == "Datastore/operation/Redis/HSET" end)
hset_event = TestHelper.find_span(span_events, "Datastore/operation/Redis/HSET")

assert hset_event[:"peer.address"] == "localhost:6379"
end
Expand All @@ -89,10 +84,7 @@ defmodule RedixExampleTest do

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

[err_event, _, _] =
Enum.find(span_events, fn [ev, _, _] ->
ev[:name] == "Datastore/operation/Redis/PIPELINE"
end)
err_event = TestHelper.find_span(span_events, "Datastore/operation/Redis/PIPELINE")

assert err_event[:"peer.address"] == "localhost:6379"
# On elixir 1.14 OTP 26, the error message is "unknown POSIX error: timeout"
Expand Down
51 changes: 9 additions & 42 deletions test/infinite_tracing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -131,40 +131,13 @@ defmodule InfiniteTracingTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

tx_root_process_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "Transaction Root Process"
end)

cowboy_request_process_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:"parent.id"] == tx_root_process_span[:id]
end)

function_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "InfiniteTracingTest.Traced.hello/0"
end)

nested_function_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "InfiniteTracingTest.Traced.do_hello/0"
end)

task_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "Process" && attr[:"parent.id"] == cowboy_request_process_span[:id]
end)

nested_external_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "External/example.com/HttpClient/GET"
end)
tx_span = TestHelper.find_infinite_span(spans, %{"nr.entryPoint": true})
tx_root_process_span = TestHelper.find_infinite_span(spans, "Transaction Root Process")
cowboy_request_process_span = TestHelper.find_infinite_span(spans, %{"parent.id": tx_root_process_span[:id]})
function_span = TestHelper.find_infinite_span(spans, "InfiniteTracingTest.Traced.hello/0")
nested_function_span = TestHelper.find_infinite_span(spans, "InfiniteTracingTest.Traced.do_hello/0")
task_span = TestHelper.find_infinite_span(spans, %{name: "Process", "parent.id": cowboy_request_process_span[:id]})
nested_external_span = TestHelper.find_infinite_span(spans, "External/example.com/HttpClient/GET")

[[_intrinsics, tx_event]] = TestHelper.gather_harvest(Collector.TransactionEvent.Harvester)

Expand Down Expand Up @@ -274,10 +247,7 @@ defmodule InfiniteTracingTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

error_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "InfiniteTracingTest.Traced.error/0"
end)
error_span = TestHelper.find_infinite_span(spans, "InfiniteTracingTest.Traced.error/0")

assert error_span.attributes[:"error.message"] == "(RuntimeError) Err"

Expand All @@ -294,10 +264,7 @@ defmodule InfiniteTracingTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

exit_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "InfiniteTracingTest.Traced.exit/0"
end)
exit_span = TestHelper.find_infinite_span(spans, "InfiniteTracingTest.Traced.exit/0")

assert exit_span.attributes[:"error.message"] == "(EXIT) :bad"

Expand Down
35 changes: 7 additions & 28 deletions test/instrumented/task_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ defmodule InstrumentedTaskTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

refute spansaction.attributes[:not_instrumented]
assert spansaction.attributes[:instrumented] == 6
Expand Down Expand Up @@ -93,10 +90,7 @@ defmodule InstrumentedTaskTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

refute spansaction.attributes[:not_instrumented]
assert spansaction.attributes[:instrumented] == "check"
Expand Down Expand Up @@ -151,10 +145,7 @@ defmodule InstrumentedTaskTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

refute spansaction.attributes[:not_instrumented]
assert spansaction.attributes[:instrumented] == "check"
Expand Down Expand Up @@ -233,10 +224,7 @@ defmodule InstrumentedTaskTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

refute spansaction.attributes[:not_instrumented]
assert spansaction.attributes[:instrumented] == "check"
Expand Down Expand Up @@ -305,10 +293,7 @@ defmodule InstrumentedTaskTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

refute spansaction.attributes[:not_instrumented]
assert spansaction.attributes[:instrumented] == 3
Expand Down Expand Up @@ -368,10 +353,7 @@ defmodule InstrumentedTaskTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

refute spansaction.attributes[:not_instrumented]
assert spansaction.attributes[:instrumented] == 3
Expand All @@ -397,10 +379,7 @@ defmodule InstrumentedTaskTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

refute spansaction.attributes[:not_instrumented]
assert spansaction.attributes[:instrumented] == "check"
Expand Down
10 changes: 2 additions & 8 deletions test/other_transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ defmodule OtherTransactionTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

assert spansaction.attributes[:error]
assert spansaction.attributes[:error_reason] =~ "RuntimeError"
Expand Down Expand Up @@ -220,10 +217,7 @@ defmodule OtherTransactionTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

refute spansaction.attributes[:error]
end
Expand Down
37 changes: 8 additions & 29 deletions test/sidecar_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -256,52 +256,33 @@ defmodule SidecarTest do

[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

assert spansaction.attributes[:root] == "YES"
refute spansaction.attributes[:async_nolink]
assert spansaction.attributes[:async_nolink_connected] == "YES"
assert spansaction.attributes[:async_stream_nolink_connected] == "YES"

tx_root_process_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "Transaction Root Process"
end)
tx_root_process_span = TestHelper.find_infinite_span(spans, "Transaction Root Process")

task_triggering_function_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "SidecarTest.Traced.instrumented_task_async_nolink/1"
end)
TestHelper.find_infinite_span(spans, "SidecarTest.Traced.instrumented_task_async_nolink/1")

assert task_triggering_function_span.attributes[:"parent.id"] == tx_root_process_span[:id]

task_triggered_process_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "Process" &&
attr[:"parent.id"] == task_triggering_function_span[:id]
end)
TestHelper.find_infinite_span(spans, %{name: "Process", "parent.id": task_triggering_function_span[:id]})

hey_function_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "SidecarTest.Traced.hey/0"
end)
hey_function_span = TestHelper.find_infinite_span(spans, "SidecarTest.Traced.hey/0")

assert hey_function_span.attributes[:"parent.id"] == task_triggered_process_span[:id]

connected_stream_task_process_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:"parent.id"] == task_triggered_process_span[:id]
end)
TestHelper.find_infinite_span(spans, %{"parent.id": task_triggered_process_span[:id]})

assert connected_stream_task_process_span.attributes[:name] == "Process"

hello_function_span =
Enum.find(spans, fn %{attributes: attr} ->
attr[:name] == "SidecarTest.Traced.hello/0"
end)
hello_function_span = TestHelper.find_infinite_span(spans, "SidecarTest.Traced.hello/0")

assert hello_function_span.attributes[:"parent.id"] == connected_stream_task_process_span[:id]
end
Expand Down Expand Up @@ -345,9 +326,7 @@ defmodule SidecarTest do
[%{spans: spans}] = TestHelper.gather_harvest(TelemetrySdk.Spans.Harvester)

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

assert spansaction.attributes[:root] == "YES"
assert spansaction.attributes[:async_nolink_connected] == "YES"
Expand Down
Loading

0 comments on commit 92db998

Please sign in to comment.