Skip to content

Commit

Permalink
fix some telemetry related warnings, and standardize on test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
benwilson512 committed Nov 23, 2023
1 parent d8644ff commit 228fa2e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 48 deletions.
18 changes: 11 additions & 7 deletions test/absinthe/execution/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,7 @@ defmodule Absinthe.Execution.SubscriptionTest do
[:absinthe, :subscription, :publish, :start],
[:absinthe, :subscription, :publish, :stop]
],
fn event, measurements, metadata, config ->
send(self(), {event, measurements, metadata, config})
end,
&Absinthe.TestTelemetryHelper.send_to_pid/4,

Check warning on line 646 in test/absinthe/execution/subscription_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15 / OTP 25

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 646 in test/absinthe/execution/subscription_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14 / OTP 25

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 646 in test/absinthe/execution/subscription_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14 / OTP 24

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 646 in test/absinthe/execution/subscription_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15 / OTP 24

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)
%{}
)

Expand All @@ -657,10 +655,13 @@ defmodule Absinthe.Execution.SubscriptionTest do
context: %{pubsub: PubSub}
)

assert_receive {[:absinthe, :execute, :operation, :start], measurements, %{id: id}, _config}
assert_receive {:telemetry_event,
{[:absinthe, :execute, :operation, :start], measurements, %{id: id}, _config}}

assert System.convert_time_unit(measurements[:system_time], :native, :millisecond)

assert_receive {[:absinthe, :execute, :operation, :stop], _, %{id: ^id}, _config}
assert_receive {:telemetry_event,
{[:absinthe, :execute, :operation, :stop], _, %{id: ^id}, _config}}

Absinthe.Subscription.publish(PubSub, "foo", thing: client_id)
assert_receive({:broadcast, msg})
Expand All @@ -672,8 +673,11 @@ defmodule Absinthe.Execution.SubscriptionTest do
} == msg

# Subscription events
assert_receive {[:absinthe, :subscription, :publish, :start], _, %{id: id}, _config}
assert_receive {[:absinthe, :subscription, :publish, :stop], _, %{id: ^id}, _config}
assert_receive {:telemetry_event,
{[:absinthe, :subscription, :publish, :start], _, %{id: id}, _config}}

assert_receive {:telemetry_event,
{[:absinthe, :subscription, :publish, :stop], _, %{id: ^id}, _config}}

:telemetry.detach(context.test)
end
Expand Down
27 changes: 15 additions & 12 deletions test/absinthe/integration/execution/telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Elixir.Absinthe.Integration.Execution.TelemetryTest do
[:absinthe, :execute, :operation, :start],
[:absinthe, :execute, :operation, :stop]
],
&__MODULE__.handle_event/4,
&Absinthe.TestTelemetryHelper.send_to_pid/4,

Check warning on line 13 in test/absinthe/integration/execution/telemetry_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15 / OTP 25

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 13 in test/absinthe/integration/execution/telemetry_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14 / OTP 25

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 13 in test/absinthe/integration/execution/telemetry_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14 / OTP 24

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 13 in test/absinthe/integration/execution/telemetry_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15 / OTP 24

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)
%{}
)

Expand All @@ -21,10 +21,6 @@ defmodule Elixir.Absinthe.Integration.Execution.TelemetryTest do
:ok
end

def handle_event(event, measurements, metadata, config) do
send(self(), {event, measurements, metadata, config})
end

defmodule TestSchema do
use Absinthe.Schema

Expand Down Expand Up @@ -60,27 +56,34 @@ defmodule Elixir.Absinthe.Integration.Execution.TelemetryTest do
assert %{"asyncThing" => "ASYNC", "objectThing" => %{"name" => "Foo"}} == data

# Operation events
assert_receive {[:absinthe, :execute, :operation, :start], measurements, %{id: id}, _config}
assert_receive {:telemetry_event,
{[:absinthe, :execute, :operation, :start], measurements, %{id: id}, _config}}

