Skip to content

Commit

Permalink
add telemetry phrase when subscription config errors
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcaterisano committed Jun 14, 2024
1 parent 66ccfee commit 7b929c2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
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
25 changes: 25 additions & 0 deletions test/absinthe/execution/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,31 @@ defmodule Absinthe.Execution.SubscriptionTest do
)
end

test "fires telemetry events when subscription config returns error", %{test: test} do
:ok =
:telemetry.attach_many(
"#{test}",
[
[:absinthe, :execute, :operation, :start],
[:absinthe, :execute, :operation, :stop]
],
&Absinthe.TestTelemetryHelper.send_to_pid/4,
%{pid: self()}
)

assert {:ok, %{errors: [%{locations: [%{column: 3, line: 2}], message: "unauthorized"}]}} ==
run_subscription(
@query,
Schema,
variables: %{"clientId" => "abc"},
context: %{pubsub: PubSub, authorized: false}
)

assert_received {:telemetry_event, {[:absinthe, :execute, :operation, :start], _, _, _}}

assert_received {:telemetry_event, {[:absinthe, :execute, :operation, :stop], _, _, _}}
end

@query """
subscription Example {
reliesOnDocument
Expand Down

0 comments on commit 7b929c2

Please sign in to comment.