Skip to content

Commit

Permalink
Add Absinthe.Plug integration test.
Browse files Browse the repository at this point in the history
WIP: promising lead on #11

Still having trouble with unwinding errors
  • Loading branch information
Garth Kidd committed Jul 12, 2019
1 parent df4b6cb commit 0eb4d73
Show file tree
Hide file tree
Showing 9 changed files with 557 additions and 4 deletions.
10 changes: 10 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
use Mix.Config

if Mix.env() == :test do
config :opencensus,
send_interval_ms: 1,
reporters: [{Opencensus.Absinthe.TestSupport.SpanCaptureReporter, []}]
end

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
10 changes: 8 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Opencensus.Absinthe.MixProject do
dialyzer: dialyzer(),
docs: docs(),
elixir: "~> 1.5",
elixirc_paths: elixirc_paths(Mix.env()),
package: package(),
preferred_cli_env: [
coveralls: :test,
Expand All @@ -36,6 +37,9 @@ defmodule Opencensus.Absinthe.MixProject do
]
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]

defp package() do
[
licenses: ["Apache 2.0"],
Expand All @@ -54,13 +58,15 @@ defmodule Opencensus.Absinthe.MixProject do
{:absinthe_plug, "~> 1.4.0", optional: true},
{:credo, "~> 1.1.0", only: :test},
{:dialyxir, "~> 1.0.0-rc.6", only: :dev, runtime: false},
{:ex_doc, ">= 0.0.0", only: :docs},
{:ex_doc, ">= 0.0.0", only: [:docs, :test]},
{:excoveralls, "~> 0.11.1", only: :test},
{:inch_ex, "~> 2.0.0", only: :docs},
{:jason, "~> 1.0", only: [:docs, :test]},
{:licensir, "~> 0.4.0", only: :test},
{:mix_test_watch, "~> 0.8", only: :test},
{:opencensus, "~> 0.9.2"},
{:opencensus_elixir, "~> 0.4.0"}
{:opencensus_plug, "~> 0.3", only: :test},
{:telemetry, "~> 0.4", only: :test}
]
end

Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"mix_test_watch": {:hex, :mix_test_watch, "0.9.0", "c72132a6071261893518fa08e121e911c9358713f62794a90c95db59042af375", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"},
"opencensus": {:hex, :opencensus, "0.9.2", "ab36b0c4e4500b976180bd088cea7520d345711a72df2d7188e2d7b9573a8728", [:rebar3], [{:counters, "~> 0.2.1", [hex: :counters, repo: "hexpm", optional: false]}, {:ctx, "~> 0.5", [hex: :ctx, repo: "hexpm", optional: false]}, {:wts, "~> 0.3", [hex: :wts, repo: "hexpm", optional: false]}], "hexpm"},
"opencensus_elixir": {:hex, :opencensus_elixir, "0.4.0", "ac00f3b027841ba47ad8a9205c442c2e5473c1ffeb45e9c2569cccd5d35fa814", [:mix], [{:opencensus, "~> 0.9", [hex: :opencensus, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
"opencensus_plug": {:hex, :opencensus_plug, "0.3.0", "ff04bdd59c39dee66fc56086e9300818325afe184a2e8919a8bb47912ad5164c", [:mix], [{:opencensus, "~> 0.9", [hex: :opencensus, repo: "hexpm", optional: false]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"plug": {:hex, :plug, "1.8.2", "0bcce1daa420f189a6491f3940cc77ea7fb1919761175c9c3b59800d897440fc", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm"},
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
Expand Down
Loading

0 comments on commit 0eb4d73

Please sign in to comment.