assert System.convert_time_unit(measurements[:system_time], :native, :millisecond)

assert_receive {[:absinthe, :execute, :operation, :stop], measurements, %{id: ^id} = meta,
_config}
assert_receive {:telemetry_event,
{[:absinthe, :execute, :operation, :stop], measurements, %{id: ^id} = meta,
_config}}

assert is_number(measurements[:duration])
assert %Absinthe.Blueprint{} = meta[:blueprint]
assert meta[:options][:schema] == TestSchema

# Field events
assert_receive {[:absinthe, :resolve, :field, :start], measurements, %{id: id}, _}
assert_receive {:telemetry_event,
{[:absinthe, :resolve, :field, :start], measurements, %{id: id}, _}}

assert System.convert_time_unit(measurements[:system_time], :native, :millisecond)

assert_receive {[:absinthe, :resolve, :field, :stop], measurements, %{id: ^id} = meta, _}
assert_receive {:telemetry_event,
{[:absinthe, :resolve, :field, :stop], measurements, %{id: ^id} = meta, _}}

assert is_number(measurements[:duration])
assert %Absinthe.Resolution{} = meta[:resolution]
assert is_list(meta[:middleware])

assert_receive {[:absinthe, :resolve, :field, :stop], _, _, _}
assert_receive {:telemetry_event, {[:absinthe, :resolve, :field, :stop], _, _, _}}
# Don't execute for resolvers that don't call a resolver function (ie: default `Map.get`)
refute_receive {[:absinthe, :resolve, :field, :stop], _, _, _}
refute_receive {:telemetry_event, {[:absinthe, :resolve, :field, :stop], _, _, _}}
end
end
24 changes: 10 additions & 14 deletions test/absinthe/middleware/async_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,34 +88,30 @@ defmodule Absinthe.Middleware.AsyncTest do
{asyncThing}
"""

pid = self()

:ok =
:telemetry.attach_many(
"#{test}",
[
[:absinthe, :middleware, :async, :task, :start],
[:absinthe, :middleware, :async, :task, :stop]
],
fn name, measurements, metadata, _config ->
send(pid, {:telemetry_event, name, measurements, metadata})
end,
_config = %{}
&Absinthe.TestTelemetryHelper.send_to_pid/4,

Check warning on line 98 in test/absinthe/middleware/async_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15 / OTP 25

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 98 in test/absinthe/middleware/async_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14 / OTP 25

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 98 in test/absinthe/middleware/async_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14 / OTP 24

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 98 in test/absinthe/middleware/async_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15 / OTP 24

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)
%{pid: self()}
)

assert {:ok, %{data: %{"asyncThing" => "we async now"}}} == Absinthe.run(doc, Schema)

assert_receive {:telemetry_event, [:absinthe, :middleware, :async, :task, :start],
%{system_time: _}, %{}}
assert_receive {:telemetry_event,
{[:absinthe, :middleware, :async, :task, :start], %{system_time: _}, _, _}}

assert_receive {:telemetry_event, [:absinthe, :middleware, :async, :task, :stop],
%{duration: _}, %{}}
assert_receive {:telemetry_event,
{[:absinthe, :middleware, :async, :task, :stop], %{duration: _}, _, _}}

assert_receive {:telemetry_event, [:absinthe, :middleware, :async, :task, :start],
%{system_time: _}, %{}}
assert_receive {:telemetry_event,
{[:absinthe, :middleware, :async, :task, :start], %{system_time: _}, _, _}}

assert_receive {:telemetry_event, [:absinthe, :middleware, :async, :task, :stop],
%{duration: _}, %{}}
assert_receive {:telemetry_event,
{[:absinthe, :middleware, :async, :task, :stop], %{duration: _}, _, _}}
end

test "can resolve a field using a cooler but probably confusing to some people helper" do
Expand Down
14 changes: 7 additions & 7 deletions test/absinthe/middleware/batch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,20 @@ defmodule Absinthe.Middleware.BatchTest do
[:absinthe, :middleware, :batch, :start],
[:absinthe, :middleware, :batch, :stop]
],
fn name, measurements, metadata, _ ->
send(self(), {:telemetry_event, name, measurements, metadata})
end,
&Absinthe.TestTelemetryHelper.send_to_pid/4,

Check warning on line 128 in test/absinthe/middleware/batch_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15 / OTP 25

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 128 in test/absinthe/middleware/batch_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14 / OTP 25

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 128 in test/absinthe/middleware/batch_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14 / OTP 24

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 128 in test/absinthe/middleware/batch_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15 / OTP 24

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)
nil
)

assert {:ok, %{data: data}} = Absinthe.run(doc, Schema)
assert expected_data == data

assert_receive {:telemetry_event, [:absinthe, :middleware, :batch, :start], %{system_time: _},
%{id: _, batch_fun: _, batch_opts: _, batch_data: _}}
assert_receive {:telemetry_event,
{[:absinthe, :middleware, :batch, :start], %{system_time: _},
%{id: _, batch_fun: _, batch_opts: _, batch_data: _}, _}}

assert_receive {:telemetry_event, [:absinthe, :middleware, :batch, :stop], %{duration: _},
%{id: _, batch_fun: _, batch_opts: _, batch_data: _, result: _}}
assert_receive {:telemetry_event,
{[:absinthe, :middleware, :batch, :stop], %{duration: _},
%{id: _, batch_fun: _, batch_opts: _, batch_data: _, result: _}, _}}
end

test "propagates the OTel context" do
Expand Down
20 changes: 12 additions & 8 deletions test/absinthe/schema/notation/experimental/import_sdl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ defmodule Absinthe.Schema.Notation.Experimental.ImportSdlTest do
[:absinthe, :execute, :operation, :start],
[:absinthe, :execute, :operation, :stop]
],
&__MODULE__.handle_event/4,
&Absinthe.TestTelemetryHelper.send_to_pid/4,

Check warning on line 592 in test/absinthe/schema/notation/experimental/import_sdl_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15 / OTP 25

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 592 in test/absinthe/schema/notation/experimental/import_sdl_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14 / OTP 25

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 592 in test/absinthe/schema/notation/experimental/import_sdl_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14 / OTP 24

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)

Check warning on line 592 in test/absinthe/schema/notation/experimental/import_sdl_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15 / OTP 24

Absinthe.TestTelemetryHelper.send_to_pid/4 is undefined (module Absinthe.TestTelemetryHelper is not available or is yet to be defined)
%{}
)

Expand All @@ -605,16 +605,20 @@ defmodule Absinthe.Schema.Notation.Experimental.ImportSdlTest do
%{data: %{"posts" => [%{"upcasedTitle" => "FOO"}, %{"upcasedTitle" => "BAR"}]}}} =
Absinthe.run(@query, Definition)

assert_receive {[:absinthe, :execute, :operation, :start], _, %{id: id}, _config}
assert_receive {:telemetry_event,
{[:absinthe, :execute, :operation, :start], _, %{id: id}, _config}}

assert_receive {[:absinthe, :execute, :operation, :stop], _measurements, %{id: ^id},
_config}
assert_receive {:telemetry_event,
{[:absinthe, :execute, :operation, :stop], _measurements, %{id: ^id},
_config}}

assert_receive {[:absinthe, :resolve, :field, :start], _measurements,
%{resolution: %{definition: %{name: "posts"}}}, _config}
assert_receive {:telemetry_event,
{[:absinthe, :resolve, :field, :start], _measurements,
%{resolution: %{definition: %{name: "posts"}}}, _config}}

assert_receive {[:absinthe, :resolve, :field, :stop], _measurements,
%{resolution: %{definition: %{name: "posts"}}}, _config}
assert_receive {:telemetry_event,
{[:absinthe, :resolve, :field, :stop], _measurements,
%{resolution: %{definition: %{name: "posts"}}}, _config}}
end
end
end

0 comments on commit 228fa2e

Please sign in to comment.