Skip to content

Commit e9121f4

Browse files
author
Garth Kidd
committed
squash! Add Absinthe.Plug integration test.
WIP: promising lead on #11
1 parent 9457b1c commit e9121f4

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

test/opencensus_absinthe_test.exs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,62 @@ defmodule Opencensus.AbsintheTest do
5555
pipeline: {__MODULE__, :traced_pipeline}
5656
)
5757

58+
def call(conn, opts) do
59+
span_ctx = :ocp.current_span_ctx() |> IO.inspect(label: "#{__MODULE__}.call/2 span_ctx")
60+
61+
try do
62+
conn
63+
|> super(opts)
64+
|> assign(:called_all_plugs, true)
65+
|> IO.inspect(label: "#{__MODULE__}.call/2 conn AFTER")
66+
rescue
67+
err ->
68+
{err, :ocp.current_span_ctx()} |> IO.inspect(label: "#{__MODULE__}.call/2 err")
69+
reraise(err, [], __STACKTRACE__)
70+
after
71+
unwind_span_failures(span_ctx)
72+
:ocp.with_span_ctx(span_ctx)
73+
end
74+
end
75+
76+
defmodule SpanContext do
77+
require Record
78+
@fields Record.extract(:span_ctx, from_lib: "opencensus/include/opencensus.hrl")
79+
Record.defrecordp(:span_ctx, @fields)
80+
defstruct Keyword.keys(@fields)
81+
82+
def from(record) when Record.is_record(record, :span_ctx),
83+
do: struct!(__MODULE__, span_ctx(record))
84+
85+
def from(:undefined), do: nil
86+
end
87+
88+
@spec unwind_span_failures(:opencensus.span_ctx() | %SpanContext{}) :: nil
89+
defp unwind_span_failures(target_span_ctx) when is_tuple(target_span_ctx) do
90+
target_span_ctx
91+
|> SpanContext.from()
92+
|> dead_child_spans(:ocp.current_span_ctx())
93+
|> IO.inspect(label: "dead spans")
94+
|> Enum.each(&:oc_trace.finish_span(&1))
95+
end
96+
97+
@spec dead_child_spans(%SpanContext{}, :opencensus.span_ctx(), [:opencensus.span_ctx()]) :: [
98+
:opencensus.span_ctx()
99+
]
100+
defp dead_child_spans(target, span_ctx, spans \\ [])
101+
102+
defp dead_child_spans(%SpanContext{}, :undefined, _), do: []
103+
104+
defp dead_child_spans(%SpanContext{} = target, span_ctx, spans) do
105+
%{span_id: span_id, trace_id: trace_id} = target
106+
107+
case SpanContext.from(span_ctx) do
108+
%{span_id: ^span_id} -> spans
109+
%{trace_id: ^trace_id} -> [span_ctx | spans]
110+
_ -> []
111+
end
112+
end
113+
58114
def traced_pipeline(config, pipeline_opts \\ []) do
59115
config
60116
|> Absinthe.Plug.default_pipeline(pipeline_opts)

0 commit comments

Comments
 (0)