Skip to content

Commit

Permalink
Add Absinthe.Plug integration test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Garth Kidd committed Jun 25, 2019
1 parent b02f21e commit 9457b1c
Show file tree
Hide file tree
Showing 6 changed files with 367 additions and 2 deletions.
6 changes: 6 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
use Mix.Config

config :opencensus, :send_interval_ms, 5

config :logger,
backends: [:console],
level: :warn
13 changes: 13 additions & 0 deletions lib/opencensus/absinthe/middleware.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ defmodule Opencensus.Absinthe.Middleware do
end

@doc false
@spec on_complete(Resolution.t(), span_ctx: :opencensus.span_ctx()) :: Resolution.t()
def on_complete(%{state: :resolved} = resolution, span_ctx: span_ctx) do
# credo:disable-for-next-line
error_count = length(resolution.errors)
status = if error_count == 0, do: "ok", else: "error"

:oc_trace.put_attributes(
%{
"absinthe.field.resolution_error_count" => error_count,
"absinthe.field.resolution_status" => status
},
span_ctx
)

:oc_trace.finish_span(span_ctx)
resolution
end
Expand Down
23 changes: 22 additions & 1 deletion lib/opencensus/absinthe/phase/pop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,32 @@ defmodule Opencensus.Absinthe.Phase.Pop do

alias Absinthe.Blueprint
alias Absinthe.Phase
alias Opencensus.Absinthe.Acc

@impl true
@spec run(Blueprint.t(), keyword()) :: Phase.result_t()
def run(blueprint, _opts \\ []) do
:ocp.finish_span()
acc = Acc.get(blueprint)

{status, error_count} =
case blueprint do
%{result: %{errors: errors}} -> {:error, length(errors)}
_ -> {:ok, 0}
end

:oc_trace.put_attributes(
%{
"absinthe.blueprint.error_count" => error_count,
"absinthe.blueprint.status" => Atom.to_string(status)
},
acc.span_ctx
)

# Finish our span, even if it isn't current:
:oc_trace.finish_span(acc.span_ctx)
# Restore our parent span:
:ocp.with_span_ctx(acc.parent_span_ctx)

{:ok, blueprint}
end
end
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ defmodule Opencensus.Absinthe.MixProject do
{:ex_doc, ">= 0.0.0", only: :docs},
{:excoveralls, "~> 0.11.1", only: :test},
{:inch_ex, "~> 2.0.0", only: :docs},
{:jason, "~> 1.0", only: :test},
{:licensir, "~> 0.4.0", only: :test},
{:mix_test_watch, "~> 0.8", only: :test},
{:opencensus, "~> 0.9.2"}
{:opencensus, "~> 0.9.2"},
{:telemetry, "~> 0.4", only: :test}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
"rfc3339": {:hex, :rfc3339, "0.9.0", "2075653dc9407541c84b1e15f8bda2abe95fb17c9694025e079583f2d19c1060", [:mix, :rebar], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"telemetry": {:hex, :telemetry, "0.4.0", "8339bee3fa8b91cb84d14c2935f8ecf399ccd87301ad6da6b71c09553834b2ab", [:rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
"wts": {:hex, :wts, "0.3.0", "5cdf22c775cb1ebae24c326a5db6074d753c42f4bd12a9aa47cc62d3e2c71ad1", [:rebar3], [{:rfc3339, "~>0.9.0", [hex: :rfc3339, repo: "hexpm", optional: false]}], "hexpm"},
}
Loading

0 comments on commit 9457b1c

Please sign in to comment.