Skip to content

Commit

Permalink
Merge pull request #4 from Frameio/mcaterisano/GRAPH-1087/fix-absinth…
Browse files Browse the repository at this point in the history
…e-subscription-error-handling

[GRAPH-1087] add telemetry phase for config errors and fix test
  • Loading branch information
michaelcaterisano authored Jun 11, 2024
2 parents 3ecbd3e + 79a0ef8 commit 253b1f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
22 changes: 13 additions & 9 deletions lib/absinthe/phase/document/execution/resolution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,19 @@ defmodule Absinthe.Phase.Document.Execution.Resolution do
defp reduce_resolution(%{middleware: []} = res), do: res

defp reduce_resolution(%{middleware: [middleware | remaining_middleware]} = res) do
:telemetry.span [:absinthe, :middleware, :call], %{middleware: middleware, resolution: res}, fn ->
result =
case call_middleware(middleware, %{res | middleware: remaining_middleware}) do
%{state: :suspended} = res -> res
res -> reduce_resolution(res)
end

{result, %{middleware: middleware, resolution: res}}
end
:telemetry.span(
[:absinthe, :middleware, :call],
%{middleware: middleware, resolution: res},
fn ->
result =
case call_middleware(middleware, %{res | middleware: remaining_middleware}) do
%{state: :suspended} = res -> res
res -> reduce_resolution(res)
end

{result, %{middleware: middleware, resolution: res}}
end
)
end

defp call_middleware({{mod, fun}, opts}, res) do
Expand Down
14 changes: 8 additions & 6 deletions lib/absinthe/phase/subscription/subscribe_self.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ defmodule Absinthe.Phase.Subscription.SubscribeSelf do

Absinthe.Subscription.subscribe(pubsub, field_keys, subscription_id, blueprint)

{:replace, blueprint,
[
{Phase.Subscription.Result, topic: subscription_id},
{Phase.Telemetry, Keyword.put(options, :event, [:execute, :operation, :stop])}
]}
pipeline = [
{Phase.Subscription.Result, topic: subscription_id},
{Phase.Telemetry, Keyword.put(options, :event, [:execute, :operation, :stop])}
]

{:replace, blueprint, pipeline}
else
{:error, error} ->
blueprint = update_in(blueprint.execution.validation_errors, &[error | &1])

error_pipeline = [
{Phase.Document.Result, options}
{Phase.Document.Result, options},
{Phase.Telemetry, Keyword.put(options, :event, [:execute, :operation, :stop])}
]

{:replace, blueprint, error_pipeline}
Expand Down
17 changes: 7 additions & 10 deletions test/absinthe/execution/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,9 @@ defmodule Absinthe.Execution.SubscriptionTest do
[:absinthe, :subscription, :publish, :start],
[:absinthe, :subscription, :publish, :stop]
],
&Absinthe.TestTelemetryHelper.send_to_pid/4,
fn event, measurements, metadata, config ->
send(self(), {event, measurements, metadata, config})
end,
%{}
)

Expand All @@ -699,13 +701,11 @@ defmodule Absinthe.Execution.SubscriptionTest do
context: %{pubsub: PubSub}
)

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

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

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

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

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

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

:telemetry.detach(context.test)
end
Expand Down

0 comments on commit 253b1f3

Please sign in to comment.