From bfec6e0054d7f63df8b21f574f460cf406f61201 Mon Sep 17 00:00:00 2001 From: Garth Kidd Date: Mon, 24 Jun 2019 10:24:18 +1000 Subject: [PATCH] squash! Add Absinthe.Plug integration test. WIP: promising lead on #11 --- test/opencensus_absinthe_test.exs | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/test/opencensus_absinthe_test.exs b/test/opencensus_absinthe_test.exs index 6781cea..4f677d0 100644 --- a/test/opencensus_absinthe_test.exs +++ b/test/opencensus_absinthe_test.exs @@ -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)