Skip to content

Commit

Permalink
squash! Add Absinthe.Plug integration test.
Browse files Browse the repository at this point in the history
WIP: promising lead on #11
  • Loading branch information
Garth Kidd committed Jun 24, 2019
1 parent 90a0b10 commit bfec6e0
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/opencensus_absinthe_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,62 @@ defmodule Opencensus.AbsintheTest do
pipeline: {__MODULE__, :traced_pipeline}
)

def call(conn, opts) do
span_ctx = :ocp.current_span_ctx() |> IO.inspect(label: "#{__MODULE__}.call/2 span_ctx")

try do
conn
|> super(opts)
|> assign(:called_all_plugs, true)
|> IO.inspect(label: "#{__MODULE__}.call/2 conn AFTER")
rescue
err ->
{err, :ocp.current_span_ctx()} |> IO.inspect(label: "#{__MODULE__}.call/2 err")
reraise(err, [], __STACKTRACE__)
after
unwind_span_failures(span_ctx)
:ocp.with_span_ctx(span_ctx)
end
end

defmodule SpanContext do
require Record
@fields Record.extract(:span_ctx, from_lib: "opencensus/include/opencensus.hrl")
Record.defrecordp(:span_ctx, @fields)
defstruct Keyword.keys(@fields)

def from(record) when Record.is_record(record, :span_ctx),
do: struct!(__MODULE__, span_ctx(record))

def from(:undefined), do: nil
end

@spec unwind_span_failures(:opencensus.span_ctx() | %SpanContext{}) :: nil
defp unwind_span_failures(target_span_ctx) when is_tuple(target_span_ctx) do
target_span_ctx
|> SpanContext.from()
|> dead_child_spans(:ocp.current_span_ctx())
|> IO.inspect(label: "dead spans")
|> Enum.each(&:oc_trace.finish_span(&1))
end

@spec dead_child_spans(%SpanContext{}, :opencensus.span_ctx(), [:opencensus.span_ctx()]) :: [
:opencensus.span_ctx()
]
defp dead_child_spans(target, span_ctx, spans \\ [])

defp dead_child_spans(%SpanContext{}, :undefined, _), do: []

defp dead_child_spans(%SpanContext{} = target, span_ctx, spans) do
%{span_id: span_id, trace_id: trace_id} = target

case SpanContext.from(span_ctx) do
%{span_id: ^span_id} -> spans
%{trace_id: ^trace_id} -> [span_ctx | spans]
_ -> []
end
end

def traced_pipeline(config, pipeline_opts \\ []) do
config
|> Absinthe.Plug.default_pipeline(pipeline_opts)
Expand Down

0 comments on commit bfec6e0

Please sign in to comment.